Php I';我被这篇阿贾克斯的帖子绊住了
结果: 这个ajax不能给我一个结果。我正在努力解决这个问题。我不知道为什么,也找不到错误在哪里。我尝试过另一种ajax,它可以工作,但我不知道这一种。这个怎么样?谁来帮帮我,谢谢 ztest1.php:Php I';我被这篇阿贾克斯的帖子绊住了,php,jquery,ajax,Php,Jquery,Ajax,结果: 这个ajax不能给我一个结果。我正在努力解决这个问题。我不知道为什么,也找不到错误在哪里。我尝试过另一种ajax,它可以工作,但我不知道这一种。这个怎么样?谁来帮帮我,谢谢 ztest1.php: <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <style&
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<style></style>
<script>
function checkemail() {
var status = document.getElementById("emailudahada");
var u = document.getElementById("email").value;
if (u != "") {
document.getElementById("email").style.backgroundColor = "yellow";
status.innerHTML = "<img src='/img/loading.GIF'></img>";
$.ajax({
url: '/ztest2.php',
type: "POST",
data: ({
email: u,
cekemailsignup: 'yes'
}),
success: function(result) {
status.innerHTML = result['result'] + "," + result['cekemailsignup'] + "," + result['email'];
}
});
} else {
document.getElementById("email").style.backgroundColor = "white";
}
}
</script>
</head>
<body>
<div id='emailudahada'></div>
<input type='text' id='email' onblur='checkemail()'></input>
</body>
</html>
函数checkemail(){
var status=document.getElementById(“emailudahada”);
var u=document.getElementById(“电子邮件”).value;
如果(u!=“”){
document.getElementById(“email”).style.backgroundColor=“黄色”;
status.innerHTML=“”;
$.ajax({
url:“/ztest2.php”,
类型:“POST”,
数据:({
电邮:u,,
Cekemail注册:“是”
}),
成功:功能(结果){
status.innerHTML=result['result']+”,“+result['cekemailsignup']+”,“+result['email'];
}
});
}否则{
document.getElementById(“email”).style.backgroundColor=“白色”;
}
}
ztest2.php:
<?php
include('ckcon.php');
$cekemailsignup=isset($_REQUEST['cekemailsignup'])?$_REQUEST['cekemailsignup']:null;
if($cekemailsignup=='yes'){
$email=isset($_REQUEST['email'])?$_REQUEST['email']:null;
$q=mysql_query("SELECT COUNT(email) AS ce FROM t_un WHERE email='$email' LIMIT 1");
$f=mysql_fetch_object($q);
$ce=$f->ce;
if($email==null){
$result="<img src='/img/xred.png'></img> <font color='red'>Cant be null value</font>";
}
if(strlen($email) < 4){
$result="<img src='/img/xred.png'></img> <font color='red'>4 digit at minimum</font>";
}
if(is_numeric($email[0])){
$result="<img src='/img/xred.png'></img> <font color='red'>1st character must be letter</font>";
}
if($ce<>0){
//$result="<img src='/img/xred.png'></img> <font color='red'><strong>".$email."</strong> is taken</font>";
$result="kampret lu";
}
echo "
cekemailsignup=$cekemailsignup<br>
email=$email<br>
ce=$ce<br>
result=$result<br>
";
$ar = array(
'result' => $result,
'cekemailsignup' => $cekemailsignup,
'email' => $email
);
echo json_encode($ar);
}
?>
结果是字符串,要像对象一样使用它,需要将其解析为JSON
var obj = JSON.parse(result);
您还可以在$中设置数据类型:'json',
。ajax
配置选项默认设置它,然后您不需要解析响应,可以直接使用它
由于jQuery包含在页面中,所以将其用于DOM操作
完整代码:
$('#email').on('blur', function() {
var $status = $('#emailudahada');
email = $.trim($(this).val());
if (email) {
$(this).css('backgroundColor', 'yellow');
$status.html('<img src=\'/img/loading.GIF\'></img>');
$.ajax({
url: '/ztest2.php',
type: 'POST',
dataType: 'json',
data: ({
email: email,
cekemailsignup: 'yes'
}),
success: function(result) {
$status.html(result.result + ',' + result.cekemailsignup. + ',' + result.email);
}
});
} else {
$(this).css('backgroundColor', 'white');
}
});
$('#email')。在('blur',function()上{
var$status=$('emailudahada');
email=$.trim($(this.val());
如果(电子邮件){
$(this.css('backgroundColor','yellow');
$status.html(“”);
$.ajax({
url:“/ztest2.php”,
键入:“POST”,
数据类型:“json”,
数据:({
电邮:电邮,,
Cekemail注册:“是”
}),
成功:功能(结果){
$status.html(result.result+'、'+result.cekemailsignup.+'、'+result.email);
}
});
}否则{
$(this.css('backgroundColor','white');
}
});
这里更改了js函数
<script>
function checkemail() {
var status = document.getElementById("emailudahada");
var u = document.getElementById("email").value;
if (u != "") {
document.getElementById("email").style.backgroundColor = "yellow";
status.innerHTML = "<img src='/img/loading.GIF'></img>";
$.ajax({
url: '/ztest2.php',
type: "POST",
dataType: "json", //need to tell that response will as json
data: ({
email: u,
cekemailsignup: 'yes'
}),
success: function(result) {
status.innerHTML = result.result + "," + result.cekemailsignup. + "," + result.email;
}
});
} else {
document.getElementById("email").style.backgroundColor = "white";
}
}
</script>
函数checkemail(){
var status=document.getElementById(“emailudahada”);
var u=document.getElementById(“电子邮件”).value;
如果(u!=“”){
document.getElementById(“email”).style.backgroundColor=“黄色”;
status.innerHTML=“”;
$.ajax({
url:“/ztest2.php”,
类型:“POST”,
数据类型:“json”//需要告诉您响应将作为json
数据:({
电邮:u,,
Cekemail注册:“是”
}),
成功:功能(结果){
status.innerHTML=result.result+“,“+result.cekemailsignup.+”,“+result.email;
}
});
}否则{
document.getElementById(“email”).style.backgroundColor=“白色”;
}
}
HTML文件
函数checkemail(){
var status=document.getElementById(“emailudahada”);
var u=document.getElementById(“电子邮件”).value;
如果(u!=“”){
document.getElementById(“email”).style.backgroundColor=“黄色”;
status.innerHTML=“”;
$.ajax({
url:“/ztest2.php”,
类型:“POST”,
数据:({
电邮:u,,
Cekemail注册:“是”
}),
成功:
函数(结果2){
var result=JSON.parse(result2);
status.innerHTML=result['result']+”,“+result['cekemailsignup']+”,“+result['email'];
}
});
}否则{
document.getElementById(“email”).style.backgroundColor=“白色”;
}
}
echo“cekemailsignup=$cekemailsignup
email=$email
ce=$ce
result=$result
”代码>你为什么有这个?删除这个,再试一次是的@pekka是正确的。删除它仍然不起作用你是对的Pekka和Aldrin27,但不够完美,我运行你的建议,我删除一个回声,然后运行Karan和Tushar建议,现在它起作用了。谢谢大家,你们都是我最好的朋友。为什么我投了反对票?我只是被问到,不是垃圾邮件发送者。我运行你的建议,我删除了一个echo(Pekka和Aldrin27建议),它现在起作用了。谢谢你们每一个人,你们都是我最好的朋友。请再次帮助我在Hatur Nuhun kang Indra,tapi udah dijawab ama kang Karan。Tapi tenang,ane+1卡雷娜·塞班萨和塞塔纳空气:DKang@Indra,bantuin-lagi-dong-kang,trims-kang。请再帮我一次