php multipage input value session variable print - php

please correct me here.
i have created multi page form where i want to pass data from each pages to final pages and then submit those on email. first one is apply.php, there are many input fields, but i have listed some of those, here when some enters passport number in passport field, i want this to be passed in everypage of the form and print this at couple of places on each page. here getting some issues when passing some of these fields.
this is first page ( apply.php )
<?php
// Start the session
session_start();
?>
<form name="search_form" method="post" onSubmit="return chk();" action="apply2.php">
<input name="passportno" id="passportno" type="text" maxlength="20" placeholder="Enter Passport No." size="43" >
<input name="birthdate" type="date" class="textBoxDashed" size="43" id="birthdate" datepicker="true" datepicker_min="01/01/1900" datepicker_max="21/11/2017" maxlength="10" datepicker_format="DD/MM/YYYY" isdatepicker="true" value="">
<input name="button1" type="submit" value="Continue">
this is apply2.php . here there is some issues, i am not able to find, as you can see below codes, i am able to print date of birth but not able to print passport no ( input from form1 ). Please correct where i am wrong here.
<?php
session_start();
$msg="";
////include("connect.php");
if (isset($_POST['button1']))
{
extract($_POST);
$code=strtolower($_POST['captcha_code']);
$sess=strtolower($_SESSION["code"]);
if ($sess==$code)
{
$appid=time().rand();
$result=mysqli_query($con,"select *from registration where email='$email'");
if (mysqli_fetch_row($result)>0)
{
?>
<script>
alert("This email is already exist");
</script>
<?php
}
else
{
$query="insert into registration values('$appid','$passportno','$birthdate','$email')";
if (mysqli_query($con,$query))
$msg="Data saved Successfully.Please note down the Temporary Application ID $appid";
else
echo "not inserted".mysqli_error($con);
if (!isset($_SESSION["appid"]))
{
$_SESSION["appid"]=$appid;
}
}
}
else
{
?>
<?php
}
}
?>
<form name="OnlineForm" method="post" onsubmit="return OnSubmitForm();" action="apply3.php">
<input name="applid" id="applid" value="<?php echo $_SESSION["appid"];?>">
<input type="hidden" name="birthdate" value="<?php echo $birthdate;?>"><b><?php echo $birthdate;?>
<input name="passportno" type="text" class="textBoxDashed" id="passportno" value="" size="43" maxlength="14" value="<?php echo $passportno;?>">
input name="sc" type="submit" class="btn btn-primary" id="continue" value="Save and Continue" onclick="document.pressed=this.name">

Don't use extract. Also do some checking to see if the data is set. As for not getting the the data try $_POST['passportno'] and if you want to pull the values and put them back into the input boxes simply use <?php echo isset($_POST['passportno'])?$_POST['passportno']:'' ?> to return nothing if it is not defined.
Also you need to do add some protection to your inputs.
You can add protection by using $passportno = mysqli_real_escape_string($con, $passportno);

Related

How to stop submit being shown in the address bar?

How do i stop the submit being shown in the search bar? am i doing something wrong? if i use post method it works fine, but recently i've tried using the get method and it prints both submit and the textbox text in the bar, i tried removing the name="submit" and it didn't do anything when i clicked it. SO how do i stop submit being shown in the address bar?
at the moment it give me $submit=submit, :(
ty all.
<form action="" method="get">
<input type="text" name="website" placeholder="Website Name">
<input type="submit" name="submit">
</form>
<?php
if(isset($_GET['submit']))
{
if(!empty($_GET['website']))
{
//do stuffs here
}
else
{
//else echo out that nothing was entered.
echo "nothing entered";
}
}
?>
Just use:
!empty($_GET)
In your first if statement
So your code should look something like this:
<form action="" method="get">
<input type="text" name="website" placeholder="Website Name">
<input type="submit">
</form>
<?php
if(!empty($_GET)) {
if(!empty($_GET['website'])) {
//do stuffs here
} else {
//else echo out that nothing was entered.
echo "nothing entered";
}
}
?>
To remove the variables from the URL bar, you need to use the POST method instead of the GET method.
So, this form code should work for you:
<form action="" method="POST">
<input type="text" name="website" placeholder="Website Name">
<input type="submit" name="submit">
</form>
You'll also need to alter your PHP code to use $_POST instead of $_GET.
The downside to this is that it won't show the submitted website in the URL bar, meaning users can't bookmark a submitted form. However, this may also be intended.
Alternative Solution
You can remove the name from the submit, and use isset($_GET) to test whether the form was submitted instead.
Here is an example of how this could be done:
<form action="" method="get">
<input type="text" name="website" placeholder="Website Name">
<input type="submit">
</form>
<?php
if(isset($_GET) && count($_GET) > 0) {
if(!empty($_GET['website']))
{
//do stuffs here
} else {
//else echo out that nothing was entered.
echo "nothing entered";
}
}
?>

Post Form DATA to another page after PHP Validation

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> </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?
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.

Form submit to another page and then review or write to a file

I have a script that users can submit certain data, and after a submit they can "review" the output and go back to previous page or submit to it again and there is my proble, the submitted page: How can I submit it again to write it to a file?
form.html
<form method="post" action="vaihe2.php">
<input name="laskuttaja" type="text" value="Nimi" size="25">
<input name="submit" type="submit" value="Lähetä" />
</form>
vaihe2.php
<?
$laskuttaja = $_POST['laskuttaja'];
$data = '<B>'.$laskuttaja.'</b>';
echo $data;
?>
So how can I post that $data to next page(vaihe3.php) with submit and let the script write it to a file. I know how php write file works but the post to third page is not working.
If you wat to go back, the secret is in the value of the input.
<input name="laskuttaja" type="text" value="<?php echo(isset($_POST['laskuttaja'])?$_POST['laskuttaja']:"Nimi";?>" size="25"/>
To 'save' data to the next page use $_SESSIONs. They're simple to use. Just remember everywhere you use them, you must have session_start(); on LINE 1! Can't stress that enough!
$_SESSION['data']=$data;
on your third page:
echo$_SESSION['data'];
More on sessions here.
In vaihe2.php
<form method="post" action="vaihe3.php">
<?
$laskuttaja = $_POST['laskuttaja'];
$data = '<B>'.$laskuttaja.'</b>';
echo $data;
echo "<input name=\"laskuttaja\" type=\"hidden\" value=\"".$laskuttaja."\" size=\"25\">";
?>
<input name="submit" type="submit" value="anything" />
</form>
Here you are passing laskuttaja as hidden field and on post will be available to you in third page.
Now data flow as per your requirement. User fills data in form.html -> reviews on vaihe2 and confirms -> gets written in vaihe3.
Could you post the form conditionally back to itself until validated by checkbox? the action would change to "vaihe3.php" ?
<form method="post" action="<?php if ($_POST["valid"]==1) {echo 'vaihe3.php';} ?>">
<input name="laskuttaja" type="text" value="<?php if ($_POST['laskuttaja']!=='') {echo '$_POST[laskuttaja]'} else {echo 'Nimi';} ?>" size="25">
<?php if (isset ($_POST['laskuttaja') && $_POST['laskuttaja']!=="") {
echo 'Please Confirm your answers: <input name="valid" type="checkbox" value="1" />'; } ?>
<input name="submit" type="submit" value="Lähetä" />
</form>
Otherwise, the mention above about CURL would be another option. Or - since your using PHP anyways, you could write the values of form submission to a session array and make them available to all pages until you empty the array.

Need to carry check-box data from one page to another using PHP

I am new to php and am not quite clear on what to do to carry my information from the first page to the next and then submit it to my email when they are done filling out contact information.
I need the script to work as follows:
Step1: User clicks the input check-boxes for the field they want that is stored in an array
ex:
< input type="checkbox" name="Sound[]" value="item1" > item1
and clicks a button i have written as
< input type="image" name="Submit" class="" alt="" src="images/contact1.png" border="0" >
Step2: The information from the check-boxes they have clicked needs to be carried over to the next page where they will fill out their contact info. Name email phone etc.
ex:
<tr>
<td valign="top">
<label for="telephone">Telephone Number *</label>
</td>
<td valign="top">
<input type="text" name="telephone" maxlength="30" size="30" style="margin-bottom: 10px;">
</td>
</tr>
Step3. All of this information should be sent to my email upon button press for me to contact them :D
<tr>
<td colspan="2" style="text-align:center;">
<input type="image" name="Contact" class="contactbutton" alt="" src="images/contact.jpg"/>
</td>
</tr>
I can pull the information from my inputs but do not know how to carry to the next page!
Can I do it all in the same php script? or does each page need a different php script?
Please help!
Thanks Paul
you can do it with a form and send it to the page2.php. value will be stored in
$_POST['S'] for the checkbox
<form action="page2.php" method="post">
<input type="checkbox" name="S" value="item1" > item1
<input type="SUBMIT" >
</form>
------------------
page2.php
echo($_POST['S']); // will be item1
$_SESSION array is better. to use it you need to put session_start(); at start of
every page that will use your $_SESSION variable i.e
session_start();
if(isset($_POST['S'])){
$_SESSION['h'] = $_POST['S'];
echo($_SESSION['h']); } //output value in checkbox
?>
<html><body>
<form method="post">
<input type="checkbox" name="S" value="item1" > item1
<input type="SUBMIT" value="item1" >
Once this script is run you can accesS value in $_SESSION['h'] in other pages.
the data will be deleted when you close browser.
----------------------------------
page2.php
<?php
session_start();
if(isset($_SESSION['h'])){ //check if $_SESSION['h'] has been set a value
echo $_SESSION['h']; //output value stored in var
}
?>
You will ultimately still require the use of POST data to get the checkbox status from your page.
Page 1:
<?php
session_start();
// If postdata is received then redirect to next page
if(isset($_POST['Sound'])) {
$_SESSION['Sound'] = $_POST['Sound'];
header('Location: http://www.example.com/page2.php');
exit;
}
?>
<form method="post" action="page1.php">
Sound? <input type="checkbox" name="Sound" value="item1"><br>
<input type="submit">
</form>
Page 2:
<?php
session_start();
// If postdata is received then redirect to next page
if(isset($_POST['telephone']) && isset($_POST['email'])) {
$_SESSION['telephone'] = $_POST['telephone'];
$_SESSION['email'] = $_POST['email'];
header('Location: http://www.example.com/page3.php');
exit;
}
?>
<form method="post" action="page2.php">
<!-- If you want to output the previously saved data in a disabled item -->
Sound? <input type="checkbox" name="Sound" value="item1" disabled="disabled" <?php if($_SESSION['Sound'] == 'Yes') echo('checked="checked"'); ?>>
Telephone: <input type="text" name="telephone" value=""><br>
Email: <input type="email" name="email" value=""><br>
<input type="submit">
</form>
And so on and so forth for your next pages
This does not include the code for generating the e-mail via PHP but is intend to show you how you can take the form input/checkbox selections and store there values to a SESSION ARRAY. Note that in this example: The form is submitting to itself by leaving the action="" blank, but normal would submit to a external PHP file for parsing/handling.
Also, im choosing to create a random number to represent the visitor to the form if not specifically set by $_POST['user']
<?php session_start();
if (!isset($_SESSION['user'])) {$_SESSION['user']=rand(10,700);}
if (isset($_POST['user'])) {$id=$_POST['user'];} else {$id=$_SESSION['user'];}
?>
<form action="" method="post">
Sound 1:<input name="cb1" type="checkbox" value="sound1"><br>
Sound 2:<input name="cb2" type="checkbox" value="sound2"><br>
Sound 3:<input name="cb3" type="checkbox" value="sound3"><br>
<input type="submit" name="submit" value="submit"><br><br>
<?php
if (isset($_POST['submit']) && $_POST!=="") {
foreach($_POST as $key => $value) {
$_SESSION['visitor']['sounds'][$id]=array(
'selects'=>$_POST['cb1'].",".$_POST['cb2'].",".$_POST['cb3']
);
};
echo "For user ID:".$id." We echo the comma delimited stored SESSION array: ".$_SESSION['visitor']['sounds'][$id] ['selects'];
echo "<br><br>";
// Option 2 Explodes the comma delimited ['selects'] field to handle each choice seperately
$choice = explode(",",$_SESSION['visitor']['sounds'][$id] ['selects']);
echo "For an alternative, we EXPLODE the stored 'selects' field of the SESSION ARRAY and can then echo each out seperately"."<br><br>";
echo "User ".$id." Option 1 value was: ".$choice[0]."<br>";
echo "User ".$id." Option 2 value was: ".$choice[1]."<br>";
echo "User ".$id." Option 3 value was: ".$choice[2]."<br>";
echo "<br><br>";
echo "A last example we loop through the EXPLODED values and echo only those that were selected (ie: had a value)"."<br>";
foreach ($choice as $key => $value ) { if ($value!=="") {echo "Selection: ".$value."<br>";} }
}
?>

to call same form many times depending upon a value enterd by a user

I am now developing a travel agency website, in this site when the user reserves a trip he/she have to enter trip related details. This information is collected in a forma. The user also enters the number of people who are travelling.
My question is, how do I gather the same information for everyone who is travelling? Basically I need the form to be generated many times depending upon the number of people of same family ,, so I can capture all their data. How do I do this?
well there are my Code;plllz help me am so confused and tried lots of things to solve it;
thanks in advance
<form action = "insertpassenger.php" method = "POST">
<center>Enter all the information below</center>
<?php for ($i=0;$i<$pplno;$i++) : ?>
people<?php echo $i+1 ; ?>
<input type="text" name="cpr" size="9" value="<?php echo $cpr;?>" maxlength="9">CPR
<input type="text" name="pplno" size="30" maxlength="25">Number Of People
<input type="text" name="gcpr" size="9" maxlength="9">dad CPR
<input type="reset" value="clear" name="clear">
<input type="submit" value="join" name="join">
<?php endfor; ?>
</form>
Your code jumps in and out PHP rather a lot.
Just declare the item names as array entries, either with implicit or explicit numbering:
for ($i=0;$i<$pplno;$i++) : ?>
<input type="text" name="cpr[]" size="9" value="<?php echo $cpr[$i];?>" maxlength="9">CPR
<input type="text" name="pplno[]" size="30" maxlength="25">Number Of People
<input type="text" name="gcpr[]" size="9" maxlength="9">dad CPR
<input type="reset" value="clear[]" name="clear">
<input type="submit" value="join[]" name="join">
<?php endfor;
or...
for ($i=0;$i<$pplno;$i++) {
print "<input type=\"text\" name=\"cpr[$i]\" size=\"9\" value=\"$cpr[$i]\" maxlength="9">CPR";
....
}
I would personally use a session variable and count down how many times the form needs to be completed. Unfortunately that would cause to page to reload after each form entry, but this allows you to have as many amount of forms as your user requests, without a screen scrolling down a few pages to create all the forms on one page.
At the start of your code before you displaying anything to the browser :
<?php
session_start ();
?>
And where you receive your count for looping:
<?php
if (!isset($_SESSION['yourAppName']))
}
$_SESSION['yourAppName'] = $pplno;
} else {
$_SESSION['yourAppName']--;
}
if ($_SESSION['yourAppName'] > 0) {
?>
<form action=''>
<input type="text" name="cpr" size="9" value="<?php echo $cpr;?>" maxlength="9">CPR
<input type="text" name="pplno" size="30" maxlength="25">Number Of People
<input type="text" name="gcpr" size="9" maxlength="9">dad CPR
<input type="reset" value="clear" name="clear">
<input type="submit" value="join" name="join">
<input type="submit" value="Proceed">
</form>
<?php
} else {
// code when all forms are filled in
}
?>
remember to have your form return to the same page. This code is only to guide you, don't expect it to work without some editing. :)

Categories