2013-07-22 82 views
-2

嗨即時聯繫我們表單而不刷新頁面。但是,當我點擊發送按鈕,即使字段爲空,我的必需屬性不起作用。請幫我必填屬性不起作用

contact.html上述

<!DOCTYPE html> 
<html> 
<head> 

<link rel="stylesheet" media="screen" href="styles.css" > 
<script src="http://code.jquery.com/jquery-latest.js"></script> 
<script> 
$(document).ready(function(){ 

$('#submit').click(function(){ 

$.post("send.php", $("#mycontactform").serialize(), function(response) { 
$('#success').html(response); 
//$('#success').hide('slow'); 
}); 
return false; 


}); 

}); 
</script> 
</head> 
<body> 

<form id="mycontactform" class="contact_form" action="" method="post" name="contact_form"> 
    <ul> 
     <li> 
      <h2>Contact Us</h2> 
      <span class="required_notification">* Denotes Required Field</span> 
     </li> 
     <li> 
      <label for="name">Name:</label> 
      <input type="text" id="name" name="name" placeholder="John Doe" required /> 
     </li> 
     <li> 
      <label for="email">Email:</label> 
      <input type="email" name="email" id="email" placeholder="[email protected]" required /> 
      <span class="form_hint">Proper format "[email protected]"</span> 
     </li> 

     <li> 
      <label for="message">Message:</label> 
      <textarea name="message" id="message" cols="40" rows="6" required ></textarea> 
     </li> 
<li> 
      <input type="button" class="submit" style="width:70px; text-align:center; height:30px; margin-left:200px; cursor:pointer" value="SEND" id="submit" /> 


     </li><div id="success" style="color:red;"></div> 
</form> 
</body> 

send.php

<?php 

// Here we get all the information from the fields sent over by the form. 
$name = $_POST['name']; 
$email = $_POST['email']; 
$message = $_POST['message']; 

    $to = '[email protected]'; 
    $subject = 'the subject'; 
    $message = 'FROM: '.$name.' Email: '.$email.'Message: '.$message; 
    $headers = 'From: [email protected]' . "\r\n"; 

if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // this line checks that we have a valid email address 
    mail($to, $subject, $message, $headers); //This method sends the mail. 
    echo "Your email was sent!"; // success message 
}else{ 
    echo "Invalid Email, please provide an correct email."; 
} 
?> 

是我的代碼。請幫幫我。我需要必要的屬性來點擊按鈕。

+0

你是什麼意思「所需」沒有工作..檢查請求是否被髮送 –

+0

你使用什麼瀏覽器?你確定它支持HTML5的'required'嗎? – Steve

+1

你使用什麼瀏覽器? 'required'是一個HTML5屬性,舊版瀏覽器可能不支持它。 – Barmar

回答

0

這只是我的看法,但我會使用PHP來檢查輸入字段是否爲空。

只要有一個if語句來查看他們是否填寫就行了。

<?php 

// Here we get all the information from the fields sent over by the form. 
$name = $_POST['name']; 
$email = $_POST['email']; 
$message = $_POST['message']; 

if($name=='' || $email=='' || $message=='') { //Make sure all of the fields are not empty 
echo "Please fill in all of the fields"; 
} 
else { //If they are filled in, process the rest of the form 

$to = '[email protected]'; 
$subject = 'the subject'; 
$message = 'FROM: '.$name.' Email: '.$email.'Message: '.$message; 
$headers = 'From: [email protected]' . "\r\n"; 

if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // this line checks that we have a valid email address 
    mail($to, $subject, $message, $headers); //This method sends the mail. 
    echo "Your email was sent!"; // success message 
}else{ 
    echo "Invalid Email, please provide an correct email."; 
} 
} 
?>