PHP: Simple Form

  • Johan007
  • Guru
  • Guru
  • User avatar
  • Posts: 1080
  • Loc: Aldershot, UK

Post 3+ Months Ago

For this school I have a email form done and works.

However if something does not meet the validation all the information is lost! How can I keep the information?

Code: [ Select ]
<html>
<head>
<title>Web Design Quote</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<LINK href="css/standard.css" rel="stylesheet">
</head>
<body bgcolor="#FFFFFF" text="#000000">

<?php
$error_message="All fields must be completed";
?>

<?php

if ($submit) { // if the submit button was pressed

    if($name && $product ) { // if all fields were completed

        // check if email syntax is valid
        if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email) )
        {

         echo "<b>The e-mail was not valid</b>";

        }

        else {

            mail("info@*****.com","order","$product + $message + $phone + $address + $postcode + $fax","From: $name <$email>") or die("email error");


            echo "<b>Your message has been sent</b>";  // if all went well, display message was sent

        } // end of is email valid or not



    } // end of if all fields were completed

else {

echo "$error_message<BR>"; // if not all were filled in, display error message
}


} // end php if statement

?>
<form action="" method="post">
 <p align="left">
  <select name="product">
   <option selected>Please Select</option>
   <option>EAAS, full version £475.00</option>
   <option>EAAS, Human Resources £250.00</option>
   <option>EAAS, Budget module £250.00</option>
   <option>EAAS, Asset Register £100.00</option>
   <option>EAAS, Demo CD</option>
  </select>
  <br>
  Your name: <br>
  <input type="text" name="name">
  <br>
  Your email: <br>
  <input type="text" name="email">
  <br>
  Phone number<br>
  <input type="text" name="phone">
  <br>
  Fax number<br>
  <input type="text" name="fax">
  <br>
  School name<br>
  <input type="text" name="school">
  <br>
  Address<br>
  <textarea name="address" cols="50"></textarea>
  <br>
  Postcode<br>
  <input type="text" name="postcode">
  <br>
  Additional <br>
  <textarea name="message" cols="50" rows="7"></textarea>
  <br>
  <input type="submit" name="submit" value="Send">
  <input type="reset" name="Reset" value="Reset">
 </p>
 
</form>
</body>
</html>
  1. <html>
  2. <head>
  3. <title>Web Design Quote</title>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  5. <LINK href="css/standard.css" rel="stylesheet">
  6. </head>
  7. <body bgcolor="#FFFFFF" text="#000000">
  8. <?php
  9. $error_message="All fields must be completed";
  10. ?>
  11. <?php
  12. if ($submit) { // if the submit button was pressed
  13.     if($name && $product ) { // if all fields were completed
  14.         // check if email syntax is valid
  15.         if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email) )
  16.         {
  17.          echo "<b>The e-mail was not valid</b>";
  18.         }
  19.         else {
  20.             mail("info@*****.com","order","$product + $message + $phone + $address + $postcode + $fax","From: $name <$email>") or die("email error");
  21.             echo "<b>Your message has been sent</b>";  // if all went well, display message was sent
  22.         } // end of is email valid or not
  23.     } // end of if all fields were completed
  24. else {
  25. echo "$error_message<BR>"; // if not all were filled in, display error message
  26. }
  27. } // end php if statement
  28. ?>
  29. <form action="" method="post">
  30.  <p align="left">
  31.   <select name="product">
  32.    <option selected>Please Select</option>
  33.    <option>EAAS, full version £475.00</option>
  34.    <option>EAAS, Human Resources £250.00</option>
  35.    <option>EAAS, Budget module £250.00</option>
  36.    <option>EAAS, Asset Register £100.00</option>
  37.    <option>EAAS, Demo CD</option>
  38.   </select>
  39.   <br>
  40.   Your name: <br>
  41.   <input type="text" name="name">
  42.   <br>
  43.   Your email: <br>
  44.   <input type="text" name="email">
  45.   <br>
  46.   Phone number<br>
  47.   <input type="text" name="phone">
  48.   <br>
  49.   Fax number<br>
  50.   <input type="text" name="fax">
  51.   <br>
  52.   School name<br>
  53.   <input type="text" name="school">
  54.   <br>
  55.   Address<br>
  56.   <textarea name="address" cols="50"></textarea>
  57.   <br>
  58.   Postcode<br>
  59.   <input type="text" name="postcode">
  60.   <br>
  61.   Additional <br>
  62.   <textarea name="message" cols="50" rows="7"></textarea>
  63.   <br>
  64.   <input type="submit" name="submit" value="Send">
  65.   <input type="reset" name="Reset" value="Reset">
  66.  </p>
  67.  
  68. </form>
  69. </body>
  70. </html>
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Scorpius
  • Proficient
  • Proficient
  • User avatar
  • Posts: 401
  • Loc: Scorpion Hole

Post 3+ Months Ago

What do you mean by keep it?
  • _Leo_
  • Proficient
  • Proficient
  • User avatar
  • Posts: 279
  • Loc: Buenos Aires, Argentina

Post 3+ Months Ago

Like this:

Code: [ Select ]
<html>
<head>
<title>Web Design Quote</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<LINK href="css/standard.css" rel="stylesheet">
</head>
<body bgcolor="#FFFFFF" text="#000000">

<?php
$error_message="All fields must be completed";
?>

<?php

if ($submit) { // if the submit button was pressed

  if($name && $product ) { // if all fields were completed

   // check if email syntax is valid
   if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email) )
   {

    echo "<b>The e-mail was not valid</b>";

   }

   else {

     mail("no_spam@dot.com","order","$product + $message + $phone + $address + $postcode + $fax","From: $name <$email>") or die("email error");


     echo "<b>Your message has been sent</b>";  // if all went well, display message was sent

   } // end of is email valid or not



  } // end of if all fields were completed

else {

echo "$error_message<BR>"; // if not all were filled in, display error message
}


} // end php if statement

?>
<form action="" method="post">
 <p align="left">
  <select name="product">
   <option>Please Select</option>
   <option value="1" <?=($product==1)?"SELECTED":""?>>EAAS, full version £475.00</option>
   <option value="2" <?=($product==2)?"SELECTED":""?>>EAAS, Human Resources £250.00</option>
   <option value="3" <?=($product==3)?"SELECTED":""?>>EAAS, Budget module £250.00</option>
   <option value="4" <?=($product==4)?"SELECTED":""?>>EAAS, Asset Register £100.00</option>
   <option value="5" <?=($product==5)?"SELECTED":""?>>EAAS, Demo CD</option>
  </select>
  <br>
  Your name: <br>
  <input type="text" name="name" value="<?=$name?>">
  <br>
  Your email: <br>
  <input type="text" name="email" value="<?=$email?>">
  <br>
  Phone number<br>
  <input type="text" name="phone" value="<?=$phone?>">
  <br>
  Fax number<br>
  <input type="text" name="fax" value="<?=$fax?>">
  <br>
  School name<br>
  <input type="text" name="school" value="<?=$school?>">
  <br>
  Address<br>
  <textarea name="address" cols="50"><?=$address?></textarea>
  <br>
  Postcode<br>
  <input type="text" name="postcode" value="<?=$postcode?>">
  <br>
  Additional <br>
  <textarea name="message" cols="50" rows="7"><?=$message?></textarea>
  <br>
  <input type="submit" name="submit" value="Send">
  <input type="reset" name="Reset" value="Reset">
 </p>

</form>
</body>
</html>
  1. <html>
  2. <head>
  3. <title>Web Design Quote</title>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  5. <LINK href="css/standard.css" rel="stylesheet">
  6. </head>
  7. <body bgcolor="#FFFFFF" text="#000000">
  8. <?php
  9. $error_message="All fields must be completed";
  10. ?>
  11. <?php
  12. if ($submit) { // if the submit button was pressed
  13.   if($name && $product ) { // if all fields were completed
  14.    // check if email syntax is valid
  15.    if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email) )
  16.    {
  17.     echo "<b>The e-mail was not valid</b>";
  18.    }
  19.    else {
  20.      mail("no_spam@dot.com","order","$product + $message + $phone + $address + $postcode + $fax","From: $name <$email>") or die("email error");
  21.      echo "<b>Your message has been sent</b>";  // if all went well, display message was sent
  22.    } // end of is email valid or not
  23.   } // end of if all fields were completed
  24. else {
  25. echo "$error_message<BR>"; // if not all were filled in, display error message
  26. }
  27. } // end php if statement
  28. ?>
  29. <form action="" method="post">
  30.  <p align="left">
  31.   <select name="product">
  32.    <option>Please Select</option>
  33.    <option value="1" <?=($product==1)?"SELECTED":""?>>EAAS, full version £475.00</option>
  34.    <option value="2" <?=($product==2)?"SELECTED":""?>>EAAS, Human Resources £250.00</option>
  35.    <option value="3" <?=($product==3)?"SELECTED":""?>>EAAS, Budget module £250.00</option>
  36.    <option value="4" <?=($product==4)?"SELECTED":""?>>EAAS, Asset Register £100.00</option>
  37.    <option value="5" <?=($product==5)?"SELECTED":""?>>EAAS, Demo CD</option>
  38.   </select>
  39.   <br>
  40.   Your name: <br>
  41.   <input type="text" name="name" value="<?=$name?>">
  42.   <br>
  43.   Your email: <br>
  44.   <input type="text" name="email" value="<?=$email?>">
  45.   <br>
  46.   Phone number<br>
  47.   <input type="text" name="phone" value="<?=$phone?>">
  48.   <br>
  49.   Fax number<br>
  50.   <input type="text" name="fax" value="<?=$fax?>">
  51.   <br>
  52.   School name<br>
  53.   <input type="text" name="school" value="<?=$school?>">
  54.   <br>
  55.   Address<br>
  56.   <textarea name="address" cols="50"><?=$address?></textarea>
  57.   <br>
  58.   Postcode<br>
  59.   <input type="text" name="postcode" value="<?=$postcode?>">
  60.   <br>
  61.   Additional <br>
  62.   <textarea name="message" cols="50" rows="7"><?=$message?></textarea>
  63.   <br>
  64.   <input type="submit" name="submit" value="Send">
  65.   <input type="reset" name="Reset" value="Reset">
  66.  </p>
  67. </form>
  68. </body>
  69. </html>


You will have to add an array for the product name since I changed it for a number from 1 to 5.
  • Johan007
  • Guru
  • Guru
  • User avatar
  • Posts: 1080
  • Loc: Aldershot, UK

Post 3+ Months Ago

wow thanks for the speedy reply _Leo_.

And could you just do one thing for me and take off my email from your post cos I forgot about those sneeky spiders.

cheers again
  • _Leo_
  • Proficient
  • Proficient
  • User avatar
  • Posts: 279
  • Loc: Buenos Aires, Argentina

Post 3+ Months Ago

No problem :)

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 66 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.