Redirect after submitting form. Please help

Discussion in 'PHP' started by lokielookies, Mar 11, 2007.

  1. #1
    Hi!

    I'm working on a little script, in which info sent through a form is written to a MySQL database.

    My problem is, after submitting the form, the visitor gets redirected to the page with the empty form again. There's no indication that his info was sent successfully.

    I would like to either
    * redirect the visitor to a success-page after successful submission

    or
    * display a message on the form page after successful submission (using php)

    And this without messing up the code for sending the form :rolleyes:

    The problem is, I don't know where to start. Do I make changes to the <form> code, or do I edit the 'action' script file?

    Please help me out?

    Greetz!
    mieke
     
    lokielookies, Mar 11, 2007 IP
  2. hamidof

    hamidof Peon

    Messages:
    619
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Post your script so we can help you.
     
    hamidof, Mar 11, 2007 IP
  3. srobona

    srobona Active Member

    Messages:
    577
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    88
    #3
    Not a tough job. Please pm me with the php code. I'll try to help you
     
    srobona, Mar 11, 2007 IP
  4. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #4
    
    First example :
    
    <?
    /**
    * Example form letting the user know what's happening
    **/ 
    if($_POST)
    {
    	/**
    	* This is where you code will be to check the form submission
    	**/
    	if( strlen($_POST['item_1']) > 5 )
    	{
    		$message = "Your name is less than five letters long<br />\n";
    	}
    	else
    	{
    		$message = "Your name is longer than five letters long<br />\n";
    	}
    }
    ?>
    <!-- Next the tag to display the message generated by the action(s) above -->
    <?=$messages ?>
    <form action="" method="post">
    Item 1 : <input type="text" name="item_1" /><br/>
    <input type="submit" value="Post" />
    </form>
    
    Second example :
    
    <?
    /**
    * Example form redirecting the user after form submission
    **/
    if($_POST)
    {
    	/**
    	* This is where you code will be to check the form submission
    	**/
    	if( strlen($_POST['item_1']) > 5 )
    	{
    		header("location:page_for_more_than_five_letters.php");
    		exit;
    	}
    	else
    	{
    		header("location:page_for_less_than_five_letters.php");
    		exit;
    	}
    }
    ?>
    <form action="" method="post">
    Item 1 : <input type="text" name="item_1" /><br/>
    <input type="submit" value="Post" />
    </form>
    
    PHP:
     
    krakjoe, Mar 11, 2007 IP
  5. mariush

    mariush Peon

    Messages:
    562
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #5
    mariush, Mar 11, 2007 IP
  6. techie007

    techie007 Peon

    Messages:
    261
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I have a simple code for you..try this out..if this is what you are looking for.

    <html>
    <body>
    <form id="form" name="form">
    <table border="0" cellpadding="0" cellspacing="0" width="781">
    <tr>
    <td align="Left" width="115" ><font face="Verdana" size="2"><strong>Name:</strong></font></td>
    <td align="Left" >
    <input name="Name" type="text" value="Enter Your Name" size="27" tabindex="1" /></td>
    </tr>
    <tr>
    <td width="115"><b><font face="Verdana" size="2">Item Name:</font></b></td>
    <td>
    <input name="ItemName" type="text" value="Enter Your Item Name" size="27" tabindex="1" /></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td><font face="Verdana"></font></td>
    <td>
    <input type="submit" name="Submit" value="Submit" style="float: left" /></td>
    </tr>
    </table>
    <?
    // Connect database.
    $host="localhost"; // MySQL Host name; commonly it is localhost.
    $db_user=""; // MySQL username.
    $db_password=""; // MySQL password.
    $database=""; // Database name.
    $db_table=""; //Table where you want to insert the information.
    mysql_connect($host,$db_user,$db_password);
    mysql_select_db($database);
    if (isset($_REQUEST['Submit']))
    {
    mysql_query("INSERT INTO `userinfo` VALUES ('$name', '$itemname')");
    echo "Thank you ".$Name.", You have item name as ".$ItemName;
    } 	
    ?>
    <p align="center">&nbsp;</p>
    </form>
    </body>
    </html>
    PHP:
    Thanks,
     
    techie007, Mar 11, 2007 IP
  7. lokielookies

    lokielookies Well-Known Member

    Messages:
    1,246
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    150
    #7
    Thanks for all the tips!

    I've found the solution to my problem.
    It was there all the time, I just didn't recognize it :rolleyes:

    I just had to change the page name in

    header("Location: thanks.php")

    Greetz!
    Mieke Janssens
     
    lokielookies, Mar 12, 2007 IP