Send variable to a modal

0

How about, I'm trying to send an index.php variable to a modal without reloading the page at all, since some forms are previously filled in, then I can not just send by get and reload the page (which would be very easy)

var fech = $('#input').val();

I send index.php to a modal that is included in index.php , this modal I have it in a different modal.php file included in the same index.php as mentioned above

include("modal.php");

and probe with ajax, jquery etc but I just can not get the value (this scritp is inside index.php)

<script>
  $("#btnsend").click(function(){
    var fech = $('#input').val();
    $.ajax({
      method: "POST",
      url: "modal.php",
      data: { dato: fech }
    })
    .done(function(msg) {
       alert( "Data Saved: " + fech);
     });
  });
</script>

This was my last test, the value is printed on the alert with success but it is not seen in the modal.

already in modal.php that's how I get this variable in a normal way but it is not displayed.

echo $vr2=$_POST["dato"];
    
asked by matteo 09.06.2017 в 19:38
source

3 answers

1

You can change the method:"POST" by type: "POST" , another option is to contact the url with the parameters

<script> $("#btnsend").click(function(){ var fech = $('#input').val(); var direccion = "modal.php"+"?dato="+fech; $.ajax({ url: direccion, type:"POST" data: {} }) .done(function(msg) { alert( "Data Saved: " + fech); }); }); </script>

    
answered by 09.06.2017 в 22:08
1

I imagine that in the modal.php you have some php code, that is, some validations or calculations that you want to do with the variables that you are receiving, if you do not want to reload the page, instead of doing: <?php include "modal.php"; ?>

You must load the modal.php with ajax and send the data either by GET

$('#div_modal').load('modal.php?variable=1');

or by POST:

var fech = $('#input').val();
$.ajax({
  url: "modal.php",
  type:"POST",
  data: {"valor":fech}
})
.done(function(r) {
   $('#div_modal').html(r);
 });

in the modal you should have something like this:

<div class="modal">
  <input type="text" value="<?php print $_POST['variable']; ?>"/>
</div>
    
answered by 13.06.2017 в 19:28
0

Does the modal.php have it included in the index.php in this way?

<?php include "modal.php"; ?>

Or what are you invoking through ajax with:

$('#div_modal').load('modal.php');

I await your response to help you.

    
answered by 10.06.2017 в 19:12