I apologize if this has already been asked/answered

I am have trouble with this, although I imagine the solution is rather simple:

  1. User completes form and submits
  2. Page either reloads displaying User-entered data or redirects to a new page displaying said data for User

I have been trying this with questionable results.

Any direction or help would be greatly appreciated.

Thank you in advance!

Matthew

Recommended Answers

All 8 Replies

you can use the php code

<?php header('Location:http://localhost/submit.php'); ?>

To a submit.php page that displays "Your input has been submitted sucessfully" or one that displays the information the user just entered

I want to allow the User to enter and submit data - Then, that data should be displayed for the User, formatted on a new page.

You could write your own code(php or javascript) to validate that the user must enter all the fields and enter them appropriately@gabrielcastillorecommended this code for me a while back

if( isset($_POST) ){
    $errors = array();
    if(!isset($_POST['title'])){
        $errors['title'] = 'Error: Title is required.';
    }
    if(!isset($_POST['body']){
        $errors['body'] = 'Error: Body is required.';
    }
    if(isset($errors['title']) && isset($errors['body'])){
        foreach($errors as $error){
            echo $error . '<br />';
        }
    }else{
        //Run code below
    }
}

Or use a validation library eg my two favorites http://rickharrison.github.io/validate.js/ and http://jqueryvalidation.org/documentation/ .Create two files one Submitsuccess.php which retrieves the values from the $_POST array and displays (what the user just entered) to the user and a second file that inserts the submitted information to a database (prefferably) or just saves in to that file.voila...

//someform.php
<!DOCTYPE html>
<html>
      <head>
        <title>Some form</title>
        <script type="text/javascript" src="validate.min.js"></script>
      </head>
     <body>
     <form action="submitsuccess.php" method="post">
    Name: <input type="text" name="name"required="required"><br>
    Email:<input type="text" name="email" required="required"><br>
  Password: <input type="text" name="password"required="required"><br>
  <input type="submit" value="Submit">
     </form>
        <script type="text/javascript">
        var validator = new FormValidator('example_form', [{
    name: 'req',
    display: 'required',    
    rules: 'required'
}, {
    name: 'alphanumeric',
    rules: 'alpha_numeric'
}, {
    name: 'password',
    rules: 'required'
},{
    name: 'email',
    rules: 'valid_email'
}, {
    name: 'minlength',
    display: 'min length',
    rules: 'min_length[8]'
}, {
    name: 'email',
    rules: 'valid_email'
}], function(errors, event) {
    if (errors.length > 0) {
        // Show the errors
    }
});</script>
     </body> 
</html>

Then

//submitsuccess.php
<!DOCTYPE html>
<html>
      <head>
        <title>Form submitted</title>
      </head>
     <body>
     <h1>This is what you just posted</h1>
     <?php echo $_POST['name'];?><br>
     <?php echo $_POST['email'];?>

     </body> 
</html>

And with this code you dont need the php redirect

<?php header('Location:'); ?>

It redirects you right away, i tested it on my local server and it works

Hi Just echo your submitted data in another page if you want something like a preview page before inserting it to DB as rhodoscoder suggested..

something like

preview.php
<?php

echo $_POST['name'];

?>

Thank you for everyone's help thus far!

I am able to redirect sucessfully, but there is a further issue:

  1. I place the redirect code at the very top (To ensure no data is sent to the browser) - Now, after retesting, I receive the following error: Parse error: syntax error, unexpected 'header' (T_STRING) in /home/vhosts/absinthe--.---.org/simple------_php.php on line 6
  2. When it appeared to be working earlier, the redirection, I load the form page, and instead of displaying the form for User input, it instantly redirects the page, no form dispayed for User input > Submit

Please view current code on the said page:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<?php
//Redirect     
header('Location: /index1.php');
?>

<html>
<body>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">Employee Name</td>
<td><input name="emp_name" type="text" id="emp_name"></td>
</tr>
<tr>
<td width="100">Employee Address</td>
<td><input name="emp_address" type="text" id="emp_address"></td>
</tr>
<tr>

<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="add" type="submit" id="add" value="Add Employee">
</td>
</tr>
</table>
</form>

<?php
$con = mysql_connect("localhost","372---", "c--------");
if (!$con)
  {
  die('NEIN! ' . mysql_error());

  }
//Connect to DB
$res = mysql_query("SHOW DATABASES");

while ($row = mysql_fetch_assoc($res)) {
    echo $row['Database'] . "\n";
}
mysql_select_db(372110, $con)
or die("Lost");
//Detect DB

$sql="INSERT INTO b8_14160309_-------- (emp_name, emp_address)
VALUES
('$_POST[emp_name]','$_POST[emp_address]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "3 record added";
//Add entry to DB

//TEST
$result = mysql_query("SELECT * FROM b8_14160309_-------- LIMIT 1");
echo "<table border='1'>
<tr>
<th>Employee Name</th>
<th>Employee Address</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['emp_name'] . "</td>";
echo "<td>" . $row['emp_address'] . "</td>";
echo "</tr>";
}
echo "</table>";   
?>

Thank you in advance, Friends!
Matthew

Update:

Fixed the parse error - But page still instantly redirects, omitting User, data-form. Confused.

Redirects use Absolute paths , I used my localhost absolute address earlier that directs to the desired file, like so

<?php header('Location:http://localhost/submit.php'); ?>

If you had to redirect I dont think you would place this code at the very top , rather after the user had entered and submitted the form.

Looking at your script , you are insering the information to the database then retrieving it to the same file(I personally would do this in a separate file) so you don't really need the redirect.(remove this from the top of your script)

<?php
//Redirect     
header('Location: /index1.php');
?>

I also think you have this code wrong

"<?php $_PHP_SELF ?>

I think it's $PHP_SELF and i'm not sure on this but i think its deprecated, i could find it on the php manual online (which you should refer to alot)
PHP manual

My recommendations , in the <form action=""> use a different file e.g submit.php then place all this code in it

/Connect to DB
$res = mysql_query("SHOW DATABASES");
while ($row = mysql_fetch_assoc($res)) {
    echo $row['Database'] . "\n";
}
mysql_select_db(372110, $con)
or die("Lost");
//Detect DB
$sql="INSERT INTO b8_14160309_-------- (emp_name, emp_address)
VALUES
('$_POST[emp_name]','$_POST[emp_address]')";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "3 record added";
//Add entry to DB
//TEST
$result = mysql_query("SELECT * FROM b8_14160309_-------- LIMIT 1");
echo "<table border='1'>
<tr>
<th>Employee Name</th>
<th>Employee Address</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['emp_name'] . "</td>";
echo "<td>" . $row['emp_address'] . "</td>";
echo "</tr>";
}
echo "</table>";   
?>

This would automatically redirect your script.
Other recommendations create a separate file for database connections and require or include it in your files(cleaner code).
And finally use PDO prepared statements with mysql(Google it) it's more secure, i think basic queries might be deprecated in the future.
Phew...!! I ramble .Hope this helps .

/****just some novice coder trying to help and learn, my code isn't perfect****/

rhodoscoder:

I shall implement ALL of your suggestions and report back to this thread with the results.

Thank you for your excellent help. You outdid yourself, my friend!

Regards,
Matthew

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.