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 13 of 13
  1. #1
    New Coder
    Join Date
    Dec 2006
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form Displays Echo before submitted

    Hi, hope everyone had a great Christmas.

    The form I'm having problem with is this:

    PHP Code:
    <div id="headnav"><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <p>Please Enter Your
    <label>Name: <input type="text" name="name" /></label> <label>Email: <input type="text" name="email" /></label><input type="submit" value="SUBMIT" />
    </form>

    <?php
      
    // A new author has been entered
      // using the form below.

    include 'db.inc.php';

      
    $name $_POST['name'];
      
    $email $_POST['email'];
      
    $sql "INSERT INTO author SET
          name='$name',
          email='$email'"
    ;
      if (@
    mysql_query($sql)) {
        echo 
    'Thanks for signing up! <a href="submit.php">Submit an article</a>';
      } else {
        echo 
    'Error adding new author: ' .
            
    mysql_error() . '</p>';
      }

    ?>
    What I want to happen is after the user has logged in and clicked 'submit', the message will come up 'Thanks for signing up!'. But, its already appearing before the submit button has been clicked.

    I'm sure there's a fault with my php or form somewhere, I just can't place it.

  • #2
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    The problem is that after it makes the form, it just keeps going and executes the PHP code below. You need to either store the form handling code in a different file or make the form handling code execute only if the form has been submitted..

    Maybe like this:
    PHP Code:
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>?submit=true">
    //Rest of form stuff here

    <?php
    $submit 
    $_GET['submit'];
    if(
    $submit == "true")
    {
    //Code to handle form
    }
    ?>
    HTH, Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #3
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    You could also just check isset($_POST['your submit button name']); Make sure to give your submit button a name, and use it with $_POST.

    good luck.

  • #4
    New Coder
    Join Date
    Dec 2006
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, so now I have:

    PHP Code:
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>?submit=true" method="post">
    <p>Please Enter Your
    <label>Name: <input type="text" name="name" /></label> <label>Email: <input type="text" name="email" /></label><input type="submit" value="SUBMIT" /></form> 
    <?php

      
    // A new author has been entered
      // using the form below.

    include 'db.inc.php';

    $submit $_GET['submit'];
    if(
    $submit == "true")

      
    $name $_POST['name'];
      
    $email $_POST['email'];
      
    $sql "INSERT INTO author SET
          name='$name',
          email='$email'"
    ;
      if (@
    mysql_query($sql)) {
        echo 
    'Thanks for signing up! <a href="submit.php">Submit an article</a>';
      } else {
        echo 
    '<p>Error adding new author: ' .
            
    mysql_error() . '</p>';
      }
    }
    ?>
    The 'thanks for signing up!' message is displaying aswell as the form - is it possible to have the form 'disappear' and have just the message?

  • #5
    Regular Coder
    Join Date
    Oct 2005
    Location
    Surrey, England
    Posts
    286
    Thanks
    2
    Thanked 2 Times in 2 Posts
    PHP Code:
    <?php
    $submit 
    $_GET['submit'];
    if(
    $submit != "true")

    echo 
    '

    <form action="' 
    $_SERVER['PHP_SELF'] . '?submit=true" method="post">
    <p>Please Enter Your
    <label>Name: <input type="text" name="name" /></label> <label>Email: <input type="text" name="email" /></label><input type="submit" value="SUBMIT" /></form>

    '
    ;
    }

      
    // A new author has been entered
      // using the form below.

    if($submit == "true")
    {

    include 
    'db.inc.php';
      
    $name $_POST['name'];
      
    $email $_POST['email'];
      
    $sql "INSERT INTO author SET
          name='$name',
          email='$email'"
    ;
      if (@
    mysql_query($sql)) {
        echo 
    'Thanks for signing up! <a href="submit.php">Submit an article</a>';
      } else {
        echo 
    '<p>Error adding new author: ' .
            
    mysql_error() . '</p>';
      }

    }
    ?>
    Daniel Warner
    - - - - - - - - - -
    GuitarMart.co.uk - Free Online Classified Advertisement for Guitarists in the United Kingdom. Free Ads with a Free Photo.

  • #6
    New Coder
    Join Date
    Dec 2006
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Aha! Excellent. It appears I had a few things the wrong way round...

    I guess my next question would be, how do I keep the 'thanks for signing up!' (or some kind of welcome message) message on my site at all times?

  • #7
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    When the person logs in, store his info in a SESSION variable. Then on each page, check to see if the session var exists, and if it does, echo the welcome msg.

    EDIT: Looks like I have more time to explain than I thought I did:
    In order to use sessions, you must put this line:
    session_start();
    at the very very beginning of each page.

    Then, you can do this on the log in page
    PHP Code:
    //Code to log user in
    $_SESSION['logged_in'] = "true"
    ...and this on every page where you want to display a msg if the person is logged in

    PHP Code:
    if($_SESSION['logged_in'] == "true")
    {
    echo 
    $welcome_msg;

    HTH
    Dan
    Last edited by whizard; 12-28-2006 at 12:29 AM.
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #8
    New Coder
    Join Date
    Dec 2006
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Is it going to be much harder to code this if I want the message to remain if a user leaves the site, and then comes back again?

  • #9
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    You would need some way of determining that it was the user when they came back.

    You could use a cookie, which is easy to setup, but it will expire after a while, and some people disable them.

    Your other option is to use a database and a login script, which is harder to setup, and people will still have to login to get the message, but is permanent.

    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #10
    New Coder
    Join Date
    Dec 2006
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think I would like to use the 'cookie' method for now, I'll add a disclaimer that the site requires cookies (Users only need to sign in if they want to add an article to the site - regular viewers can view the articles without having to sign up).

  • #11
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Here is the PHP page on cookies...
    http://us3.php.net/manual/en/features.cookies.php

    I was going to give example codes, but then it struck me that I wasn't sure exactly what you are trying to do.

    Who are you trying to show the welcome message to?
    Everyone?
    Some People? - If so, how will you know which people?

    Unfortuantly, I'm gonna be in Virginia tomorrow and New York (State) Fri-Sun, so I hope someone else will be able to pick up the slack for a while.
    (I may check these boards once tomotrrow night, but I hope your problem is solved by then!)

    For now, check out the PHP cookies link and see what that does for you!

    HTH
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #12
    New Coder
    Join Date
    Dec 2006
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by whizard View Post
    Here is the PHP page on cookies...
    http://us3.php.net/manual/en/features.cookies.php

    I was going to give example codes, but then it struck me that I wasn't sure exactly what you are trying to do.

    Who are you trying to show the welcome message to?
    Everyone?
    Some People? - If so, how will you know which people?
    I'm trying to show the welcome message to those who have completed the form.

    Will check out the link - thanks!

  • #13
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Quote Originally Posted by XiledWeb
    I'm trying to show the welcome message to those who have completed the form.
    So, when someone submits your form, you put this code in the form handler

    PHP Code:
    setcookie(*/ Put your values here */); 
    and in any page you want to show a welcome message, you would put something like this:
    PHP Code:
    if(isset($_COOKIE['NAME YOU PUT IN SETCOOKIE'];))
    {
    print(
    "Thank You for signing up!");

    HTH, Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION


  •  

    Posting Permissions

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