user3786102
user3786102

Reputation: 145

Change a field in PHP form from required to optional

I am a complete novice when it comes to PHP. I downloaded a form I found online to include in my Bootstrap site. It is a very simple form anyone can use to send me a message. I managed to set up Wamp to test out the PHP but when I leave the Phone field blank it gives me an error message telling me please go back and correct the error. I want to make it so if someone leaves out the phone number it still sends the email. Thank you.

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();
    }
?>

Upvotes: 0

Views: 2082

Answers (4)

jonju
jonju

Reputation: 2736

You are facing this error because you are validating the phone number i.e $_POST['inputPhone'] using the check_input function in the line

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

You can avoid this error in multiple ways:

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

OR

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

OR

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

Upvotes: 0

metalbox
metalbox

Reputation: 302

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();


?>

Upvotes: 0

Quynh Nguyen
Quynh Nguyen

Reputation: 3009

Because function check_input include function show_error.

And function show_error have exit() when have errors.

So, your phone input == NULL => so have error and call to exit().

Solution for this case

Don't check require with phone number input.

Upvotes: 0

Ismail RBOUH
Ismail RBOUH

Reputation: 10460

Please change this line:

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

to:

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

This way show_error($problem); won't be called.

Upvotes: 1

Related Questions