8 Replies - 3591 Views - Last Post: 20 November 2012 - 02:18 PM Rate Topic: -----

#1 Xarim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-November 10

HOW TO REDIRECT PAGE AFTER FORM SUBMISSION AND REMOVE DUPLICATE ROW

Posted 20 November 2012 - 07:52 AM

Hi all,

I have a database with two tables:

CREATE TABLE IF NOT EXISTS `players` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `firstname` varchar(32) NOT NULL,
  `lastname` varchar(32) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=19 ;


and the second Table:
CREATE TABLE IF NOT EXISTS `teams` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `team_name` varchar(32) NOT NULL,
  `player_id` varchar(32) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=127 ;



The following form allows me to "Add a new record", "delete" or "add more info" into Table "players" database:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
        <title>View Records</title>
</head>
<body>

<?php
/* 
        VIEW.PHP
        Displays all data from 'players' table
*/

        // connect to the database
        include('connect-db.php');

        // get results from database
        $result = mysql_query("SELECT * FROM players ORDER BY lastname") 
                or die(mysql_error());  
                
        // display data in table
        echo "<p><b>View All</b> | <a href='view-paginated.php?page=1'>View Paginated</a></p>";
        
        echo "<table border='1' cellpadding='10'>";
        
        echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th>Edit</th> <th>Delete</th><th>Add more</th></tr>";

        // loop through results of database query, displaying them in the table
        while($row = mysql_fetch_array( $result )) {
                
                // echo out the contents of each row into a table
                echo "<tr>";
                echo '<td>' . $row['id'] . '</td>';
                echo '<td>' . $row['firstname'] . '</td>';
                echo '<td>' . $row['lastname'] . '</td>';
                echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>';
                echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>';
				echo '<td><a href="add.php?id=' . $row['id'] . '">Add more info</a></td>';
                echo "</tr>"; 
        } 

        // close table>
        echo "</table>";
?>
<p><a href="new.php">Add a new record</a></p>

</body>
</html> 




The "Add more info" is linked to a second form called "add.php" which in turn inserts team_name as follows:

<html>
 <form action="" method="post">
  <strong>Team Name: *</strong> <input type="text" name="team_name"><br/>
 <input type="submit" name="submit" value="Submit">
 </form> 
</html>
<?php
 $player_id = (int)($_GET['id']);
 $team = $_POST['team_name'];
 // connect to the database
 include('connect-db.php');
 // save the data to the database
 mysql_query("INSERT INTO teams (team_name, player_id) VALUES('$team','$player_id')")
 or die(mysql_error()); 
?>



I'm not sure if the form is any good, and this is where I run into trouble:

The form inserts (id, team_name, and player_id) "teams" table however it inserts it twice: once with the team_name in and the other with blank. And I don't know how to redirect the form to another page after submission.

Your help will is highly appreciated.
PHP is keeping late into the night every night.

Thank you all.

Is This A Good Question/Topic? 0
  • +

Replies To: HOW TO REDIRECT PAGE AFTER FORM SUBMISSION AND REMOVE DUPLICATE ROW

#2 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: HOW TO REDIRECT PAGE AFTER FORM SUBMISSION AND REMOVE DUPLICATE ROW

Posted 20 November 2012 - 07:58 AM

Hello there,

What I guess is happening is you are redirecting back to the page with the GET value still in place after submission but the POST is not, causing an extra blank submission.

What I suggest you do is, when you have submitted for the first time, use a 303 redirect like shown below. You'll need to perform this before sending ANY output to the client.
header('HTTP/1.1 303 See Other');
				header('Location: myPage.php');

What does 303 do? Well, it will redirect them to the page you point them at and they will have no knowledge of ever being on the page between. If they try to use their browsers back or history, the skip never happened, as far as they are concerned, they posted to the page you redirected to.
Was This Post Helpful? 1
  • +
  • -

#3 Xarim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-November 10

Re: HOW TO REDIRECT PAGE AFTER FORM SUBMISSION AND REMOVE DUPLICATE ROW

Posted 20 November 2012 - 08:07 AM

Hi Kruithne,

Thank you I appreciate your quick reply and help. This redirects the user to another page but I want them first to submit form before they get redirected.

Here is the form again:

<?php header('HTTP/1.1 303 See Other');
				header('Location: view.php');?>
<html>
 <form action="" method="post">
  <strong>Team Name: *</strong> <input type="text" name="team_name"><br/>
 <input type="submit" name="submit" value="Submit">
 </form> 
</html>
<?php
 $player_id = (int)($_GET['id']);
 $team = $_POST['team_name'];
 // connect to the database
 include('connect-db.php');
 // save the data to the database
 mysql_query("INSERT INTO teams (team_name, player_id) VALUES('$team','$player_id')")
 or die(mysql_error()); 
?>


This post has been edited by Dormilich: 21 November 2012 - 12:52 AM
Reason for edit:: removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#4 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: HOW TO REDIRECT PAGE AFTER FORM SUBMISSION AND REMOVE DUPLICATE ROW

Posted 20 November 2012 - 08:19 AM

Hello again,

I think the structure of the page needs to change a little, for multiple reasons. Take a look at the small rewrite below and see how I've changed things. It should work better for you.
<?php
	$player_id = (int)($_GET['id']); // Get the player id
	$team = (isset($_POST['team_name']) ? $_POST['team_name'] : null); // Check for the team name
	
	if ($team !== null) // If we have a team name set, do this scope.
	{
		include('connect-db.php'); // DB connection stuff
		mysql_query("INSERT INTO teams (team_name, player_id) VALUES('$team', '$player_id')") or die(mysql_error()); // Submit
		
		header('HTTP/1.1 303 See Other'); // Set return
		header('Location: view.php'); // Set location
	}
?>
<html>
 <form action="" method="post">
  <strong>Team Name: *</strong> <input type="text" name="team_name"><br/>
 <input type="submit" name="submit" value="Submit">
 </form> 
</html>

I've included some comments in the code to point out specific things too, hopefully they provide a little more aid than me explaining below will.

First thing I changed was your $team declaration. You'll get errors if you try to call something from the $_POST array when it hasn't been set. What I've done is a simple shorthand declaration, if the variable exists in the POST array, that's what the variable is set to, otherwise it is set to null.

We put all our submission stuff in a scope that checks if the $team variable is null or not. If it's not, we'll submit it to the database and then call the headers to redirect.

Your processing code should always go above your HTML markup. The main reason it plays importance here is because when using the header function, you can't have anything outputted.

In total, the page will now show a form if there is no POST data, otherwise it will submit it to the DB and redirect. Hopefully this helps you out and gives you an idea of what was going wrong!
Was This Post Helpful? 0
  • +
  • -

#5 Xarim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-November 10

Re: HOW TO REDIRECT PAGE AFTER FORM SUBMISSION AND REMOVE DUPLICATE ROW

Posted 20 November 2012 - 08:51 AM

Hi Kruithne,

Thank you ever so much, It's all working, the only thing I noticed is that It creates another blank row if I pressed submit without filling in the input box. Am i missing something??
I do really appreciate your time and help.

Regards
Xarim

This post has been edited by Dormilich: 21 November 2012 - 12:53 AM
Reason for edit:: removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#6 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: HOW TO REDIRECT PAGE AFTER FORM SUBMISSION AND REMOVE DUPLICATE ROW

Posted 20 November 2012 - 09:35 AM

Hello again, you appear to have quoted my post four times in your own, you might want to remove them to make it look a little clearer.

Could you post how your code looks now so I can debug the blank line issue.
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2992
  • Posts: 10,337
  • Joined: 08-August 08

Re: HOW TO REDIRECT PAGE AFTER FORM SUBMISSION AND REMOVE DUPLICATE ROW

Posted 20 November 2012 - 10:01 AM

I'm traveling and using one of those "4G" LTE routers/modems. My limit is 5GB per month, so all of this redirecting of internet traffic bothers me now more than it used to. It's always bothered me though, because it's a waste of resources. The browser is already in contact with your server, so there is no reason to tell them to break the connection and start a new one! Just include the page that you would have redirected them to!

if($form_completed) {
  include "file_that_gets_team_info.php";
} else {
  include "file_that_shows_form_with_posted_notposted_info.php";
}

This way you don't waste the user's bandwidth and you don't waste yours either! When you waste the user's bandwidth you can cost them money. When you waste yours you can cost yourself customers and money!
Was This Post Helpful? 1
  • +
  • -

#8 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: HOW TO REDIRECT PAGE AFTER FORM SUBMISSION AND REMOVE DUPLICATE ROW

Posted 20 November 2012 - 10:25 AM

Exactly what CTphpnwb said. I normally do this as standard with web applications, the reason the examples I provided went against this because OP specifically requested redirection. I highly suggest you go for the selective inclusion if possible.
Was This Post Helpful? 0
  • +
  • -

#9 Xarim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-November 10

Re: HOW TO REDIRECT PAGE AFTER FORM SUBMISSION AND REMOVE DUPLICATE ROW

Posted 20 November 2012 - 02:18 PM

Hi Guys,

I appreciate the input from both of you guys, I really do. I'm still learning, everything I learnt from basic html, css and php has been through online tutorials and forums like these, I value every feedback too and it's only due to my lack of mastering/understanding the language (PHP). Sometimes a simple thing like (redirecting or not ) seems so obvious for an experienced web developer but it takes me much more time to understand it and how to apply more web standards techniques.

Thanks again for your time and feedback.
Xarim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1