2014-09-07 1 views
-1

Voici le form.Validation de formulaire PHP - Ne pas voir le message d'erreur

Lorsque je clique sur Envoyer sans saisir les données requises, le message d'erreur n'apparaît pas, il saute simplement en haut de la page. Comment puis-je réparer cela?

Le code de validation PHP ajouté à la tête de la forme est:

if ($_POST['submit']) 
{ 

/* Check all form inputs and strip unnecessary characters from data */ 

$quotetype = $_POST['quotetype']; 
$name = check_input($_POST['name']); 
$phone = check_input($_POST['phone']); 
$email = check_input($_POST['email']); 
$zipcode = check_input($_POST['zipcode']); 
$appgender = $_POST['appgender']; 
$appdob = check_input($_POST['appdob']); 
$appsmoker = $_POST['appsmoker']; 
$spousedob = check_input($_POST['spousedob']); 
$spousesmoker = $_POST['spousesmoker']; 
$child1gender = check_input($_POST['child1gender']); 
$child1dob = check_input($_POST['child1dob']); 
$child2gender = check_input($_POST['child2gender']); 
$child2dob = check_input($_POST['child2dob']); 
$child3gender = check_input($_POST['child3gender']); 
$child3dob = check_input($_POST['child3dob']); 
$child4gender = check_input($_POST['child4gender']); 
$child4dob = check_input($_POST['child4dob']); 
$currentcarrier = check_input($_POST['currentcarrier']); 
$carriertype = $_POST['carriertype']; 
$coverage = $_POST['coverage']; 
$deductible = check_input($_POST['deductible']); 
$premium = check_input($_POST['premium']); 
$officecopay = check_input($_POST['officecopay']); 
$rxcopay = check_input($_POST['rxcopay']); 
$medconditions = $_POST['medconditions']; 
$coverageamount = $_POST['coverageamount']; 
$comments = $_POST['comments']; 

if (is_array($quotetype)) 
$displayquotetype = implode(",",$quotetype); 

$errorstring = ""; //default value of error string 

/* Check for required fields */ 

if (empty($quotetype)) 
$errorstring = $errorstring."*Quote Type "; 
if (!$name) 
$errorstring = $errorstring."*Name "; 
if (!$phone) 
$errorstring = $errorstring."*Phone "; 
if (!$email) 
$errorstring = $errorstring."*Email "; 
if (!$zipcode) 
$errorstring = $errorstring."*Zip Code "; 
if ($appgender=="choose") 
$errorstring = $errorstring."*Gender "; 
if ($appsmoker=="choose") 
$errorstring = $errorstring."*Smoker"; 

if ($errorstring =="") {  
$to = '[email protected]'; 

$subject = 'Quote Request';   
$message = " <html><body> </body></html>  "; 

$headers = "MIME-Version: 1.0" . "\r\n";  
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; 

mail($to,$subject,$message,$headers); 

header('location: messagesent.html'); 
exit(); 
} 

}  

/* Functions used */ 

function check_input($data)  
{  
$data = trim($data);   
$data = stripslashes($data);  
$data = htmlspecialchars($data);   
return $data; 
} 

formulaire dans index.php:

<form id="quote-form" name="quote-form" method="post" action="index.php"> 
... 
<?php 
if ($errorstring!="") 
{echo "<span class='errormessage'>Please fill out the following fields: $errorstring<br></span>";} 
?> 
... 
</form> 
+1

Votre bouton Envoyer ne pas le _NAME_ 'submit' (et ne fait aucun autre des éléments de formulaire), et par conséquent votre chèque par' si ($ _POST [ 'submit']) 'échoue. – CBroe

+0

@CBroe Merci beaucoup! Yay, ça marche maintenant. Question de suivi: Une fois soumis et il y a une erreur, comment puis-je l'obtenir pour revenir à l'ancre où le formulaire est au lieu du haut de la page? – isosmall

Répondre

0

Ajouter un attribut "nom" à votre entrée soumettre.

<input type="submit" class="rounded-button button-shadow" id="submit" value="Submit" name="submit" /> 
+0

Merci beaucoup! Cela l'a définitivement réglé. Savez-vous comment je peux l'obtenir pour revenir au formulaire s'il y a une erreur au lieu du haut de la page? – isosmall

+0

Bien sûr. Remplacez l'attribut d'action de formulaire par "index.php # quotes". –

+0

Vous êtes un épargnant de vie! Merci beaucoup!! – isosmall

Questions connexes