JavaScript事件,与PHP混合?

-2 javascript css php javascript-events

所以我遇到了一个巨大的错误,我提出了一个想法.所以我正在一个项目,在我的主网站上,我们需要在页面上设置一个正在工作的yadayda,但是我想添加让用户向我们发送他们的电子邮件的功能,但是在我们收到该数据之后,我发布了一个流行对话框会显示..但这不是我想要的工作.

所以我需要帮助,实际上是PHP和JavaScript事件,使它确认消息和电子邮件已发送,然后显示对话框.有谁知道如何做到这一点?或者至少如何在用户做某事之后进行对话显示,比如输入信息而不是单击按钮?如果有人可以提供帮助,我会非常欣赏它!

Lix*_*Lix 5

如果使用jQuery,则可以对服务器端脚本进行AJAX调用,并使用成功回调在客户端启动对话框.

$.ajax({
  url: 'ajax/test.php',
  data: { name: "WeLikeThePandaz", email: "panda@gmail.com" },
  success: function(response) {
    if (response.status == "OK"){
      // Show dialog 
    }else{
      // Let the user know there were errors
      alert(response.error);
    }
  }
},'json');
Run Code Online (Sandbox Code Playgroud)

以下是使用该$.ajax方法的相关文档-

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


您的服务器端PHP代码ajax/test.php然后可以解密发送的数据并组装一个json对象以返回到jQuery -

<?php
$err= '';
$name = sanitizeString($_POST['name']);
$email = sanitizeString($_POST['email']);
// note the sanitization of the strings before we insert them - always make sure
// to sanitize your data before insertion into your database.

// Insert data into database.
$result = mysql_query('INSERT INTO `user_table` VALUES...');
if (!$result) {
  $status = "FAIL";
  $err = mysql_error();
}else{
  $status = "OK";
} 

echo json_encode(array('error'=>$err,'status'=>$status)); // send the response
exit();

?>
Run Code Online (Sandbox Code Playgroud)