2014-06-19 111 views
0

我正在制作一个将某些值发送到另一个脚本的ajax脚本。发送此值后,必须执行此脚本(使用这些值)。我尝试了几件事,但我不知道如何做到这一点。任何人有任何想法如何做到这一点?在Ajax POST上执行PHP脚本

$.ajax({ 
    url: 'script.php', 
    type: 'POST', 
    dataType: 'text', 
    data: {result:JSON.stringify(values)}, 
    success: function(response){ 
     // preform script? 
     }//end success 

}); //end ajax 
+0

这将执行'script.php'。你还需要做什么? – Barmar

回答

0

ajax本身可以接受数据键中的json。 它使您更易于阅读并且易于管理。

Ajax调用只是建立这样的JSON之前:

var data = {value1: value1, value2:value2, ..., valueN=valueN} 

然后用你的AJAX使用它:

$("#buttonID").click(function(){ 
    $.ajax({ 
     url: 'script.php', 
     type: 'POST', 
     dataType: 'json', 
     data: data, 
     success: function(response){ 
      $('body').append(response); 
      }//end success 

    }); //end ajax 
}); 

就这样,你可以在你的PHP念想的变量这个:

$value1 = $_POST['value1']; 
+0

是的,我已经做了这些事情,但我也想执行script.php,如果它成功传递值。 – user3756186

+0

这就是阿贾克斯所做的。 它使用这些参数运行url。 Yuor响应是页面的答案。 –

+0

'success:function(response){$(#element).html(response); }' –

0

你想在执行这个后执行另一个脚本吗? 在客户端计算机上运行的JS。你不能在那里运行PHP脚本。但你可以这样从服务器调用PHP

$.ajax({ 
    url: 'script.php', 
    type: 'POST', 
    dataType: 'text', 
    data: {result:JSON.stringify(values)}, 
    success: function(response){ 
     $.ajax({ 
      url: 'otherscript.php', 
      type: 'POST' ... }); 
     }//end success 

}); //end ajax 

或者你可以在js上重写你的代码。或者,你可以从PHP生成JS(

< SCRIPT SRC = 'js.php?有些'>