首先,将此任务分解为小任务:
1) 在 JavaScript 中获取/处理变量
2) 将它们发送到 PHP
3)解析/处理那些
4) 根据结果发送响应回 JavaScript
5)处理响应并向用户显示消息
看一下这个例子,假设加载了 jquery.js。假设我们要发送我们拥有的输入值 - 电子邮件和密码。
<script type="text/javascript">
$("#Send").click(function(){
$.ajax({
type : "GET",
//Look carefully:
data : {
// it'll be PHP vars // This is JS vars
email : $("#email").val(),
password : $("#password").val()
},
success : function(respondFromPHP){
alert(respondFromPHP);
}
});
});
</script>
<input type="text" id="email" />
<input type="password" id="password" />
<br />
<button id="Send">Send to php</button>
在您的 php 脚本中,只需处理您获得的变量,如下所示:
<?php
print_r($_GET); // will print smth like Array("email" => "foo", "password" => "bar")
// Then create function so that you can simplify handling of the vars.
// Like this:
function validate_password($password){}
function validate_email($email){}