在使用PHP和Ajax进行服务器端验证时遇到问题 [英] Got problems in server side validation using PHP and ajax
本文介绍了在使用PHP和Ajax进行服务器端验证时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我期望有一个输出,但没有显示任何输出.我的代码中是否缺少内容或出了什么问题?在此先感谢
我的主页上有这行代码
I was expecting an ouput but it didn''t show any. Is there missing in my code or what''s wrong? Thanks in advance
I have this line of code in my main page
<form>
<label for="text_email">E-mail:</label>
<input id="text_email" type="email" >
<script type="text/javascript" charset="utf-8" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js">
$(document).ready(function(){
$('#text_email').keyup(function(){
email($(this).val());
});
});
function email(str){
$.post('checkmail.php',{ email: str},
function(data){
$('#check_email').html(data.returnValue);
}, 'json');
}
</script>
<label for="text_email" id="check_email"></label>
</form>
这行代码
And this line of code
<?php
if(isset($_POST['email']))
{
try
{
$pdo=new PDO('mysql:host=localhost;dbname=class;charset=utf-8', 'root');
}
catch(PDOException $e)
{
echo 'Failed: '.$e->getMessage();
}
$stmt=$pdo->prepare('SELECT email FROM class where email=:email LIMIT 1');
$stmt->execute(array(':email'=>$_POST['email']));
if($stmt->rowCount()>0)
{
$check='E-mail cannot be use.';
}
else
{
$check='E-mail can be use.';
}
echo json_encode(array('returnValue'=>$check));
}
?>
推荐答案
(document).ready(function(){
(document).ready(function(){
('#text_email').keyup(function(){ email(
('#text_email').keyup(function(){ email(
(this).val()); }); }); 功能email(str){
(this).val()); }); }); function email(str){
这篇关于在使用PHP和Ajax进行服务器端验证时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文