-1

I'm having a slight problem with a form,

I have made a form that inputs the customers name and school year and the PHP code behind it will mail the credentials to the employer, originally the form was connected to a 'submit' button, and it worked. Now I have to put a PayPal button in instead of a submit button, because this will be a way of ordering a voucher card.

And the PayPal button won't connect with the PHP code. Here is my code:

        <?php 
if (isset($_REQUEST['submit'])) {
// Initialize error array.
  $errors = array();
  // Check for a proper First name
  if (!empty($_REQUEST['firstname'])) {
  $firstname = $_REQUEST['firstname'];
  $pattern = "/^[a-zA-Z0-9\_]{2,20}/";// This is a regular expression that checks if the name is valid characters
  if (preg_match($pattern,$firstname)){ $firstname = $_REQUEST['firstname'];}
  else{ $errors[] = 'Your Name can only contain _, 1-9, A-Z or a-z 2-20 long.';}
  } else {$errors[] = 'You forgot to enter your First Name.';}
  
  // Check for a proper Last name
  if (!empty($_REQUEST['lastname'])) {
  $lastname = $_REQUEST['lastname'];
  $pattern = "/^[a-zA-Z0-9\_]{2,20}/";// This is a regular expression that checks if the name is valid characters
  if (preg_match($pattern,$lastname)){ $lastname = $_REQUEST['lastname'];}
  else{ $errors[] = 'Your Name can only contain _, 1-9, A-Z or a-z 2-20 long.';}
  } else {$errors[] = 'You forgot to enter your Last Name.';}
  
  //Check for a valid Email
  if (!empty($_REQUEST['Email'])) {
  $Email = $_REQUEST['Email'];
  $pattern = "/(.*?){1,10}/";
  if (preg_match($pattern,$Email)){ $Email = $_REQUEST['Email'];}
  else{ $errors[] = 'Your year can only be numbers and letters.';}
  } else {$errors[] = 'You forgot to enter your order.';}
  
  }


    //End of validation 
  if (isset($_REQUEST['submit'])) {
  if (empty($errors)) { 
  $from = "Order in from " . $firstname. ""; //Site name
  // Change this to your email address you want to form sent to
  $to = "scoildaracookthebooks@gmail.com"; 
  $subject = "Amanda! Someone bought a card!" . $firstname . "";
  
  $message = "Message from " . $firstname . " " . $lastname . " has just purchased a card, make sure to check the account before you give them a card!

  School year: " . $Email . " ";


  mail($to,$subject,$message,$from);
  }
}
?>
<?php 
  //Print Errors
  if (isset($_REQUEST['submit'])) {
  // Print any error messages. 
  if (!empty($errors)) { 
  echo '<hr /><h3>The following occurred:</h3><ul>'; 
  // Print each error. 
  foreach ($errors as $msg) { echo '<li>'. $msg . '</li>';}
  echo '</ul><h3>Your order could not be sent due to input errors.</h3><hr />';}
   else{echo '<hr /><h3 align="center">Your order was sent to Amanda. Thank you!</h3><hr /><p>Below is the contact details that you sent out to us.</p>'; 
  echo "<u>Order  €20 voucher from " . $firstname . " " . $lastname . " </u><br /><b>School year: </b> ".$Email."";
  }
  }
//End of errors array
  ?>

    </div>
    </div>

      </div>
        <div class="clearfix visible-lg"></div>
      </div>
      <div class="container">
        <div class="jumbotron">

    <h2>Order now!</h2>
  <p>Fill out the form below.</p>
  <div class ="form-group">
  <form role ="form" action="" method="post">
  <label>First Name: <br />
  <input name="firstname" type="text" class="form-control" placeholder="- Enter First Name -" /><br /></label>
</div>
   <div class ="form-group">
  <label>Last Name: <br />
  <input name="lastname" type="text" class="form-control" placeholder="- Enter Last Name -" /><br /></label>
  </div>
   <div class ="form-group">
  <label>School year: <br />
  <input name="Email" type="text" class="form-control" placeholder="- Enter School year -" /><br /></label>
</div>
  
   <div class ="form-group">

    <!-- This is where the problem starts, the name="submit" won't connect with the PHP code-->

<form action="https://www.paypal.com/cgi-bin/webscr" method="post"  target="_blank">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="HEVRWPVT75BKE">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" name="submit" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>

</div>
 </div>
  </form>
Conor Thompson
  • 198
  • 1
  • 15
  • You can't nest forms: http://stackoverflow.com/questions/379610/can-you-nest-html-forms – user4035 Feb 08 '15 at 22:59
  • what is the user flow that you want? –  Feb 08 '15 at 22:59
  • For one thing, your `$from` header is invalid. It'll Spam out. `mail()` requires a valid `From:` parameter being an email, not a person's name. – Funk Forty Niner Feb 08 '15 at 23:04
  • Well the `$from` works fine, and the mail is only going into one account, and the account is set up for the sole purpose of reading these mail forms that are sent from here, it's worked fine every other time – Conor Thompson Feb 09 '15 at 10:49

1 Answers1

0

Take a look at the PayPal Cart Upload method. You'll use that instead of the button/form that you're using now.

That method will allow you to build your own, single form, and you could combine whatever parameters of your own that you want plus the variables that PayPal provides (many as hidden fields).

Your form could submit to a "processor" script that does whatever you need to with your own data from the form, and then also takes the PayPal data to generate a URL string that you redirect the user to for the PayPal payment.

If you need to handle any post-transaction processing of your database, email notifications, etc. you'll want to do that with IPN.

Drew Angell
  • 25,968
  • 5
  • 32
  • 51