简体   繁体   English

通过ajax调用发送php变量

[英]send php variables via ajax call

According to jquery API this is the wat of sending value asigned to a name with ajax 根据jquery API,这是发送分配给带有ajax名称的值的方法

.data( key, value )

Problem is my values are already in php variables. 问题是我的值已经在php变量中。 how can i send them using ajax? 如何使用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. 当按钮被计时时,仍然找不到将php变量发送到另一个php页面的方法。 sad that jQuery API Documentation doesn't have examples. 遗憾的是jQuery API文档没有示例。

You have two php....let say page.php and ajax.php. 您有两个php ....让我们说page.php和ajax.php。 If you call ajax.php from ajax in page.php you must write variables from php to javascript with something like 如果您从page.php中的ajax调用ajax.php,则必须使用类似以下内容将变量从php写入javascript

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

and then add these variables to ajax call. 然后将这些变量添加到ajax调用中。

Best regards, nele 最好的问候,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;?>" >

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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