[英]How to Convert jQuery ajax success data to a PHP variable
I'm sending data to a php
page for processing through jQuery
and ajax
and as result I will get 我正在将数据发送到php
页面以通过jQuery
和ajax
处理,结果我将得到
success: function(data){
mydata = data;
}
my data is an url that looks like mydata = https://myurl.com
. 我的数据是一个看起来像mydata = https://myurl.com
的URL。 Now I want to set mydata to a php
variable and use it again inside another function in the main page, means: to set $url = mydata;
现在,我想将mydata设置为php
变量,并在主页的另一个函数中再次使用它,这意味着:设置$url = mydata;
. 。 How can I do that? 我怎样才能做到这一点?
Thanks, 谢谢,
You can do that using Post via a from, GET via a query string or you can use cookie like in this solution 您可以使用发件人中的Post,查询字符串中的GET进行操作,也可以像本解决方案中那样使用cookie
how to assign javascript variable value to php variable 如何将javascript变量值分配给php变量
You can send the mydata
variable to PHP
by calling an XMLHttpRequest()
via AJAX
. 您可以通过AJAX
调用XMLHttpRequest()
将mydata
变量发送到PHP
。
For example 例如
var ajax = new XMLHttpRequest(); // Create new request
ajax.open("POST", "mainpage.php"); // Open the file where you want to send it with POST
ajax.send("mydata="+mydata); // Send mydata variable to PHP
Then in PHP
you can check for the variable mydata
然后在PHP
您可以检查变量mydata
if(isset($_POST['mydata'])){
// do something
}
PHP scripts execute when you open a file and before everything else. 当您打开文件时以及其他所有操作之前,将执行PHP脚本。 Ajax calls exectue when the Javascript is completely loaded. 当Javascript完全加载后,Ajax会调用exectue。 (After php). (在php之后)。
So you cant do it. 所以你做不到。 could you give me more detail about what do you want to do? 您能给我更多您想做什么的细节吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.