使用各种教程,即here和here,我设法将以下PHP脚本放在一起,该脚本对提交的表单执行服务器端验证。(我已经有处理“客户端”验证的脚本。
<?php
//email signup ajax call
if($_GET['action'] == 'signup'){
//sanitize data
$email = mysql_real_escape_string($_POST['signup-email']);
//validate email address - check if input was empty
if(empty($email)){
$status = "error";
$message = "You did not enter an email address!";
}
else if(!preg_match('/^[^\W][a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\@[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\.[a-zA-Z]{2,4}$/', $email)){ //validate email address - check if is a valid email address
$status = "error";
$message = "You have entered an invalid email address!";
}
else {
$insertSignup = mysql_query("INSERT INTO signups (signup_email_address) VALUES ('$email')");
if($insertSignup){ //if insert is successful
$status = "success";
$message = "You have been signed up!";
}
else { //if insert fails
$status = "error";
$message = "Ooops, Theres been a technical error!";
}
}
//return json response
$data = array(
'status' => $status,
'message' => $message
);
echo json_encode($data);
exit;
}
?>
我现在要做的是添加另一个字段,在本例中是“名称”,我也想验证它。
我遇到的问题是我不确定如何在上面的代码中添加另一个字段。同样,我一直在尝试找到一个可以用来学习的示例,但我还没有找到任何可以使用的示例。
我只是想知道是否有人可以看看这个,也许会指出我正确的方向。
非常感谢和亲切的问候