如何利用Ajax调用成功返回数据到php代码运行mysql查询在同一文件


How to utilize the Ajax call success return data to php code for running mysql query in same file

我正在从ajax调用获取数据。但这些数据来自Jquery,我将其保存在一个变量中。现在我希望这些数据被用于运行一些php和mysql代码。有人能解决这个问题吗?

 $("#submit_bt").click(function () {
    var name = $('#search-box').val();
    var dataString = 'name=' + name;
    if (name == "" ){
         $('.alert').show().html('Please fill all information')
    }
    else
    {             
         // AJAX Code To Submit Form.
         $.ajax({
             type: "POST",
             url: "read_data.php",
             data: dataString,
             cache: false,
             success: function (result) {
                     alert(result);
                 //$('.alert').show().html(result).delay(2000).fadeOut(3000);
                 setTimeout(function(){window.location.href = "index.php";},2000);
             }
         });
     }
 return result;
 });

如果您想在单击该按钮时导航到index.php页面,则按以下方式操作:

$("#submit_bt").click(function () {
    var name = $('#search-box').val();
    var dataString = 'name=' + name;
    if (name == "" ){
         $('.alert').show().html('Please fill all information')
    }
    else
    {             
     // AJAX Code To Submit Form.
     $.ajax({
         type: "POST",
         url: "read_data.php",
         data: dataString,
         cache: false,
         success: function (result) {
                 alert(result); //you may remove this. use console.log for debugging your js next time
             setTimeout(function(){window.location.href = "index.php?result="+result;},2000); //why the timeout?
         }
     });
     }
 });

更简单和合适的解决方案应该是重用ajax在另一个PHP文件中使用这个变量。

$("#submit_bt").click(function () {
var name = $('#search-box').val();
var dataString = 'name=' + name;
if (name == "" ){
     $('.alert').show().html('Please fill all information')
}
else
{             
     // AJAX Code To Submit Form.
     $.ajax({
         type: "POST",
         url: "read_data.php",
         data: dataString,
         cache: false,
         success: function (result) 
         {
             //AJAX code to execute your MySQL query
             $.ajax({
                 type: "POST",
                 url: "read_data2.php",
                 data: result,
                 cache: false,
                 success: function (result) 
                 {
                     //Manage your read_data2.php output
                 }
             });
         }
     });
 }