www.webdeveloper.com
Results 1 to 6 of 6

Thread: [RESOLVED] Posting form data to a DB, and then redirecting to another page.

  1. #1
    Join Date
    Feb 2005
    Posts
    42

    resolved [RESOLVED] Posting form data to a DB, and then redirecting to another page.

    Hi all, i'm working on a project where I have this form which when submitted dumps the fields into a database. Once the form data is stored however, I wanted the page to redirect to a 2nd page.

    I thought this could be done by simply adding a line at the bottom of my data submission script which would say something like:

    Code:
    header("Location: http://www.test.com/page2.html");
    When I add that bit of code I get errors on the page.

    Any suggestions on how to get this to work? or is my code just bugged.

    Heres my source code:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>EasyMoney4YouOnline.com | Sign up now to receive your money</title>
    <link href="css/main.css" rel="stylesheet" type="text/css" />
    <?
    #SUBMITS DATA FIELDS TO THE DATABASE
    	 include('connection.php');
         $FirstName = $_POST['FirstName'];
    	 $LastName = $_POST['LastName'];
         $Address   = $_POST['Address'];
    	 $City   = $_POST['City'];
    	 $State   = $_POST['State'];
    	 $Zipcode   = $_POST['Zipcode'];
    	 $Email     = $_POST['Email']; 
    
    	 if (isset($FirstName)) {
            $strSQL = "INSERT INTO UserData (FirstName, LastName, Address, City, State, Zipcode, Email) Values ('" . $FirstName . "','" . $LastName . "','" .$Address . "','".$City . "','" .$State . "','" .$Zipcode . "','" .$Email . "')" ;          
            $strResult = mysql_query($strSQL) or die(mysql_errno() . " : " . mysql_error() . "  <strong>Insert Failed for Some Reason</strong>");  
         }
    ?>
    </head>
    
    <body>
    <div class="container">
    	<div class="header"><a href="index.html"><img src="images/header.jpg" alt="Cash In Your Hands! The Fast and Easy Way."  width="862" height="190"/></a></div>
        <div class="bar_2">Provide Your Shipping Information</div>
        <div class="content">
    		<div class="faqs_text">
    			<div>
    			<form action="<?php echo $_SERVER[PHP_SELF]; ?>" method="post">
                    <fieldset><legend style="color:#000000;">Please fill out the form to continue to your offer.</legend>
    					<p>
    					<div>
    						<span style="float:left; width:150px; font-weight:bold;">First Name:</span>
    						<input type="text" name="FirstName" size="20" maxlength="40" value="<?php if (isset($_POST['FirstName'])) echo $_POST['FirstName']; ?>"/>
    					</div>
    					<div style="text-indent:150px; color:#CC3300; font-size:10px; height:20px;">
    						<?php
    							if (isset($_POST['submit'])) {
    								if (strlen($_POST ['FirstName']) > 0) {
    									$name = TRUE;
    								}else{
    									$name = FALSE;
    									echo'You forgot to enter your first name!';
    								}
    							}
    						?>
    					</div>
    					</p>
    					
    					<div>
    						<span style="float:left; width:150px; font-weight:bold;">Last Name:</span>
    						<input type="text" name="LastName" size="20" maxlength="40" value="<?php if (isset($_POST['LastName'])) echo $_POST['LastName']; ?>"/>
    					</div>
    					<div style="text-indent:150px; color:#CC3300; font-size:10px; height:20px;">
    						<?php
    							if (isset($_POST['submit'])) {
    								if (strlen($_POST ['LastName']) > 0) {
    									$name = TRUE;
    								}else{
    									$name = FALSE;
    									echo'You forgot to enter your last name!';
    								}
    							}
    						?>
    					</div>
    					
    					<div>
    						<span style="float:left; width:150px; font-weight:bold;">Email Address:</span>
    						<input type="text" name="email" size="40" maxlength="60" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>"/>
    					</div>
    					<div style="text-indent:150px; color:#CC3300; font-size:10px; height:20px;">
    						<?php
    							if (isset($_POST['submit'])) {
    								if (strlen($_POST ['email']) > 0) {
    									$email = TRUE;
    								}else{
    									$email = FALSE;
    									echo'You forgot to enter your email address!';
    								}
    							}
    						?>
    					</div>
    					
    					<div>
    						<span style="float:left; width:150px; font-weight:bold;">Shipping Address:</span>
    						<input type="text" name="address" size="50" maxlength="90" value="<?php if (isset($_POST['address'])) echo $_POST['address']; ?>"/>
    					</div>
    					<div style="text-indent:150px; color:#CC3300; font-size:10px; height:20px;">
    						<?php
    							if (isset($_POST['submit'])) {
    								if (strlen($_POST ['address']) > 0) {
    									$address = TRUE;
    								}else{
    									$address = FALSE;
    									echo'You forgot to enter your mailing address!';
    								}
    							}
    						?>
    					</div>
    					
    					<div>
    						<span style="float:left; width:150px; font-weight:bold;">City:</span>
    						<input type="text" name="city" size="15" maxlength="20" value="<?php if (isset($_POST['city'])) echo $_POST['city']; ?>"/>
    					</div>
    					<div style="text-indent:150px; color:#CC3300; font-size:10px; height:20px;">
    						<?php
    							if (isset($_POST['submit'])) {
    								if (strlen($_POST ['city']) > 0) {
    									$city = TRUE;
    								}else{
    									$city = FALSE;
    									echo'You forgot to enter what city your from!';
    								}
    							}
    						?>
    					</div>
    					
    					<div>
    						<span style="float:left; width:150px; font-weight:bold;">State:</span>
    						<input type="text" name="state" size="2" maxlength="2" value="<?php if (isset($_POST['state'])) echo $_POST['state']; ?>"/>
    					</div>
    					<div style="text-indent:150px; color:#CC3300; font-size:10px; height:20px;">
    						<?php
    							if (isset($_POST['submit'])) {
    								if (strlen($_POST ['state']) > 0) {
    									$state = TRUE;
    								}else{
    									$state = FALSE;
    									echo'You forgot to enter what state your from!';
    								}
    							}
    						?>
    					</div>
    					
    					<div>
    						<span style="float:left; width:150px; font-weight:bold;">Zipcode:</span>
    						<input type="num" name="zipcode" size="5" maxlength="5" value="<?php if (isset($_POST['zipcode'])) echo $_POST['zipcode']; ?>"/>
    					</div>
    					<div style="text-indent:150px; color:#CC3300; font-size:10px; height:20px;">
    						<?php
    							if (isset($_POST['submit'])) {
    								if (strlen($_POST ['zipcode']) > 0) {
    									$zipcode = TRUE;
    								}else{
    									$zipcode = FALSE;
    									echo'You forgot to enter your zipcode!';
    								}
    							}
    						?>
    					</div>
    					<br />
    					<div style="text-align:center; font-weight:bold; font-size:13px;">
    						You must provide correct information<br />
    						or your cash will not ship.
    					</div>
    					<br />
    					<br />
    					<br />
    					<br />
                        <div align="center" style="color:#CC3300; font-size:13px;">Click "<span style="font-weight:bold;">Submit Shipping Info</span>" to Continue.</div>
                        <div align="center"><br /><input type="submit" name="submit" value="Submit Shipping Info" style="font-size:14px;" /></div>
                    </fieldset>
                </form>
    			<br />
    			</div>
    			<div style="background:#CCCCCC;"><span style="font-weight:bold; font-size:11px;">Please note:</span> <span style="font-style:italic; font-size:11px;">You MUST use true and valid information. ANY signups found to be using invalid information (false name, address, credit card, etc..) will be disqualified from this offer.</span>
    			<br />
    			<br />
    			<span style=" font-size:11px;">You must be a US resident over 18 to participate. After submitting your order form, you will be taken to the offer page, where you must complete the required signup(s) from our partner advertisers. Your cash will ship within 24 hours of verification of your completion of the required advertiser signup(s).
    			<br />
    			<br />
    			Accurate information must be provided to the partner advertisers. You may only complete each advertiser offer one time. Any false or intentionally deceptive information provided to the advertisers will result in forfeiture of your free cash. By clicking "Submit" you are agreeing to these terms and conditions.</span></div>
    		</div>
        </div>
    	<div class="footer">
    		Copyright&copy; EasyMoney4YouOnline.com | <a href="termsofservice.html">Terms of Service</a>
    	</div>
    </div>
    </body>
    </html>

  2. #2
    Join Date
    Nov 2003
    Location
    Worthington, OH, USA
    Posts
    3,634
    As a general rule the header(.....) needs to come before any other headers or html statements are sent by the page.

    Depending on your configuration, sometimes bracketing the header with ob_start and ob_clean will work though:
    PHP Code:
    ob_start();
    header("Location: http://www.test.com/page2.html");
    ob_clean(); 
    54 68 65 42 65 61 72 4D 61 79

  3. #3
    Join Date
    Feb 2005
    Posts
    42
    Yeah, I just read on another site that you basically need to have the header() above any other html... so I tried putting my block of php at the top of the page and that fixed the problem.

    However now a new problem has arose. Since the header has nothing that prevents it from running at the time the page loads, it auto redirects my form page to the other page I wanted it to redirect to after the form has submitted.

    Code:
    <?php
    #SUBMITS DATA FIELDS TO THE DATABASE
    	 include('connection.php');
             $FirstName = $_POST['FirstName'];
    	 $LastName = $_POST['LastName'];
             $Address   = $_POST['Address'];
    	 $City   = $_POST['City'];
    	 $State   = $_POST['State'];
    	 $Zipcode   = $_POST['Zipcode'];
    	 $Email     = $_POST['Email']; 
    
    	 if (isset($FirstName)) {
            $strSQL = "INSERT INTO UserData (FirstName, LastName, Address, City, State, Zipcode, Email) Values ('" . $FirstName . "','" . $LastName . "','" .$Address . "','".$City . "','" .$State . "','" .$Zipcode . "','" .$Email . "')" ;          
            $strResult = mysql_query($strSQL) or die(mysql_errno() . " : " . mysql_error() . "  <strong>Insert Failed for Some Reason</strong>");  
         }
    	 header('Location: http://www.test.com/otherpage');
    ?>
    Any thoughts on how I can prevent it from redirecting until I submit the form?

    My first thought would be to just dump this black of code into another php file and have the form action set to that page for form processing then redirect again inside that processing page to go to my secondary page.... but would the data from the form carry from the form page to the processing page in that way?

  4. #4
    Join Date
    Feb 2005
    Posts
    42
    Ok so, yeah what I posted above does work. The form data is sent to the DB, and the page redirects, however now my form validation is broken, because rather than refreshing the page when the submit button is clicked it just goes to the processing page.

    Any ideas on how I can fix the validation?

    Heres what i've got now.

    signup.php
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>EasyMoney4YouOnline.com | Sign up now to receive your money</title>
    <link href="css/main.css" rel="stylesheet" type="text/css" />
    </head>
    
    <body>
    <div class="container">
    	<div class="header"><a href="index.html"><img src="images/header.jpg" alt="Cash In Your Hands! The Fast and Easy Way."  width="862" height="190"/></a></div>
        <div class="bar_2">Provide Your Shipping Information</div>
        <div class="content">
    		<div class="faqs_text">
    			<div>
    			<form action="process_account.php" method="post">
                    <fieldset><legend style="color:#000000;">Please fill out the form to continue to your offer.</legend>
    					<p>
    					<div>
    						<span style="float:left; width:150px; font-weight:bold;">First Name:</span>
    						<input type="text" name="FirstName" size="20" maxlength="40" value="<?php if (isset($_POST['FirstName'])) echo $_POST['FirstName']; ?>"/>
    					</div>
    					<div style="text-indent:150px; color:#CC3300; font-size:10px; height:20px;">
    						<?php
    							if (isset($_POST['submit'])) {
    								if (strlen($_POST ['FirstName']) > 0) {
    									$name = TRUE;
    								}else{
    									$name = FALSE;
    									echo'You forgot to enter your first name!';
    								}
    							}
    						?>
    					</div>
    					</p>
    					
    					<div>
    						<span style="float:left; width:150px; font-weight:bold;">Last Name:</span>
    						<input type="text" name="LastName" size="20" maxlength="40" value="<?php if (isset($_POST['LastName'])) echo $_POST['LastName']; ?>"/>
    					</div>
    					<div style="text-indent:150px; color:#CC3300; font-size:10px; height:20px;">
    						<?php
    							if (isset($_POST['submit'])) {
    								if (strlen($_POST ['LastName']) > 0) {
    									$name = TRUE;
    								}else{
    									$name = FALSE;
    									echo'You forgot to enter your last name!';
    								}
    							}
    						?>
    					</div>
    					
    					<div>
    						<span style="float:left; width:150px; font-weight:bold;">Email Address:</span>
    						<input type="text" name="email" size="40" maxlength="60" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>"/>
    					</div>
    					<div style="text-indent:150px; color:#CC3300; font-size:10px; height:20px;">
    						<?php
    							if (isset($_POST['submit'])) {
    								if (strlen($_POST ['email']) > 0) {
    									$email = TRUE;
    								}else{
    									$email = FALSE;
    									echo'You forgot to enter your email address!';
    								}
    							}
    						?>
    					</div>
    					
    					<div>
    						<span style="float:left; width:150px; font-weight:bold;">Shipping Address:</span>
    						<input type="text" name="address" size="50" maxlength="90" value="<?php if (isset($_POST['address'])) echo $_POST['address']; ?>"/>
    					</div>
    					<div style="text-indent:150px; color:#CC3300; font-size:10px; height:20px;">
    						<?php
    							if (isset($_POST['submit'])) {
    								if (strlen($_POST ['address']) > 0) {
    									$address = TRUE;
    								}else{
    									$address = FALSE;
    									echo'You forgot to enter your mailing address!';
    								}
    							}
    						?>
    					</div>
    					
    					<div>
    						<span style="float:left; width:150px; font-weight:bold;">City:</span>
    						<input type="text" name="city" size="15" maxlength="20" value="<?php if (isset($_POST['city'])) echo $_POST['city']; ?>"/>
    					</div>
    					<div style="text-indent:150px; color:#CC3300; font-size:10px; height:20px;">
    						<?php
    							if (isset($_POST['submit'])) {
    								if (strlen($_POST ['city']) > 0) {
    									$city = TRUE;
    								}else{
    									$city = FALSE;
    									echo'You forgot to enter what city your from!';
    								}
    							}
    						?>
    					</div>
    					
    					<div>
    						<span style="float:left; width:150px; font-weight:bold;">State:</span>
    						<input type="text" name="state" size="2" maxlength="2" value="<?php if (isset($_POST['state'])) echo $_POST['state']; ?>"/>
    					</div>
    					<div style="text-indent:150px; color:#CC3300; font-size:10px; height:20px;">
    						<?php
    							if (isset($_POST['submit'])) {
    								if (strlen($_POST ['state']) > 0) {
    									$state = TRUE;
    								}else{
    									$state = FALSE;
    									echo'You forgot to enter what state your from!';
    								}
    							}
    						?>
    					</div>
    					
    					<div>
    						<span style="float:left; width:150px; font-weight:bold;">Zipcode:</span>
    						<input type="num" name="zipcode" size="5" maxlength="5" value="<?php if (isset($_POST['zipcode'])) echo $_POST['zipcode']; ?>"/>
    					</div>
    					<div style="text-indent:150px; color:#CC3300; font-size:10px; height:20px;">
    						<?php
    							if (isset($_POST['submit'])) {
    								if (strlen($_POST ['zipcode']) > 0) {
    									$zipcode = TRUE;
    								}else{
    									$zipcode = FALSE;
    									echo'You forgot to enter your zipcode!';
    								}
    							}
    						?>
    					</div>
    					<br />
    					<div style="text-align:center; font-weight:bold; font-size:13px;">
    						You must provide correct information<br />
    						or your cash will not ship.
    					</div>
    					<br />
    					<br />
    					<br />
    					<br />
                        <div align="center" style="color:#CC3300; font-size:13px;">Click "<span style="font-weight:bold;">Submit Shipping Info</span>" to Continue.</div>
                        <div align="center"><br /><input type="submit" name="submit" value="Submit Shipping Info" style="font-size:14px;" /></div>
                    </fieldset>
                </form>
    			<br />
    			</div>
    			<div style="background:#CCCCCC;"><span style="font-weight:bold; font-size:11px;">Please note:</span> <span style="font-style:italic; font-size:11px;">You MUST use true and valid information. ANY signups found to be using invalid information (false name, address, credit card, etc..) will be disqualified from this offer.</span>
    			<br />
    			<br />
    			<span style=" font-size:11px;">You must be a US resident over 18 to participate. After submitting your order form, you will be taken to the offer page, where you must complete the required signup(s) from our partner advertisers. Your cash will ship within 24 hours of verification of your completion of the required advertiser signup(s).
    			<br />
    			<br />
    			Accurate information must be provided to the partner advertisers. You may only complete each advertiser offer one time. Any false or intentionally deceptive information provided to the advertisers will result in forfeiture of your free cash. By clicking "Submit" you are agreeing to these terms and conditions.</span></div>
    		</div>
        </div>
    	<div class="footer">
    		Copyright&copy; EasyMoney4YouOnline.com | <a href="termsofservice.html">Terms of Service</a>
    		<br />
    		Site coded and designed by <a href="http://www.axonms.com">Axon Multimedia Solutions</a>.
    	</div>
    </div>
    </body>
    </html>
    process_account.php
    Code:
    <?php
    #SUBMITS DATA FIELDS TO THE DATABASE
    	 include('connection.php');
         $FirstName = $_POST['FirstName'];
    	 $LastName = $_POST['LastName'];
         $Address   = $_POST['Address'];
    	 $City   = $_POST['City'];
    	 $State   = $_POST['State'];
    	 $Zipcode   = $_POST['Zipcode'];
    	 $Email     = $_POST['Email']; 
    
    	 if (isset($FirstName)) {
            $strSQL = "INSERT INTO UserData (FirstName, LastName, Address, City, State, Zipcode, Email) Values ('" . $FirstName . "','" . $LastName . "','" .$Address . "','".$City . "','" .$State . "','" .$Zipcode . "','" .$Email . "')" ;          
            $strResult = mysql_query($strSQL) or die(mysql_errno() . " : " . mysql_error() . "  <strong>Insert Failed for Some Reason</strong>");  
         }
    	 header('Location: http://www.easymoney4youonline.com/offers.html');
    ?>

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,359
    Looks to me like you want to move the header() line up inside the preceding IF block (right after the query is executed).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Feb 2005
    Posts
    42
    This code seems to have fixed all the problems together.

    Code:
    <?php
    if (strlen($_POST ['FirstName']) > 0 && strlen($_POST ['LastName']) > 0 && strlen($_POST ['email']) > 0 && strlen($_POST ['address']) > 0 && strlen($_POST ['city']) > 0 && strlen($_POST ['state']) > 0 && strlen($_POST ['zipcode']) > 0) {
    	include('connection.php');
    	$FirstName = $_POST['FirstName'];
    	$LastName = $_POST['LastName'];
    	$Address = $_POST['Address'];
    	$City = $_POST['City'];
    	$State = $_POST['State'];
    	$Zipcode = $_POST['Zipcode'];
    	$Email = $_POST['Email']; 
    	
    	if (isset($FirstName)) {
    		$strSQL = "INSERT INTO UserData (FirstName, LastName, Address, City, State, Zipcode, Email) Values ('" . $FirstName . "','" . $LastName . "','" .$Address . "','".$City . "','" .$State . "','" .$Zipcode . "','" .$Email . "')" ;          
    		$strResult = mysql_query($strSQL) or die(mysql_errno() . " : " . mysql_error() . "  <strong>Insert Failed for Some Reason</strong>");  
    	}
    	header('Location: http://www.easymoney4youonline.com/offers.html');
    }
    ?>
    Allowed me to keep the processing code in the page, and I just used strlen functions to check if every field in the form has something in it, before it processes the page.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles