Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    Regular Coder student101's Avatar
    Join Date
    Nov 2007
    Posts
    634
    Thanks
    80
    Thanked 15 Times in 15 Posts

    Smile newsletter form better code review? please

    I'm trying to make this code more efficient, ideas are welcome.
    Thanks in advance!

    PHP Code:
    <?php
    if ((isset($_POST["brofrm"])) && ($_POST["brofrm"] == "form1")) {
        
    if (
    $_POST['thename'] == '' || $_POST['email'] == '' || $_POST['company'] == '' || $_POST['method'] == '' || $_POST['address'] == '' || $_POST['tel'] == '' || $_POST['brochure_type'] == '' ){

    // echo errors if above is true
    $mess .= "Please complete the form below, <strong>*</strong> important!<br>";

    if (empty(
    $_POST['thename'])) {$mess .="You did not supply a name.<br>"; }

    if (empty(
    $_POST['email'])) {$mess .="You did not supply an email address.<br>"; }

    if (empty(
    $_POST['method'])) {$mess .="You did not specify which format you would prefer to receive the brochure, we offer a physical paperback copy or an electronic copy. We need this to respond to your request.<br>"; }

    if (empty(
    $_POST['address'])) {$mess .="You did not supply an address. We need this to respond to your request. Please go back and complete the necessary field(s).<br>"; }

    if (empty(
    $_POST['tel'])) {$mess .="You did not supply an telephone number. We need this to respond to your request. Please go back and complete the necessary field(s).<br>"; }

    if (empty(
    $_POST['brochure_type'])) {$mess .="You did not specify which brochures you wanted to receive.<br>"; }

    }else{

    // add variables 
    if (isset($_POST['thename'])) {$name=$_POST['thename'];} 
    if (isset(
    $_POST['email'])) {$email=$_POST['email'];}
    if (isset(
    $_POST['company'])) {$company=$_POST['company'];}
    if (isset(
    $_POST['method'])) {$method=$_POST['method'];}
    if (isset(
    $_POST['address'])) {$address=$_POST['address'];}
    if (isset(
    $_POST['tel'])) {$tel=$_POST['tel'];}
    if (isset(
    $_POST['brochure_type'])) {$brochure_type=$_POST['brochure_type[]'];}

    // get checkbox items
    if (is_array($_POST['brochure_type'])) {
    $brochure_type implode(', '$_POST['brochure_type']);}

    // message
    $messge="<b><u>Name:</u></b> ".$_POST['thename']."<br /><b><u>Method:</u></b> ".$_POST['method']."<br /><b><u>Company:</u></b> ".$_POST['company']."<br /><b><u>Address:</u></b> ".$_POST['address']."<br /><b><u>Telephone No:</u></b> ".$_POST['tel']."<br /><b><u>Email Address:</u></b> ".$_POST['email']."<br /><b><u>Selected Brochures:</u></b> ".$brochure_type;

    ### 
    ## ADD EMAIL SCRIPT HERE
    ###

    echo $messge;
    echo 
    "<br>";
    echo 
    "Your message has been successfully sent. <a href=\"../../index.php\" title=\"Return to our homepage\">Return To Homepage</a><br />You will receive the brochure(s) you requested as soon as possible.</p>";
    $mess " ";
    }
    }
    ?>

    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    </head>

    <body>

    <p style="text-align:center;"><?php if(!empty($mess)){echo '<em><strong>'.$mess.'</em></strong>';?><br><a href="javascript:history.go(-1)">&laquo; Go Back</a></p>

    <?php }else{?>
    <form action="pagename.php" method="post" id="frmsubmit" name="frmsubmit">
    <table class="contacts" style="width:600px;">
    <tr>
    <td width="300"><p style="font-weight:bold;margin:5px;">*Name:</p></td>
    <td width="300"><input name='thename' type='text' id="thename" value="<?php echo((isset($_POST["thename"]))?trim(stripslashes($_POST["thename"])):""); ?>" size='40' /></td>
    </tr>
    <tr>
    <td width="300"><p style="font-weight:bold;margin:5px;">*Format:</p></td>
    <td width="300"><select name='method' id="method" style="width:267px;" title="<?php echo $_POST['method']; ?>">
    <option value='Hard Copy'>Hard Copy</option><option value='Electronic Copy'>Electronic Copy</option>
    </select></td>
    </tr>
    <tr>
    <td width="300"><p style="font-weight:bold;margin:5px;">Company:</p></td>
    <td width="300"><input name='company' type='text' id="company" value="<?php echo((isset($_POST["company"]))?trim(stripslashes($_POST["company"])):""); ?>" size='40' /></td>
    </tr>
    <tr>
    <td width="300" valign="top"><p style="font-weight:bold;margin:5px;">*Address:</p></td>
    <td width="300"><textarea name='address' cols='21' rows='5' id="address" style='resize:none;width:260px;'><?php echo((isset($_POST["address"]))?trim(stripslashes($_POST["address"])):""); ?></textarea></td>
    </tr>
    <tr>
    <td width="300"><p style="font-weight:bold;margin:5px;">*Telephone:</p></td>
    <td width="300"><input name='tel' type='text' id="tel" value="<?php echo((isset($_POST["tel"]))?trim(stripslashes($_POST["tel"])):""); ?>" size='40' /></td>
    </tr>
    <tr>
    <td width="300"><p style="font-weight:bold;margin:5px;">*Email:</p></td>
    <td width="300"><input name='email' type='text' id="email" value="<?php echo((isset($_POST["email"]))?trim(stripslashes($_POST["email"])):""); ?>" size='40' /></td>
    </tr>
    <tr>
    <td><p style="font-weight:bold;margin:5px;">*Brochures:</p></td>
    <td><p>Please select which brochure(s) you would like a copy of:<br /><br /><input <?php if (!(strcmp($_POST['brochure_type[]'],"1"))) {echo "checked=\"checked\"";} ?> name='brochure_type[]' type='checkbox' id="brochure_type[]" value='Brochure 1' /> Brochure 1<br />
    <input <?php if (!(strcmp($_POST['brochure_type[]'],"1"))) {echo "checked=\"checked\"";} ?> name='brochure_type[]' type='checkbox' id="brochure_type[]" value='Brochure 2' /> Brochure 2</p></td>
    </tr>
    <tr>
    <td colspan="2">
    <input name="brofrm" type="hidden" id="brofrm" value="form1">
    </td>
    </tr>
    <tr>
    <td></td>
    <td><input type='submit' value='Submit' /><input type='reset' value='Clear' /></td>
    </tr>
    </table>
    </form>
    </body><?php }?>
    </body>
    </html>
    Last edited by student101; 10-20-2013 at 08:06 PM. Reason: I'm trying to make this code more efficient, ideas are welcome.
    Thanks for your support!
    Update MySQL with checkboxes | Tell A Friend | Delete MySQL with checkboxes

    Give thanks & resolve when done :thumbsup:


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •