Needing help with .php for contact form

Discussion in 'Software' started by ben1213, Mar 21, 2013.

  1. ben1213

    ben1213 Private E-2

    I am fairly new at html and am needing help with php for a contact form that I have made. I have made the html code for the form, but am unsure what the next step is. I do know that I would like for the fields with asterisks to be required fields, and that I would like the confirm email field to have to match identically to the email field. Also I would like for this form when submitted correctly< I would like for the user to be redirected to a custom url which is
    http://collinswindowcleaning.com/thank you.php.
    I have went ahead and placed the html code for the form below.Any and all help will be greatfully appreciated as to how to reconcile this.
    Thanks in advance for all help and suggestions.

    <form action="contactus.php" method="post" ><td valign="top" rowspan="1"align="left" class="auto-style2"><font color="#00001c"><strong>
    <span class="auto-style1">Contact Us</span></strong></font>
    <br /><br />
    <div><font color="#00001c" size="+0"><strong>Name</strong><span class="auto-style2">*</span></font></div>
    <input name="Name" type="text" size="30" tabindex="1" /> <div class="style1"><font color="#00001c" size="+0"><strong>Address</strong></font></div>
    <div><font color="#00001c" size="+0">Street Address</font></div>
    <input name="Street_Address" type="text" size="40" tabindex="3"/>
    <br />
    <div><font color="#00001c" size="+0">City</font></div>
    <input name="City" type="text" size="25" tabindex="4"/>
    <br />
    <div><font color="#00001c"size="+0">State&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Zip Code</font></div>
    <select name="State" tabindex="5">
    <option>Select One</option>
    <option>AL</option><option>AK</option><option>AR</option><option>AZ</option><option>CA</option><option>CO</option><option>CT</option><option>DE</option><option>FL</option><option>GA</option><option>HI</option><option>IA</option><option>ID</option><option>IL</option><option>IN</option><option>KS</option><option>KY</option><option>LA</option><option>MA</option><option>MD</option><option>ME</option><option>MI</option><option>MN</option><option>MO</option><option>MS</option><option>MT</option><option>NC</option><option>ND</option><option>NE</option><option>NH</option><option>NJ</option><option>NM</option><option>NV</option><option>NY</option><option>OH</option><option>OK</option><option>OR</option><option>PA</option><option>RI</option><option>SC</option><option>SD</option><option>TN</option><option>TX</option><option>UT</option><option>VA</option><option>VT</option><option>WA</option><option>WI</option><option>WV</option><option>WY</option>
    </select>
    <input name="Zip_Code" type="text" size="15" tabindex="6" class="auto-style3"/>
    <div><font color="#00001c" size="+0"><strong>Telephone Number</strong><span class="auto-style2">*</span></font></div>
    <input name="Telephone Number" type="text" size="15" tabindex="7" class="auto-style3"/>
    <div><font color="#00001c" size="+0"><strong>Email</strong><span class="auto-style2">*</span></font></div>
    <input name="Email" type="email" size="50" tabindex="10"/>
    <div><font color="#00001c" size="+0"><strong>Confirm Email</strong><span class="auto-style2">*</span></font></div>
    <input name="Confirm_Email" type="email" size="50" tabindex="11"/>
    <div class="style1"><font color="#00001c" size="+0"><strong>Request For</strong></font></div>
    <input name="Residential" type="radio" tabindex="12"/><font color="#00001c" size="+0">Residential</font>
    <div><input name="Commercial" type="radio" tabindex="13"/><font color="#00001c" size="+0">Commercial</font></div>
    <div class="style1"><font color="#00001c" size="+0"><strong>Request/Comments</strong><span class="auto-style2">*</span></font></div>
    <textarea name="Request/Comments" cols="40" rows="5" tabindex="14"></textarea>
    <div class="style1"><font color="#00001c" size="+0"><strong>How did you hear about us?</strong><span class="auto-style2">*</span></font></div>
    <select name="Referral" tabindex="15">
    <option>Please Select</option>
    <option>Internet Search Engine</option><option>Window Cleaner</option><option>CWC Representative Phone Call</option><option>CWC Representative On-Site Visit</option>
    <option>Social Media:Facebook</option><option>Referal From a Friend</option><option>Referal From a Business</option><option>Yellow Pages</option><option>Online Yellow Pages</option>
    <option>Existing Customer</option><option>Other</option>
    </select>
    <br />
    <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input name="Submit1" type="submit" value="Submit" /> </td></form>
     
  2. PC-XT

    PC-XT Master Sergeant

    Here's a few quick notes:

    You will want one of two results. Either present the form again, or redirect. I usually have the form page submit to itself, and redirect on success, or even put it all into one php script. All that is needed is for the code to decide which path to take.

    Since this is post data, you can use $_POST[$name] to reference form data. Here is some simple untested code for example:
    PHP:
    <?php
    //if the form is filled properly, redirect
    if (
    !empty(
    $_POST['Name'])
    &&
    $_POST['Email']==$_POST['Confirm_Email']
    &&
    validEmail($_POST['Email'])
    //&& other required fields and verifications
    ){
    //redirect to thanks page. One way is to simply include it without changing the url:
    include('thank you.php');exit;
    //or, you can redirect to the url, as long as you don't have any html or whitespace before the opening php tag:
    header('Location: http://collinswindowcleaning.com/thank%20you.php');exit;
    //Either way, exit to prevent the form from showing, if it's included below.
    }

    function 
    validEmail($email){
    //there are different ways to check emails, from matching a regex grossly simplified to /.+@.+\..+/ to actually checking if the email address will accept mail.
    //return false if email is found invalid, or true if it passes
    }

    ?><html><head><!--...--></head><body><!--...-->
    <form action="contactus.php" method="post" ><td valign="top" rowspan="1"align="left" class="auto-style2"><font color="#00001c"><strong>
    <span class="auto-style1">Contact Us</span></strong></font>
    <br /><br />
    <div><font color="#00001c" size="+0"><strong>Name</strong><span class="auto-style2">*</span></font><?php if(empty($_POST['Name']))echo('Your name is required.'); ?></div>
    <input name="Name" value="<?php echo($_POST['Name']); ?>" type="text" size="30" tabindex="1" /> <div class="style1"><font color="#00001c" size="+0"><strong>Address</strong></font></div>
    <!--form continues...-->
    I also added some code to this part of the form to refill the data already entered, and prompt for a required field. Most of the code I added should be highlighted. You can do this with JavaScript, instead, but I like to use both, in case someone disables scripting. If the form is in a separate file, you can probably still include it and add the extra php stuff in, but JavaScript checking before the form is submitted is often better.

    For the other required fields and verifications, you will probably need different kinds of checks for some fields, customized for your needs, such as making sure the state is one from a list or the phone number matches a regular expression... It might help to make functions for these like I showed for the email.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds