简体   繁体   中英

send php variables via ajax call

According to jquery API this is the wat of sending value asigned to a name with ajax

.data( key, value )

Problem is my values are already in php variables. how can i send them using ajax?

this problem is relate to this question i asked yesterday. Still couldn't find a way to send php variables to another php page when a button is clocked. sad that jQuery API Documentation doesn't have examples.

You have two php....let say page.php and ajax.php. If you call ajax.php from ajax in page.php you must write variables from php to javascript with something like

var data1 = <?= $data1 ?>;
var data2 = <?= $data2 ?>;

and then add these variables to ajax call.

Best regards, nele

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<script type="text/javascript">

var ajax_value1 = $("#ajax_value1").val();

var ajax_value2 = $("#ajax_value2").val();

 $.ajax({

     type: "POST",

     url: 'sample.php?ajax_value1='+value1+'&value2='+ajax_value2,

     //Specify the datatype of response if necessary

     data: $("#your_form_id").serialize(),

     success: function(data){

     }

});

</script>


<!-- Set your ajax value in html input fields -->

<input type="text"  name="ajax_value1" id="ajax_value1" value="<?php echo $ajax_value1;?>" > 

<input type="text"  name="ajax_value2" id="ajax_value2" value="<?php echo $ajax_value2;?>" >

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM