简体   繁体   English

Ajax成功之后,如何将数据传递给php的变量

[英]Ajax after success, how to pass data to a variable to php

I have script like this 我有这样的脚本

 function getval(sel) {
         var id= sel.value;  
                $.ajax({
                        type:"POST",
                        url:"./tab.php",
                        data:{id:id,task:'search'},
                         success: function(response){
                             //(I don't know what i should write for pass to php code)
                         }
            });
    }

I don't know how I can pass data response to php code ? 我不知道如何将数据response传递给php代码? For Example: if I alert response , it's show 123 .so I want pass value 123 to a variable in php $id = 123 例如:如果我alert response ,则显示123我想将值123传递给php $id = 123的变量。

response is the result passed BY php, not TO php. response是php传递的结果,而不是php传递的结果。 What's passed to php is the id and the task . 传递给php的是idtask

In your tab.php , you can access these two values : 在您的tab.php ,您可以访问以下两个值:

<?php
$id = $_POST['id'];
$task = $_POST['task'];
//do anything you want with it...
?>

This is not the right workflow. 这不是正确的工作流程。 PHP executes on runtime, so every time the page has finished loading you can't really put variables back into PHP (unless you reload the page). PHP在运行时执行,因此,每次页面加载完成时,您都无法真正将变量放回PHP中(除非您重新加载页面)。 This is where AJAX comes in, so you can call PHP scripts from JavaScript / jQuery and don't have to reload the page every time. 这是AJAX的来历,因此您可以从JavaScript / jQuery调用PHP脚本,而不必每次都重新加载页面。

The right thing to do is to either store the variable you have generated in the tab.php script in a database, $_SESSION, $_COOKIE or something similar like so: 正确的做法是将您在tab.php脚本中生成的变量存储在数据库$ _SESSION,$ _ tab.php或类似的文件中:

//put this at the top of all your php scripts you want to use the variable in
session_start();
//now store the variable you wanted to pass (in tab.php)
$_SESSION['returnValue'] = $myValue;

Now if you want to use the variable in other pages just echo it like so (remember to add session_start() at the top of the PHP script. 现在,如果您想在其他页面中使用该变量,就可以像这样回显它(记住在PHP脚本的顶部添加session_start()

echo $_SESSION['returnValue'];

First of all, start by reading this 首先,通过阅读开始

To answer your question, 为了回答您的问题,

function getval(sel) {
   var id= sel.value;  
   $.ajax({
      type:"POST",
      url:"./tab.php",
      data:{id:id,task:'search'},
      success: function(response){
         //(I don't know what i should write for pass to php code)
      }
    });
}

The result from id and task are sent via $_POST (type:"POST") to the page tab.php (url:"./tab.php"). idtask的结果通过$_POST (类型:“ POST”)发送到页面tab.php (URL:“ ./ tab.php”)。 If you want to do that on a different page, just change the url in your ajax call: url:"./any_other_page.php" and the posted values will be sent there 如果要在其他页面上执行此操作,只需在ajax调用中更改url:url:“ ./ any_other_page.php”,发布的值将发送到该页面

Finally, read THIS post as well. 最后,还请阅读帖子。 It is greatly written and very well explained. 它写得很好,解释也很好。

Hope it helps! 希望能帮助到你!
Keep on coding! 继续编码!
Ares. 战神

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

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