提交联系表单时出错。
未定义的索引:第7行的C:\ xampp \ htdocs \ dishadwellings \ dishaparkwest \ contact.php中的HTTP_X_REQUESTED_WITH {"输入":"错误","文字":"抱歉请求必须是Ajax POST"}
这是HTML FORM代码:
<form action="contact.php" method="POST">
<input type="text" name="do-input-name" id="do-input-name" placeholder="Name">
<input type="email" name="do-input-email" id="do-input-email" placeholder="Email">
<input type="text" name="do-input-web" id="do-input-web" placeholder="Web">
<textarea name="do-input-message" id="do-input-message" cols="30" rows="10" class="do-input-message" placeholder="Comment"></textarea>
<button type="submit" id="do-submit-btn" class="do-btn-round-solid">SEND</button>
</form>
以下是联系表单的代码:我无法纠正错误。请帮忙解决这个问题
<?php
if($_POST)
{
$to_email = "abc@gmail.com"; //Recipient email, Replace with own email here
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
$output = json_encode(array( //create JSON data
'type'=>'error',
'text' => 'Sorry Request must be Ajax POST'
));
die($output); //exit script outputting json data
}
//Sanitize input data using PHP filter_var().
$name = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
$email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
$message = filter_var($_POST["message"], FILTER_SANITIZE_STRING);
//additional php validation
if(strlen($name)<4){ // If length is less than 4 it will output JSON error.
$output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
die($output);
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ //email validation
$output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!'));
die($output);
}
if(strlen($message)<3){ //check emtpy message
$output = json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.'));
die($output);
}
//email body
$message_body = $message."\r\n\r\n-".$name."\r\nEmail : ".$email;
//proceed with PHP email.
$headers = 'From: '.$name.'' . "\r\n" .
'Reply-To: '.$email.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$send_mail = mail($to_email, $subject, $message_body, $headers);
if(!$send_mail)
{
//If mail couldn't be sent output error. Check your PHP email configuration (if it ever happens)
$output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
die($output);
}else{
$output = json_encode(array('type'=>'message', 'text' => 'Hi '.$user_name .' Thank you for your email'));
die($output);
}
}
?>
答案 0 :(得分:0)
尝试if条件如下:
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || ( isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest')) {
$output = json_encode(array( //create JSON data
'type'=>'error',
'text' => 'Sorry Request must be Ajax POST'
));
die($output); //exit script outputting json data
}
接下来就是,您通过普通的HTML表单提交表单,并且您正在尝试检查提交数据或仅在其ajax提交时发送电子邮件。这不是真理,因此不起作用。
要使您的代码正常工作,请通过ajax提交表单数据或删除上面显示的条件。
您的邮政编码
<?php
if($_POST)
{
$to_email = "abc@gmail.com"; //Recipient email, Replace with own email here
//Sanitize input data using PHP filter_var().
$name = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
$email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
$message = filter_var($_POST["message"], FILTER_SANITIZE_STRING);
$subject = "Test email";
$user_name = "Sharnya";
//additional php validation
if(strlen($name)<4){ // If length is less than 4 it will output JSON error.
$output = 'Name is too short or empty!';
die($output);
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ //email validation
$output = 'Please enter a valid email!';
die($output);
}
if(strlen($message)<3){ //check emtpy message
$output = 'Too short message! Please enter something.';
die($output);
}
//email body
$message_body = $message."\r\n\r\n-".$name."\r\nEmail : ".$email;
//proceed with PHP email.
$headers = 'From: '.$name.'' . "\r\n" .
'Reply-To: '.$email.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$send_mail = mail($to_email, $subject, $message_body, $headers);
if(!$send_mail)
{
//If mail couldn't be sent output error. Check your PHP email configuration (if it ever happens)
$output = 'Could not send mail! Please check your PHP mail configuration.';
die($output);
}else{
$output = 'Hi '.$user_name .' Thank you for your email';
die($output);
}
}
?>
HTML表单代码:
<form action="contact.php" method="POST" enctype="text/plain">
<input type="text" name="name" id="name" placeholder="Name">
<input type="email" name="email" id="email" placeholder="Email">
<input type="text" name="web" id="web" placeholder="Web">
<textarea name="message" id="message" cols="30" rows="10" class="message" placeholder="Comment"></textarea>
<button type="submit" id="submit" class="do-btn-round-solid">SEND</button>
</form>
快乐的编码!