用Ajax执行php文件

当我点击“立即注册”按钮时,我想执行’input.php’,其中我有代码将数据插入数据库并显示成功消息。 我不想离开当前页面。

  $(document).ready(function() { $("#confirm").click(function() {  alert ("data Added successfully."); }); });  

我的代码给了我“数据已成功添加”消息,但PHP文件尚未执行,并且没有数据添加到数据库。 所有必要的数据都在会话变量中。

建议你尝试类似下面的东西。 您不应该尝试在jQuery脚本中执行PHP。 执行AJAX调用并传递数据并在PHP中处理它,而不是依赖于会话变量。 例如:

  

firstname,lastname将是您的正常会话数据。

您可以在jQuery API文档中了解有关jQuery.ajax()函数的更多信息。

要从javascript执行Php脚本,您必须使用Ajax。

以下代码:

 $("#confirm").click(function() {  alert ("data Added successfully."); }); 

不管用

http://api.jquery.com/jquery.ajax/

你需要使用AJAX。 Ajax是从页面内部通过javascript调用php文件的概念。 然后,您可以在变量中获取php页面输出,您可以选择是否显示它。 这项技术的一个例子是显示Facebook的更多post。 这可以通过jQuery轻松完成。

 $.post( PHP_FILE, { field1: 'value', field2: 'value'}).done(function( data ) {alert("this function will be run when the request is over and the variable data will have the output : " + data);}); 

你可以通过ajax post方法做到这一点..

  $.ready(function(){ $("#confirm").click(function() { $.ajax({ type: "POST", url: "give url to the input.php file ", data:, success:function(data) { alert('data');// data is the return value from input.php } }); }); 

});

试试这个:

   

我不太确定你在那里做过什么。 无论如何这里有一个js的片段应该为你做(我很确定在新的jQuery版本中有更好的方法来做到这一点):

 $.ajax({ url: "input.php", success: function(data) { // here, for example, you load the data received from input.php into // an html element with id #content and show an alert message $("#content").html(data); alert("Success") } });