0

I have a form in HTML but when the submit button is pressed, the values typed in the form are not passed to the PHP script. I've been testing it with a basic regex check to test if an email is valid. I know that the emails I am typing work with the regex function since I've used it before. However, the test fails in PHP I am guessing because the variable is empty.

Form code:

<!-- Registration -->
<section id="registration">
  <div class="col-sm-6 col-sm-offset-3">
    <form class="animated pulse" id="register" action="/php/registration-form.php" method="POST">
      <p class="small">There are no refunds available under any circumstances.</p>
      <input id="register_first_name" type="text" placeholder="FIRST NAME">
      <input id="register_last_name" type="text" placeholder="LAST NAME">
      <input id="register_email" type="text" placeholder="E-MAIL">
      <input id="submit" type="submit" value="Checkout Now">
    </form>
  </div>
    </section>
<!-- End Registration -->

PHP Code:

    <?php
$first_name = $_POST["register_first_name"];
$last_name = $_POST["register_last_name"];
$email = $_POST["register_email"];

//Validate the attendee's email
if( validate_email($email) ) {

} else {
    ?>
    <script language="javascript" type="text/javascript">
        alert('Sorry! Please enter a valid email address.');
        window.location = '/';
    </script>
    <?php
    die("ERROR: Attendee entered an invalid email.");
}

function validate_email($email_to_check) {
    $regex = "^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-z]{2,4})$";
    return !empty($email_to_check) && preg_match($regex, $email_to_check);
} ?>

How should I be passing text to my PHP variables?

Thanks

mx0
  • 6,445
  • 12
  • 49
  • 54

2 Answers2

4

Name of the input fields should be the name by which you're trying to get them (here you have made them ids)

example

<input name="register_first_name" type="text" placeholder="FIRST NAME">
Pablo Escobar
  • 393
  • 2
  • 16
0

You have to use name attribute instead of id attribute just correct your form code convert id to name it will be work

Osama
  • 2,912
  • 1
  • 12
  • 15