SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2004
    Location
    Europe
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Beginner 'form' question

    Got the code to send a form below & it works fine but instead of printing 'Message sent' & 'All fields must be filled in' on the same page, how to I get it to direct to mysite/form_success.php or mysite/form_unsuccess.php - where the message is already in place on the respective page. Realise this must be pretty basic. Any help appreciated. Thanks.

    <form action="" method="post" name="contact">
    Name: <br>
    <input type="text" name="name">
    <br>
    Email: <br>
    <input type="text" name="email">
    <br>
    Subject: <br>
    <input type="text" name="subject">
    <br>
    Textbr>
    <textarea name="message" cols="40" rows=""></textarea>
    <br>
    <input type="submit" name="submit" value="Send">
    <input type="reset" name="Reset" value="Reset">
    </form>
    <?php
    if ($submit) { // if the form was sent do the following

    if($name && $subject && $email && $message ) { // if all field are filled-in send email
    mail("me@mysite.com","$subject","$message","From: $name <$email>") or die("email error");
    echo "Message Sent"; // if all went well, display message was sent
    } else {
    echo "All fields must be filled in!<BR>"; // if not all were filled in, display error message
    }
    } // end php submission code
    ?>

  2. #2
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this:
    PHP Code:
    <?
    ob_start
    ();

    if (
    $submit) { // if the form was sent do the following

    if($name && $subject && $email && $message ) { // if all field are filled-in send email
    mail("me@mysite.com","$subject","$message","From: $name <$email>") or die("email error");
    echo 
    "Message Sent"// if all went well, display message was sent
    header("Location: http://yoursite.com/form_success.php");
    exit();
    } else {
    header("Location: http://yoursite.com/form_unsuccess.php");
    exit();
    }
    }else{
    //display the form
    ?>
    <form action="" method="post" name="contact">
    Name: <br>
    <input type="text" name="name">
    <br>
    Email: <br>
    <input type="text" name="email">
    <br>
    Subject: <br>
    <input type="text" name="subject">
    <br>
    Text:<br>
    <textarea name="message" cols="40" rows=""></textarea>
    <br>
    <input type="submit" name="submit" value="Send">
    <input type="reset" name="Reset" value="Reset">
    </form>
    <?php
    }//end else
    ?>
    HTH
    Last edited by Mandibal; Feb 14, 2004 at 09:09. Reason: Changed more of the code example.
    Erh

  3. #3
    SitePoint Enthusiast MadDog31's Avatar
    Join Date
    Nov 2003
    Location
    Wilmington, NC
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I may not be much help, but maybe my idea could trigger an idea from someone else.

    I think in Kevin Yank's book on PHP & MySQL, he does what's called a multipurpose page where it has the form and PHP code right in the same script and you could modify it to work w/ your code...it's moreless like below:

    PHP Code:
    <html>
    <head>
      <title>Multipurpose Page</title>
    </head>
    <body>

    <?php if(condition) { ?>
      <!--HTML content to display if condition is true--!>

    <?php } else { ?>
      <!--HTML content to display if condition is false--!>

    <?php ?>

    </body>
    </html>
    I don't know if that'll help much but maybe someone else can shed some light on this topic too. It all requires using...

    PHP Code:
    <form action = "<?=$_SERVER['PHP_SELF'}?>" method = "get"
    // etc etc etc
    ...to have it post to the same page. PHP_SELF will always be set to the URL of the current page.

    Hope this at least gets the wheels turning.
    Ian
    "It's way better to have 100 idiot clients than to have one idiot boss."

  4. #4
    SitePoint Enthusiast
    Join Date
    Jan 2004
    Location
    Europe
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys for the quick response. Have been playing around with it but no joy. With the code you give Mandibal, I get the following:

    Form filled in OK = the same page comes back but instead of the form I see:

    Warning: Cannot modify header information - headers already sent by (output started at /home/mysite/public_html/faq.php:5) in /home/mysite/public_html/faq.php on line 188

    Form not filled in OK = the same page comes back but instead of the form I see:

    Warning: Cannot modify header information - headers already sent by (output started at /home/mysite/public_html/faq.php:5) in /home/mysite/public_html/faq.php on line 191

  5. #5
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the ob_start(); should be at the top of the script faq.php. This warning message is usually recieved when trying to send header information after output has already been sent by the web server. The ob_start(); call goes at the top (first line) of the page so that you can use the header("Location...") redirect any where on the page.
    Erh

  6. #6
    SitePoint Enthusiast
    Join Date
    Jan 2004
    Location
    Europe
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This at the top?

    <?
    ob_start();
    ?>

    Is so, still getting the header error problem.


Bookmarks

Posting Permissions

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