I need to save my submit form data like:Name for two pages...
For some reason the $_POST only saves data for the "action" page, but cannot be retrived after the action page.
Here's my code:
HTML (form):
<html>
<body> <form name="input" action="staff.php" method="post">
Username: <input type="text" name="Name">
<input type="submit" value="Submit">
</form>
</body>
</html>
Here's the next page after submiting and it works... (staff.php)
<html>
<?php
session_start();
echo "You have choosen". $_POST['Name']; // it shows what you've choosen...
?>
<form name="input" action="staff2.php" method="post">
Age: <input type="text" name="Age">
<input type="submit" value="Submit">
</form>
</html>
Ok and after age submiting Name and Age stop working... (staff2.php)
Here's the code:
<?php
session_start();
echo "You have choosen".
$_POST['Name']; //it does't show Name.. Please help!
$_POST['Age']; // it doesnt't show this either..
?>
Obviously, there is nothing wrong on the first page. So don't change anything.
The second page. The post works. Then add a hidden input to preserve it and carry it on the next one:
<?php
echo "You have chosen: ". $_POST['Name']; // it shows what you've choosen...
?>
<form name="input" action="staff2.php" method="post">
Age: <input type="text" name="Age">
<input type="hidden" name="Name" value="<?php echo $_POST['Name']; ?>" /> <!-- this one -->
<input type="submit" value="Submit">
</form>
On the third and final page. Properly concatenate the variables:
echo 'You have chosen: <br/>';
echo $_POST['Name'] . '<br/>'; // this should carry the hidden input you set on the last page
echo $_POST['Age'];
//^^ you forgot the echo
as you have a session running pass them as session variables.
$_SESSION['name'] = $_POST['name'];
Related
i want to display my first page data in third page without session or database.
here is my page1.php form.
<form method="post" action="page1">
name<input type="text" name="name">
password<input type="password" name="pass">
retype password<input type="password">
<input type="submit" name="submit" value="Submit"/>
</form>
it post the value to page two. and my page2.php contain one button. if i click that button means it goes to page2
<form method="post" action="page3">
<?php
if(isset($_POST["submit"]))
{
$name= $_POST["name"];
$pass= $_POST["pass"];
}
?>
<input type="submit" name="submit1" value="submit">
</form>
now i need to display fist page data to here in page3.php
<?php
if(isset($_POST["submit1"]))
{
$name= $_POST["name"];
$pass= $_POST["pass"];
echo $name;
echo $pass;
}
please any one give idea to make this thing to work.
thank you.
We can maintain the data without using session variable or database storage, using the "input type=hidden" property like this
<form method="post" action="page3">
<?php
if(isset($_POST["submit"]))
{
$name= $_POST["name"];
$pass= $_POST["pass"];
}
?>
<input type="hidden" name="name" value="<?php echo $name; ?>">
<input type="hidden" name="pass" value="<?php echo $pass; ?>">
<input type="submit" name="submit1" value="submit">
</form>
And you can retrieve this values in your 3rd form using post variables like this..
<?php
if(isset($_POST["submit1"]))
{
$name= $_POST["name"];
$pass= $_POST["pass"];
echo $name;
echo $pass;
}
?>
Although I personally wouldn't recommend this method for a sensitive data as passwords, as the users can see the values in html form if they need to, using inspect element.
I have PHP code that do the following things
1-get some request parameters from parent page
2-send post request for it self and validate data
3-redirect the combined data to the next page
here is sample snippet
I am having difficult time fixing my messy code... pleas help me!
<?php
//on page load---from parent page
if ($_SERVER["REQUEST_METHOD"] == "GET") {
$room_number=$_GET['room_number'];
}
//on submit
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$lastname = $_POST["lastname"];
//do some stuff... validation
if($valid){
// if valide redirect to ...
header("Location: RegisterUser.php?name=".$name."&lastname=".$lastname."& roomnumber=".$room_number."");
exit();
}
}
?>
<form action='<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>' method='post' >
<input type="text" name="name">
<input type="text" name="lastname">
</form>
the problem i am having is at the redirection $room_number is empty always
how can i fix it??
I know i am doing a lot of stuff in single file that is because i am very beginner.
it would be great if someone can suggest me better way to so such stuff.
Thank you in advanced!
You could use hidden input for room_number input.
<?php
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$room_number = $_POST['room_number'];
header("Location: RegisterUser.php?name=".$name."&lastname=".$lastname."&roomnumber=".$room_number);
exit();
}
?>
<form action='<?php echo $_SERVER["PHP_SELF"];?>' method='post'>
<input type="text" name="name">
<input type="text" name="lastname">
<input type="hidden" name="room_number" value="<?php echo $room_number; ?>" />
<input type="submit" name="submit" value="Submit" />
</form>
And then use the above code in your file. It may help you. And Why are you redirecting those variables to another register page. Just use them in itself to insert into database or anything else what you want.
Add your $room_number in your form as a hidden input field so that you can pass it through as a post variable.
<input type="hidden" name="room_number" value="<?php echo $room_number; ?>" />
I can't really use PHP and from what I've seen in tutorials this should work, but it doesn't:
<html>
<head></head>
<body>
<form>
<input type='text' name="name" value='myName'>
</form>
<p>
<?php
$name = $_POST['name'];
echo $name
?>
</p>
</body>
</html>
Is there a reason I can't get the value of name?
Sorry for asking such a simple question...
here is the fiddle http://jsfiddle.net/DCmu5/1/, so, please try what you said and send it to me only when it works before answering
PHP is a server-side language, so you'll need to submit the form in order to access its variables.
Since you didn't specify a method, GET is assumed, so you'll need the $_GET super-global:
echo $_GET['name'];
It's probably better though, to use $_POST (since it'll avoid the values being passed in the URL directly. As such, you can add method attribute to your <form> element as follows:
<form method="post">
<input type='text' name="name" value="myName" />
<input type="submit" name="go" value="Submit" />
</form>
Notice I also added a submit button for good measure, but this isn't required. Simply hitting return inside the textbox would submit the form.
Well, you have to POST your form to get the value of $_POST variables. Add this to your <form>
<form action="yourpagename.php" method="post">
<input type='text' name="name" value='myName'>
<button type="submit">Submit</button>
</form>
Click button and whatever is typed in your field will show up.
<html>
<body>
<form method="post" action="1.php">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_POST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>
</body>
</html>
try this
<?php
if(isset($_REQUEST['name'])){
$name = $_REQUEST['name'];
echo $name;
}
?>
My question is how do I call input value of user selected which has show by php code from another php.
Normal simple way we get the input this way
$calendar_id_val = $_GET['calendar_id'];
and now it is not working:
For example Show.php, I have one form which show the values from Database and show the result with php variable with While Loop.
<input name="calendar_id" value="<?php echo $calendar_id;?>">
and when user is submit that form I will carry these user selected value and perform insert.php
While you are doing echo in the input use echo $calendar_id_val
You should use form like,
<form action="insert.php" method="get">
<input type="text" name="calendar_id" value="<?php echo $calendar_id;?>"/>
<input type="submit" name="submit_id" value="Insert"/>
</form>
Insert.php
echo $calendar_id_val = $_GET['calendar_id'];
Does this answer your question?
<form action="insert.php" method="GET">
<input name="calendar_id" value="<?php echo $calendar_id;?>">
</form>
If you want to redirect the user back to show.php, add this to the end of your insert.php script
header('Location: show.php');
exit();
I suggest $_POST var like this:
<form action="insert.php" method="POST">
<input type="text" name="calendar_id" value="<?php echo $calendar_id;?>" />
<input type="submit" name="submit" value="Submit" />
</form>
insert.php file:
echo $calendar_id = $_POST['calendar_id'];
I have a webpage that uses php and has a bunch of input fields in which a user enters some data and I turn the input into an SQL statement to query some database. I successfully parse the input fields and put the SQL statement together but when they click the "submit" button, all the input fields get cleared. How can I make it so these input fields don't get erase every time the user clicks "submit"?
Store them in local variables like
<?php
$name = '';
$last = '';
if(isset($_POST['post_me'])) {
$name = $_POST['post_me'];
$last = $_POST['last'];
//Process further
}
?>
<form method="post">
<input type="text" name="name" value="<?php echo $name; ?>" />
<input type="text" name="last" value="<?php echo $last; ?>" />
<input type="submit" name="post_me" />
</form>
Something like this should work.
<?
if(!isset($_POST['stackoverflow'])){
$txt = "";
} else {
$txt = $_POST['stackoverflow'];
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="" method="post">
<input type="text" name="stackoverflow" value="<?= $txt ?>">
</form>
</body>
</html>
You need to check if the POST values (assuming you're using POST) are already set when the page loads (because they have already been submitted). If they are set, echo their contents into the value attributes of the form fields, or set checked=checked etc. depending on the field types.
This has always worked for me:
<input type="text" name="somename" value="<?php echo htmlspecialchars($_POST['somename']); ?>">
The key is to store the post values in session and display it using value tag.
Example:
HTML:
<form action="index.php" method="post">
<input type="text" name="name" placeholder="First Name"
value="<?php
if (isset($_SESSION['name'])) {
echo $_SESSION['name'];
}?>" >
<input type="submit" name="submit">
</form>
PHP:
session_start();
if (isset($_POST['submit'])) {
$name=$_POST['name']);
$_SESSION['name']=$name;
}
<?php
if(isset($_POST['submit'])) {
$decimal = $_POST['decimal'];
?>
<form action="" method="post">
<input type="number" name="decimal" required value="<?php echo (isset($decimal)) ? $decimal: ''?>">
</form>
Just use your $_POST['decimal'] into your input value
Have you used sessions and Cookies in PHP??
You can store the values in session on form submit before updating in Database for a user, then check if there is a session for user and value is set for field output the value else output null.
You just have to make sure the HTML input value has a php echo statement of the appropriate php variable for that field.
Simplest answer
`<form method="post" onsubmit="return false">`
coding your input with PHP is dangerous even if with htmlentities - hope this helps