PHP CODE TO SUBMIT FORM INPUT TO EMAIL

  • stevewise
  • Newbie
  • Newbie
  • stevewise
  • Posts: 7

Post 3+ Months Ago

please THIS CODE IS WORKING FOR ME VERY WELL, BUT I AM HAVING PROBLEM IN THIS REGARD. WHEN USERS SUBMIT AN EMPTY FIELDIT WILL ACCEPT IT, I NEED A CODE THAT WILL CONSTRAINT USERS FROM SUBMITTING AN EMPTY FIELD.

Code: [ Select ]
<form id="form1" name="form1" method="post" action="feedback.php" >
        <table width="402" height="105" border="1">
         <tr>
            <td width="123"><span class="style4">Surname</span></td>
            <td width="263"><label>
             <input name="Surname" type="text" id="Surname" size="30" />
            </label></td>
         </tr>
         <tr>
            <td><span class="style4">Name</span></td>
            <td><label>
             <input name="Name" type="text" id="Name" size="30" />
            </label></td>
         </tr>
         <tr>
            <td><span class="style4">Product</span></td>
            <td><label>
             <textarea name="Product" id="Product" rows="3" cols="20"></textarea>
            </label></td>
         </tr>
         <tr>
            <td><span class="style4">Email</span></td>
            <td><label>
             <input name="email" type="text" id="email" size="30" />
            </label></td>
         </tr>
         <tr>
            <td><span class="style4">Address</span></td>
            <td><label>
             <textarea name="address" id="address" rows="3" cols="20"></textarea>
            </label></td>
         </tr>
         <tr>
            <td><span class="style4">Phone No</span></td>
            <td><label>
             <input name="phoneNo" type="text" id="phoneNo" size="20" />
            </label></td>
         </tr>
        </table>
        <p>
         <label>
         <input type="submit" name="Submit" value="Submit" />
         </label>
         <label>
         <input name="Reset" type="reset" id="Reset" value="Reset" />
         </label>
        </p>
     </form>
  1. <form id="form1" name="form1" method="post" action="feedback.php" >
  2.         <table width="402" height="105" border="1">
  3.          <tr>
  4.             <td width="123"><span class="style4">Surname</span></td>
  5.             <td width="263"><label>
  6.              <input name="Surname" type="text" id="Surname" size="30" />
  7.             </label></td>
  8.          </tr>
  9.          <tr>
  10.             <td><span class="style4">Name</span></td>
  11.             <td><label>
  12.              <input name="Name" type="text" id="Name" size="30" />
  13.             </label></td>
  14.          </tr>
  15.          <tr>
  16.             <td><span class="style4">Product</span></td>
  17.             <td><label>
  18.              <textarea name="Product" id="Product" rows="3" cols="20"></textarea>
  19.             </label></td>
  20.          </tr>
  21.          <tr>
  22.             <td><span class="style4">Email</span></td>
  23.             <td><label>
  24.              <input name="email" type="text" id="email" size="30" />
  25.             </label></td>
  26.          </tr>
  27.          <tr>
  28.             <td><span class="style4">Address</span></td>
  29.             <td><label>
  30.              <textarea name="address" id="address" rows="3" cols="20"></textarea>
  31.             </label></td>
  32.          </tr>
  33.          <tr>
  34.             <td><span class="style4">Phone No</span></td>
  35.             <td><label>
  36.              <input name="phoneNo" type="text" id="phoneNo" size="20" />
  37.             </label></td>
  38.          </tr>
  39.         </table>
  40.         <p>
  41.          <label>
  42.          <input type="submit" name="Submit" value="Submit" />
  43.          </label>
  44.          <label>
  45.          <input name="Reset" type="reset" id="Reset" value="Reset" />
  46.          </label>
  47.         </p>
  48.      </form>

THIS CODE IS THE FORM CODE FOR INPUT DATA

<?php
$ip = getenv("REMOTE_ADDR");
$message .= "Name: ".$_POST['name']."\n";
$message .= "Surname: ".$_POST['surname']."\n";
$message .= "product: ".$_POST['product']."\n";
$message .= "Email".$_POST['Email']."\n";
$message .= "phone No: ".$_POST['phoneNo']."\n";
$message .= "Address: ".$_POST['Address']."\n";
$message .= "User IP: ".$ip."\n";

$message .= "------------------------------------------------------------------\n";

$send = "info@kbashreefers.org";
$subject = "Customer Feedback";
$headers = "From: no-reply@kbashreefers.org";
$headers .= $_POST['eMailAdd']."\n";
$headers .= "MIME-Version: 1.0\n";

mail($send,$subject,$message, $headers);

header("Location: success.html");

THIS IS THE CODE THAT PROCESSESS THE FORM.
please I NEED A CODE TO CONSTRAINT MY FIELDS. ANY ONE WITH A POSSIBLE SOLUTION PLEASE HELP OUT.
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

The easiest way (IMHO) would be to use JavaScript on your form page to validate the field values before submittal, which in turn means changing your submittal button to an input type="button" with an onClick calling this function. Here's some JS to go by - this is based on one of my own forms, so you'll have to modify as needed. I'm here to teach, not freelance ;)

Code: [ Select ]
 
<script language="JavaScript">
   
    function checkField(val,type) {
        var filter = "";
        if(type == "zip")
            filter  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
        else if(type == "email")
            filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        else if(type == "phone")
            filter  = /^\d{3}-\d{3}\-\d{4}$/;
        if (filter.test(val))
            return true;
        return false;
    }
   
    function validatePage() {
        var elems = document.forms[0].elements;
        for(x=0; x<elems.length; x++) {
            if(elems[x].type in {'text':'', 'textarea':'', 'select-one':''}) {
                if(Trim(elems[x].value) == "" || Trim(elems[x].value) == null) {
                    alert("Please fill in the " + elems[x].name + ".");
                    return false;
                }
                else if(elems[x].name == "zip" && !checkField(elems[x].value,"zip")) {
                    alert("The zip code is invalid");
                    return false;
                }
                else if(elems[x].name == "email" && !checkField(elems[x].value,"email")) {
                    alert("The email address is invalid");
                    return false;
                }
                else if(elems[x].name == "phone" && !checkField(elems[x].value,"phone")) {
                    alert("The phone number is invalid");
                    return false;
                }
            }
        }
        return true;
    }
 
</script>
 
 
 
  1.  
  2. <script language="JavaScript">
  3.    
  4.     function checkField(val,type) {
  5.         var filter = "";
  6.         if(type == "zip")
  7.             filter  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
  8.         else if(type == "email")
  9.             filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  10.         else if(type == "phone")
  11.             filter  = /^\d{3}-\d{3}\-\d{4}$/;
  12.         if (filter.test(val))
  13.             return true;
  14.         return false;
  15.     }
  16.    
  17.     function validatePage() {
  18.         var elems = document.forms[0].elements;
  19.         for(x=0; x<elems.length; x++) {
  20.             if(elems[x].type in {'text':'', 'textarea':'', 'select-one':''}) {
  21.                 if(Trim(elems[x].value) == "" || Trim(elems[x].value) == null) {
  22.                     alert("Please fill in the " + elems[x].name + ".");
  23.                     return false;
  24.                 }
  25.                 else if(elems[x].name == "zip" && !checkField(elems[x].value,"zip")) {
  26.                     alert("The zip code is invalid");
  27.                     return false;
  28.                 }
  29.                 else if(elems[x].name == "email" && !checkField(elems[x].value,"email")) {
  30.                     alert("The email address is invalid");
  31.                     return false;
  32.                 }
  33.                 else if(elems[x].name == "phone" && !checkField(elems[x].value,"phone")) {
  34.                     alert("The phone number is invalid");
  35.                     return false;
  36.                 }
  37.             }
  38.         }
  39.         return true;
  40.     }
  41.  
  42. </script>
  43.  
  44.  
  45.  
  • AaliyahRoma
  • Born
  • Born
  • AaliyahRoma
  • Posts: 4

Post 3+ Months Ago

Check code of this free php contact form. There is also an empty-value check there.

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 102 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.