gpt4 book ai didi

php - 如果条件错误,ajax 响应中的错误

转载 作者:搜寻专家 更新时间:2023-10-31 21:05:34 24 4
gpt4 key购买 nike

我想显示错误,即。电子邮件已存在于数据库中。如果没有显示感谢消息。

我为此使用了 ajax 和 jquery。这是我的代码:Ajax 查询:

<script type="text/javascript" >
$(function() {
var val_holder;
$("#subscribe").click(function() {
val_holder = 0;
var subscribe_mail = jQuery.trim($("#subscribe_mail").val()); // email field
var email_regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

if(subscribe_mail == "") {
$(".error_message").html("This field is empty.");
val_holder = 1;
}
if(subscribe_mail != "") {
if(!email_regex.test(subscribe_mail)){ // if invalid email
$(".error_message").html("Your email is invalid.");
val_holder = 1;
}
}
if(val_holder == 1) {
return false;
}
val_holder = 0;
var dataString = '&subscribe_mail=' + subscribe_mail;

$.ajax({
type: "POST",
url: "ajax_subscribe.php",
data: dataString,
datatype: "json",
success: function(response) {
console.log(response);
if(response!= '')
{
$('.error_message').html(subscribe_mail + ' is already Exists.');
return true;

} else {
$('.error_message').html('Thanks For subscription!');
return false;
}
}
});
$("#subscribe_form")[0].reset();
return false;
});
});

而php文件代码是:

<?php error_reporting('E_ALL ^ E_NOTICE'); include('config.php');if($_POST) {   $subscribe_mail = $_POST['subscribe_mail'];
$date = date('Y-m-d');

$q = mysql_query("select * from signups where signup_email_address='".$subscribe_mail."' ") or die(mysql_error());
$n = mysql_fetch_row($q);
if( $n>0 )
{
echo 'Already Exists';
}
else
{
$insert = mysql_query("insert into signups(signup_email_address,signup_date) values('$subscribe_mail','$date')") or die(mysql_error());

$from = 'sunshine.kapoor270@gmail.com';
$to = $subscribe_mail;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "From: " . $from . "\r\n"; // Sender's E-mail
$headers .= "Reply-to: " . $from . "\r\n";
$headers .= "Return-Path: $from\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$subject = "Thanks for Subscription";
$message = "Subscription email id : ".$subscribe_mail." ";

$sentmail = mail($to,$subject,$message,$headers);
$sentmail = mail($from,$subject,$message,$headers);
} } ?>

只有if(response!= ''){$('.error_message').html(subscribe_mail + '已经存在。');返回真; 这种情况正在起作用。如果响应为空,则不会有其他部分。

最佳答案

当用户不在数据库中时,这取决于您在响应中发回的内容。

您可以将条件更改为:

如果(!$.trim(响应)){......

如果响应为 null 或空。

关于php - 如果条件错误,ajax 响应中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32931533/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com