3

I have this issue with my validation and posting the data to another page.

Here is my form:

Signup.php

<form id="regForm" action="<?php echo htmlspecialchars($_SERVER["submit.php"]);?>" method="post" name="regForm">
    <label for="fname">First Name:</label><input name="fname" type="text" size="25" maxlength="35" value="<?php if(isset($_POST['fname'])){echo $_POST['fname'];}?>"/><br/>
    <label for="mdname">Middle initial:</label><input name="mdname" type="text" size="10" maxlength="35" value="<?php if(isset($_POST['mdname'])){echo $_POST['mdname'];}?>"/><br/>
    <label for="lname">Last Name:</label><input name="lname" type="text" size="25" maxlength="35" value="<?php if(isset($_POST['lname'])){echo $_POST['lname'];}?>"/><br/>
    <br/>
    <label>&nbsp;</label><input type="submit" name="Signup" class="formButton" value="Signup" /></form>

And here is my submit.php which will validate the signup.html input

submit.php

function msg($status,$txt)
{
    return '{"status":'.$status.',"txt":"'.$txt.'"}';
}

// we check if everything is filled in and perform checks
//check if fname is empty
if(!$_POST['fname'])
{
    die(msg(0,"<p>Please enter your first name.</p>"));
}
//check if lname is empty
if(!$_POST['lname'])
{
    die(msg(0,"<p>Please enter your last name.</p>"));
}

Now, my issue is this, in my "submit.php" file, I want to know what codes to put after the form fields validation that would enable me post the input data to another page because, I plan making it a two-page signup form. Let's say my next page is signup-2.html

how do I post the data after validation to the next page? I know how to retrieve the posted data on the next page like using Session or Echo the $_POST data but, my main issue is....how do I make the form post the data after the validation messages in my submit.php file?

Prashant Tapase
  • 2,132
  • 2
  • 25
  • 34
Smiling Faces
  • 33
  • 1
  • 3

1 Answers1

2

use header :

header("Location: your page url?fname=$_POST['fname']&lname=$_POST['lname']");

but before this do not echo or print anything otherwise it won't redirect to that page.

you can use the data on destination page like this:

$_GET['fname']

example:

submit.php

function msg($status,$txt)
{
    return '{"status":'.$status.',"txt":"'.$txt.'"}';
}

// we check if everything is filled in and perform checks
//check if fname is empty
if(!$_POST['fname'])
{
    die(msg(0,"<p>Please enter your first name.</p>"));
}
//check if lname is empty
if(!$_POST['lname'])
{
    die(msg(0,"<p>Please enter your last name.</p>"));
}

header('Location:download.php?fname='.$_POST['fname']."&lname=".$_POST['lname']);

view.php

<html>
<body>
<form action="submit.php" method="post">
<input type='text' id="fname" name="fname"/>
<input type='text' id="lname" name="lname"/>
<input type="submit" id="button" value="submit"/>
</form>

</body>


</html>

download.php

<?php
echo "First Name........".$_GET['fname'];

put these three file in same directory and run view.php. you will be ok.

Suchit kumar
  • 11,809
  • 3
  • 22
  • 44
  • 1
    This will change your $_POST parameters to $_GET. – Hirnhamster Aug 22 '14 at 11:42
  • Please, review my "submit.php" code and tell me exactly where to put the header location to make it post the data to the next page after the validation. – Smiling Faces Aug 22 '14 at 11:50
  • you want to get this if every thing is filled so put this after second if and see.one thing always check values using isset() method. – Suchit kumar Aug 22 '14 at 11:55
  • Thanks for your response, Suchit. But, I don't really understand what you are trying to explain. Could be a little more elaborate? Thanks! – Smiling Faces Aug 22 '14 at 12:01
  • function msg($status,$txt) { return '{"status":'.$status.',"txt":"'.$txt.'"}'; } // we check if everything is filled in and perform checks //check if fname is empty if(!$_POST['fname']) { die(msg(0,"

    Please enter your first name.

    ")); } //check if lname is empty if(!$_POST['lname']) { die(msg(0,"

    Please enter your last name.

    ")); } header("Location: your page url?fname=$_POST['fname']&lname=$_POST['lname']"); then on your redirected page access the variables using $_GET method.
    – Suchit kumar Aug 22 '14 at 12:02
  • copy the above comment and replace with your submit.php and in header add your url to next page then echo $_GET['fname']; – Suchit kumar Aug 22 '14 at 12:05
  • It didn't work out. I even added exit(); after the header and location line. – Smiling Faces Aug 22 '14 at 12:26
  • Yea..it worked! Now, all I need to do is find a way to display my error properly on the first page with the "msg" error response. – Smiling Faces Aug 22 '14 at 13:32