如何将jQuery ajax成功数据转换为PHP变量

时间:2021-11-22 19:58:43

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. 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;. How can I do that?

我的数据是一个看起来像mydata = https://myurl.com的网址。现在我想将mydata设置为php变量并在主页面中的另一个函数内再次使用它,意思是:设置$ url = mydata;。我怎样才能做到这一点?

Thanks,

谢谢,

3 个解决方案

#1


-1  

PHP scripts execute when you open a file and before everything else. Ajax calls exectue when the Javascript is completely loaded. (After php).

PHP脚本在您打开文件时以及其他所有文件之前执行。当Javascript完全加载时,Ajax调用exectue。 (在PHP之后)。

So you cant do it. could you give me more detail about what do you want to do?

所以你不能这样做。你能告诉我你想做什么的更多细节吗?

#2


0  

You can do that using Post via a from, GET via a query string or you can use cookie like in this solution

您可以使用Post通过from,GET通过查询字符串来执行此操作,或者您可以使用此解决方案中的cookie

how to assign javascript variable value to php variable

如何将javascript变量值赋给php变量

#3


0  

You can send the mydata variable to PHP by calling an XMLHttpRequest() via AJAX.
For example

您可以通过AJAX调用XMLHttpRequest()将mydata变量发送到PHP。例如

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
}

#1


-1  

PHP scripts execute when you open a file and before everything else. Ajax calls exectue when the Javascript is completely loaded. (After php).

PHP脚本在您打开文件时以及其他所有文件之前执行。当Javascript完全加载时,Ajax调用exectue。 (在PHP之后)。

So you cant do it. could you give me more detail about what do you want to do?

所以你不能这样做。你能告诉我你想做什么的更多细节吗?

#2


0  

You can do that using Post via a from, GET via a query string or you can use cookie like in this solution

您可以使用Post通过from,GET通过查询字符串来执行此操作,或者您可以使用此解决方案中的cookie

how to assign javascript variable value to php variable

如何将javascript变量值赋给php变量

#3


0  

You can send the mydata variable to PHP by calling an XMLHttpRequest() via AJAX.
For example

您可以通过AJAX调用XMLHttpRequest()将mydata变量发送到PHP。例如

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
}