将ajax数据永久保存在PHP变量中

我可以将数据从 Ajax 发送到 PHP 并在 Ajax 中接收响应。有没有办法将 Ajax 数据保存在 PHP 中并在 PHP 页面中使用它?


郎朗坤
浏览 174回答 2
2回答

绝地无双

$.ajax({&nbsp; &nbsp; url:"<?= site_url('index/get_data') ?>",&nbsp; &nbsp; type:'post',&nbsp; &nbsp; dataType:'json',&nbsp; &nbsp; success: function (data) {&nbsp; &nbsp; &nbsp; &nbsp; $('div').html(data);&nbsp; &nbsp; }});&nbsp; &nbsp;&nbsp;您可以使用 jquery 设置动态数据,但我认为无法将 ajax 响应保存为 php 变量

慕后森

您可以创建一个function并在 ajaxsuccess函数上调用它,最后有两种方法可以将值放入phpbyform submit或ajax。我使用了 ajax 方法,即:&nbsp;function result(r){//"r" have response value&nbsp;&nbsp;//pass to ajax&nbsp;alert("i'm in");&nbsp; &nbsp;$.ajax({&nbsp;&nbsp;&nbsp; &nbsp; type: "POST",&nbsp;&nbsp;&nbsp; &nbsp; url: "page1.php",&nbsp;&nbsp;&nbsp; &nbsp; data: { 'value': r},&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; success: function(response){&nbsp; &nbsp; &nbsp; alert("done");&nbsp; &nbsp; },&nbsp; &nbsp;error: function (jqXHR) {&nbsp; &nbsp;console.log(jqXHR);&nbsp; &nbsp;alert("There was a problem");&nbsp; }});&nbsp;&nbsp;}$.ajax({&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; type: "POST",&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; url: "page.php",&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; success: function(response){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//passing response value to function "result"&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result(response);&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp;error: function (jqXHR) {&nbsp; &nbsp; &nbsp; &nbsp;console.log(jqXHR);&nbsp; &nbsp; &nbsp; &nbsp;alert("There was a problem");&nbsp; &nbsp; &nbsp; }&nbsp;});&nbsp;现在page1.php写如下:<?phpsession_start();//putting value in session$_SESSION['url'] = $_POST['value'];&nbsp; &nbsp;?>
打开App,查看更多内容
随时随地看视频慕课网APP