2016-07-30 7 views
0

Je suis un novice complet quand il s'agit de PHP. J'ai téléchargé un formulaire que j'ai trouvé en ligne à inclure dans mon site Bootstrap. C'est un formulaire très simple que n'importe qui peut utiliser pour m'envoyer un message. J'ai réussi à configurer Wamp pour tester le PHP mais quand je laisse le champ Téléphone vide, il me donne un message d'erreur me disant de revenir en arrière et de corriger l'erreur. Je veux faire en sorte que si quelqu'un laisse le numéro de téléphone, il envoie toujours le courriel. Je vous remercie.Modifier un champ sous forme PHP de obligatoire à facultatif

HTML

<form name="contactform" method="post" action="index.php" class="form-vertical"> 
     <div class="form-group"> 
      <label for="inputName" class="control-label">Name</label> 
       <input type="text" class="form-control" id="inputName" name="inputName" placeholder="First and Last"> 
     </div> 
     <div class="form-group"> 
      <label for="inputEmail" class="control-label">Email*</label> 
      <input type="text" class="form-control" id="inputEmail" name="inputEmail" placeholder="Required"> 
     </div> 
     <div class="form-group"> 
     <label for="inputPhone" class="control-label">Phone Number</label> 
     <input type="text" class="form-control" id="inputPhone" name="inputPhone" placeholder="Optional"> 
     </div> 
     <div class="form-group"> 
     <label for="inputMessage" class="control-label">Message</label> 
     <textarea class="form-control" rows="5" id="inputMessage" name="inputMessage" placeholder="Brief Description"></textarea> 
     </div> 
     <div class="form-group"> 
     <button type="submit" class="btn btn-custom pull-right hvr-underline-from-left">Send</button> 
     </div> 
    </form> 

PHP

<?php 
    /* Set e-mail recipient */ 
    $myemail = "[email protected]"; 

    /* Check all form inputs using check_input function */ 
    $name = check_input($_POST['inputName'], "First and Last"); 
    $email = check_input($_POST['inputEmail'], "Required"); 
    $phone = check_input($_POST['inputPhone'], "Optional"); 
    $message = check_input($_POST['inputMessage'], "Brief Description"); 

    /* If e-mail is not valid show error message */ 
    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) { 
     show_error("Invalid e-mail address"); 
    } 
    /* Let's prepare the message for the e-mail */ 

    $subject = "Contact Message from mywebsite.net"; 

    $message = " 

    Someone has sent you a message using your contact form: 

    Name: $name 
    Email: $email 
    Phone: $phone 

    Message: 
    $message 

    "; 

    /* Send the message using mail() function */ 
    mail($myemail, $subject, $message); 

    /* Redirect visitor to the thank you page */ 
    header('Location:contact.html'); 
    exit(); 

    /* Functions we used */ 
    function check_input($data, $problem='') 
    { 
     $data = trim($data); 
     $data = stripslashes($data); 
     $data = htmlspecialchars($data); 
     if ($problem && strlen($data) == 0) 
     { 
      show_error($problem); 
     } 
     return $data; 
    } 

    function show_error($myError) 
    { 
?> 
<html> 
<body> 

<p>Please correct the following error:</p> 
<strong><?php echo $myError; ?></strong> 
<p>Hit the back button and try again</p> 

</body> 
</html> 
<?php 
     exit(); 
    } 
?> 
+0

Pourriez-vous s'il vous plaît poster l'erreur? –

+0

Veuillez corriger l'erreur suivante: Facultatif Appuyez sur le bouton Précédent et réessayez – user3786102

Répondre

1

S'il vous plaît modifier cette ligne:

$phone = check_input($_POST['inputPhone'], "Optional"); 

à:

$phone = check_input($_POST['inputPhone']); 

De cette façon, show_error($problem); ne sera pas appelé.

0

Parce que la fonction check_input inclut la fonction show_error.

Et la fonction ont exit() en cas d'erreur. Donc, l'entrée de votre téléphone == NULL => alors error et appelez exit().

solution pour ce cas

Ne pas vérifier l'exigent, avec l'entrée du numéro de téléphone.

0

PHP

<?php 
function show_error($myError) 
{ 
?> 
<html> 
<body> 

<p>Please correct the following error:</p> 
<strong><?php echo $myError; ?></strong> 
<p>Hit the back button and try again</p> 

</body> 
</html> 
<?php 
exit(); 
} 


/* Functions we used */ 
function check_input($data, $problem='') 
{ 
$data = trim($data); 
$data = stripslashes($data); 
$data = htmlspecialchars($data); 
if ($problem && strlen($data) == 0) 
{ 
show_error($problem); 
} 
return $data; 
} 



/* Set e-mail recipient */ 
$myemail = "[email protected]"; 

/* Check all form inputs using check_input function */ 
$name = check_input($_POST['inputName'], "First and Last"); 
$email = check_input($_POST['inputEmail'], "Required"); 
$phone = $_POST['inputPhone']; 
$message = check_input($_POST['inputMessage'], "Brief Description"); 

/* If e-mail is not valid show error message */ 
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) 
{ 
show_error("Invalid e-mail address"); 
} 
/* Let's prepare the message for the e-mail */ 

$subject = "Contact Message from mywebsite.net"; 

$message = " 

Someone has sent you a message using your contact form: 

Name: $name 
Email: $email 
Phone: $phone 

Message: 
$message 

"; 

/* Send the message using mail() function */ 
mail($myemail, $subject, $message); 

/* Redirect visitor to the thank you page */ 
header('Location:contact.html'); 
exit(); 


?> 
0

Vous êtes face à cette erreur, car vous validez le numéro de téléphone-à-dire $_POST['inputPhone'] en utilisant la fonction check_input dans la ligne

$phone = check_input($_POST['inputPhone'], "Optional"); 

Vous pouvez éviter cette erreur de multiples façons:

$phone = $_POST['inputPhone']; //not secure 

OU

$phone = check_input($_POST['inputPhone'], ""); 

OU

$phone = filter_var($_POST['inputPhone'],FILTER_SANITIZE_STRIPPED);