michal michal - 3 years ago 196
Ajax Question

Ajax how to get variable in php file

I'm trying to send var with input value from edit_pages.php to generate_pdf.php which is in iframe. How can I get input value in generate_pdf corectly? I need to reload page somehow on scucess with ajax to update iframe file?

edit_pages.php

<iframe id="miniBrowser" style="width:100%;height:100%;overflow:hidden;" src="generate_pdf.php"></iframe>


<div class="worksheet">
<div id="usrform">
EDIT: <input class="usrname" type="text">
<button id='zapisz'>zapisz</button>
</div>
</div>
<script>
$("#zapisz").on( 'click', function () {
var usrname = $('.usrname').val();

$.ajax({
type: "POST",
url: 'generate_pdf.php',
contentType: "application/json; charset=utf-8",
dataType: "json",
data: {usrnm:usrname},
success: function(data) {
}
});
alert(usrname);
})
</script>


in generate_pdf.php I'm trying to get ajax var, but I get undefined var.

$f1 = $_POST['usrnm'];
echo $f1;

Answer Source

on click get the username and load iframe src with the parameters. fram.src

edit_pages.php

<iframe id="miniBrowser" style="width:100%;height:100%;overflow:hidden;" src="generate_pdf.php"></iframe>


<div class="worksheet">
    <div id="usrform">
      EDIT: <input class="usrname" id="username" type="text">
      <button id='zapisz'>zapisz</button>
    </div>
</div>

<!-- Add jQuery Library if not added -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script>
$("#zapisz").on( 'click', function () {

  var usrname =  $('#username').val();
  var frame = document.getElementById("miniBrowser");
  frame.src = "generate_pdf.php?usrnm="+usrname;

})
</script>

generate_pdf.php

$f1 = $_GET['usrnm'];
echo $f1;

received as GET

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download