contact form help

Discussion in 'PHP' started by Funk-woo10, Feb 7, 2008.

  1. #1
    Hi all I am making up a bais contact form, however whe I submit the form I need the results to be displayed on the same page as the form.

    At the mo its takes me to another page. How so I get it to do that ?

    I assume its a if statement, where you "post to self" in the form action ?

    Here is my two bits of code - Thanks.

    Form:

    
    
    <form method="post" action="www.mysite.com/contact">
    <fieldset>
    <legend>Contact us</legend>
    <!-- DO NOT change ANY of the php sections -->
    <?php
    $ipi = getenv("REMOTE_ADDR");
    $httprefi = getenv ("HTTP_REFERER");
    $httpagenti = getenv ("HTTP_USER_AGENT");
    ?>
    <input type="hidden" name="ip" value="<?php echo $ipi ?>" />
    <input type="hidden" name="httpref" value="<?php echo $httprefi ?>" />
    <input type="hidden" name="httpagent" value="<?php echo $httpagenti ?>" />
    <b>Your Name:</b> <br /><input type="text" name="visitor" size="35" />
    <br /> <br />
    <b>Your Email:</b> <br /><input type="text" name="visitormail" size="35" />
    <br /> <br />
    <b>Subject:</b><br />
    <select name="attn" size="1" />
    <option value=" Advertising ">Advertising </option>
    <option value=" Press ">Press </option>
    <option value=" Report a bug ">Report a bug </option>
    <option value=" Account help ">Account help </option>
    <option value=" Other (explain below) ">Other (explain below) </option>
    </select>
    <br /><br />
    <b>Mail Message:</b>
    <br />
    <textarea name="notes" rows="4" cols="40"></textarea>
    <br /><br />
    <input type="submit" value="Send Mail" />
    <br />
    </fieldset>
    </form>
    
    HTML:
    php part

    
    
    <?php
    
    $ip = $_POST['ip'];
    $httpref = $_POST['httpref'];
    $httpagent = $_POST['httpagent'];
    $visitor = $_POST['visitor'];
    $visitormail = $_POST['visitormail'];
    $notes = $_POST['notes'];
    $attn = $_POST['attn'];
    
    
    if (eregi('http:', $notes)) {
    die ("Do NOT try that! ! ");
    }
    if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,".")))
    {
    echo "<h2>Use Back - Enter valid e-mail</h2>\n";
    $badinput = "<h2>Feedback was NOT submitted</h2>\n";
    echo $badinput;
    die ("Go back! ! ");
    }
    
    if(empty($visitor) || empty($visitormail) || empty($notes )) {
    echo "<h2>Use Back - fill in all fields</h2>\n";
    die ("Use back! ! ");
    }
    
    $todayis = date("l, F j, Y, g:i a") ;
    
    $attn = $attn ;
    $subject = $attn;
    
    $notes = stripcslashes($notes);
    
    $message = " $todayis [EST] \n
    Attention: $attn \n
    Message: $notes \n
    From: $visitor ($visitormail)\n
    Additional Info : IP = $ip \n
    Browser Info: $httpagent \n
    Referral : $httpref \n
    ";
    
    $from = "From: $visitormail\r\n";
    
    
    mail("email@hotmail.com", $subject, $message, $from);
    
    ?>
    
    
    
    
    <fieldset>
    <legend>Message sent</legend>
    <p>Thanks <?php echo $visitor ?> we will respond to the e-mail your provided ( <?php echo $visitormail ?> )</p>
    <p><b>Subject:</b> <?php echo $attn ?></p>
    <p><b>Message:</b>&nbsp;<?php $notesout = str_replace("\r", "<br/>", $notes); echo $notesout; ?></p>
    <p><b>Date sent:</b> <?php echo $todayis ?> <b> - from IP address :</b><?php echo $ip ?></p>
    
    <br /><br />
    <a href="***"> Next Page </a>
    </fieldset>
    
    
    PHP:
     
    Funk-woo10, Feb 7, 2008 IP
  2. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Change the 'action' parameter of the form. Put the name of the page there, so it'll call itself.
    Then you'll have to put the php part on top of the page and probably make some changes as to only show the form when it's needed.
     
    CreativeClans, Feb 7, 2008 IP
  3. walkere

    walkere Active Member

    Messages:
    112
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Aye.

    Place the processing PHP at the top of the page. Then wrap the whole thing in an if statement, like so...

    if ( isset($_POST['submit']) ) {
      //  Do form processing here
      $doOuput = true;
    }
    
    PHP:
    Then include the output on the form page, and wrap it in an if statement as well. You may want to set a flag at the end of the form processing, so that you know if you should do the regular output or print the form again to get valid input.

    Good luck,
    - Walkere
     
    walkere, Feb 7, 2008 IP
  4. mrmaypole

    mrmaypole Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you don't specify the action of the page it defaults to itself.
     
    mrmaypole, Feb 7, 2008 IP