There are some inputs, and there is a function. The function requires these inputs, and the inputs are user-given. But, the buttons that fire the function and the input submission form are two different buttons. So, when the user presses "submit" to store his variables, the variables are stored fine. But, when he presses the "calculate" button (which fires the function), php says "undefined index" because it reads the $_POST of that input again and again.
If I disable register_globals, it does not show 'undefined index' but these values are 0 again.
If I use another file just to store these values and then redirect back to the page where the function button is, there is a redirect loop, require_once does not work.
What is the way to store the inputs in such way that they can be used again and again in functions and whatsoever? No databases, I need a way to store them in variables.
edit: the form: <label for="asdf">enter value:</label> <input type="text" id="asdf" name="asdf" value="<?php echo $asdf;?>" />
storing the value:
$asdf=$_POST['asdf'];
then I need to write $asdf in the function with the updated value that the user gave through the html form. How to do it? Cannot be much simpler
I would just store them in the session. That way they, they can be used across php scripts, but are not stored in the long-term. Here's an example:
form.php
<?php
session_start();
?>
<html>
<body>
<form action="store.php">
<input type="text" name="x" value="<?php echo $_SESSION['x'] ?>">
<input type="text" name="y" value="<?php echo $_SESSION['y'] ?>">
<input type="submit" value="Submit">
</form>
<form action="calculate.php">
<input type="submit" value="Submit">
</form>
</body>
</html>
store.php
<?php
// Start the session
session_start();
$_SESSION["x"] = $_POST['x']; // substitute your input here
$_SESSION["y"] = $_POST['y']; // substitute your input here
?>
calculate.php
<?php
// Start the session
session_start();
$result = $_SESSION["x"] * $_SESSION["y"];
echo $result;
?>
There is no way to store them in variables. Every request to your server is a new request.
You could store the variables in a cookie/session or give them back after pushing the first button and store them in a hidden field in your html form. Or store them in a file on your server.
Related
This is my form:
<form id="form1" name="form1" method="post" action="tracking.php">
<label>
<input type="text" name="trckno_trk" id="trckno_trk" />
</label>
<label>
<input type="submit" name="button" id="button" value="Submit" />
</label>
</form>
When i submit my form, the submitted form variable displays well on the "tracking.php" page using <?php echo $_POST['trckno_trk']; ?>.
But when i click on other pages in the site, it doesn't seem to display. That mean that the form variable echo $_POST['trckno_trk'] displays only on one page but does not display on any other page.
Please, how can i get it to display on every other page on my site.
Try this
<?php
// Start the session
session_start();
?>
Then you html
<form id="form1" name="form1" method="post" action="tracking.php">
<label>
<input type="text" name="trckno_trk" id="trckno_trk" />
</label>
<label>
<input type="submit" name="button" id="button" value="Submit" />
</label>
</form>
Then you should save the variable in your Session
<?php echo $_POST['trckno_trk'];
$_SESSION["trckno_trk"] = $_POST['trckno_trk'];
?>
Now you can use this Session to display on other pages.
Form submited data are available only for page you specified in action parameter. They are not stored in any way. You need to save them for example in database or SESSION variables and retrive them again when accessing other pages.
Form parameters are accesible only when you post them, to use them later you will need to save them somewhere.
If you'd like to save the value & display it everywhere for all clients, you will need to store it in a database (e.g. MySQL), fetch the value and print it
If you'd like it to be unique for each client, you could use cookies, for example:
setcookie('cookiename',
'the value',
time()+86400 /* seconds until it expires */,
'/' /* On what pages do you want to use it, '/' means all pages*/,
);
The code above saves the cookie, to print its value:
echo $_COOKIE['cookiename'];
Another option is to save it in the session, but it will expire after a short time
session_start(); // put this on the start of every page to start the session
$_SESSION['name'] = 'value'; // save a value in the session
echo $_SESSION['name']; // print it
I suggest you use $_SESSION[] to store your POSTED value, this way you will be able to access it on any other page.
So on your tracking.php you can have code like this
session_start();
$_SESSION['trckno_trk']=$_POST['trckno_trk'];
then on other pages you can access the variable using
session_start();
echo $_SESSION['trckno_trk'];
I'm trying to add a value to $_POST data while it gets submitted to the target page as follows:
post.php
<?php $_POST['field1'] = "Value1";?>
<html>
<head>
</head>
<body>
<form method="post" action="catch.php">
<input name="field2" type="text"/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
catch.php
<?php
foreach ($_POST as $key => $value) {
echo $key . " : ". $value;
echo "<br/>";
}
?>
but I cannot catch 'field1' on the other end. I don't want to use a hidden input field. How can I achieve that?
Thanks!
When you send the form, the $_POST data is reset and assumes only the inputs inside the form and a possible query string you may have appended to form action.
The best way to accomplish what you want is using hidden field but since you dont want it, you can append a query string to your form action:
<form method="post" action="catch.php?field1=Value1">
You're not submitting field1 anywhere. What happens is this:
post.php generates a HTML page (one that doesn't contain any reference to field1)
the user's browser renders the page
on submit, only the elements inside the form are submitted
catch.php receives the elements submitted above.
In other words, you need to get that value into your form:
<form method="post" action="catch.php">
<input name="field2" type="text"/>
<input name="field1" type="hidden" value="<?php echo htmlspecialchars($_POST['field1']) ?>"/>
<input type="submit" value="Submit" />
</form>
There is no other way to get the value into your POST data, if it's not present in the form. What you could do as a workaround is store the data in GET (size limit), session (concurrency issues - what happens when the user has two tabs open, each with different session data?), or cookies (size limit AND concurrency issues).
You can't do it this way. If you want to send the data you're trying to add to the POST there only through the users form, you are forced to also store it somewhere client side (e.g. a hidden field or a cookie). What you're doing right now is setting some value into the POST variable, but it gets overridden by the users form post (or rather the $_POST variable you're using after the form post is another instance).
What you could do instead to keep it serverside is save the value in the variable to the session of the user, then in the form post action server side get the value again (given the fact that you're using sessions). Lastly you could just store it in some table in a database, though I wouldn't do this.
Since $_POST are data sent by post method to script, you can not use it for another request directly. You need to compose and send another post request. The easiest way for you will be to use hidden input field/s.
Or you can choose another approach to make http post request, for example curl methods.
If you don't need data to be given by post method, you can save it in session, for example.
try this:
in post.php
<?php $_SESSION['field1'] = "Value1";?>
<html>
<head>
</head>
<body>
<form method="post" action="catch.php">
<input name="field2" type="text"/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
in catch.php
<?php
if(isset($_SESSION['field1']))
{
$_POST['field1'] = $_SESSION['field1'];
unset($_SESSION['field1']);
}
foreach ($_POST as $key => $value) {
echo $key . " : ". $value;
echo "<br/>";
}
?>
Make sure you have started the session.
Note: you must use hidden elements or query string as other user suggested.
I have a value coming from another form in the same page called $_POST['serial']. And i want to use this value to run a query in another form but after I submit the second form nothing happened and the query not running.
<?php
if (isset($_POST['serial'])) {
$serial = $_POST['serial'];
?>
<form action="" method="post">
<button type="submit" name="submit">Click to use</button>
</form>
<?php
if (isset($_POST['submit'])) {
$query = mysql_query("UPDATE table_name SET status = 'inactive' WHERE serial = '$serial'");
}
}
?>
To pass the variable along you would create a hidden input on your second form to contain the value:
<?php
// check and clean up the passed variable
$serial = isset($_POST['serial']) ? htmlspecialchars($_POST['serial']) : '';
?>
<form action="" method="post">
<input type="hidden" name="serial" value="<?php echo $serial; ?>" />
<button type="submit" name="submit">Click to use</button>
</form>
For Safety's Sake
Your script is at risk for SQL Injection Attacks.
If you can, you should stop using mysql_* functions. These extensions have been removed in PHP 7. Learn about prepared statements for PDO and MySQLi and consider using PDO, it's really not hard.
Additional Thoughts
If you're planning to do a two-step form you'll likely want to place all of the data processing outside of the form page, in a separate PHP file. With the limited code that you have shown I fear that we will miss something in our answers which will lead you to additional questions because your code still isn't working as you would expect.
A button needs a name and a value to be successful. Your button doesn't have a value so $_POST['submit'] will be undefined.
Add a value attribute to your <button> element.
After you do that, $serial will be undefined because your form doesn't submit that.
You need to include it in your form too:
<input type="hidden" name="serial" value="<?php echo htmlspecialchars($serial); ?>">
my page receives data which i retrieve with $_post. I display some data and at the bottom of page my button has to save data to mysql. I could submit form to next page, but how do i access the data that I have retrieved with post then? Lets say i have following code (in reality alot more variables ..):
<?php
$v= $_POST["something"];
echo $v;
echo "Is the following information correct? //this would be at the bottom of the page with the buttons
?>
<input type="button" value="submit data" name="addtosql">
You can do it in two methods:
1) You can save the POST variable in a hidden field.
<input type="hidden" name="somevalue" value="<?php if(isset($_POST["something"])) echo $_POST["something"];?>" >
The hidden value also will get passed to the action page on FORM submission. In that page you can access this value using
echo $_POST['somevalue'];
2) Use SESSION
You can store the value in SESSION and can access in any other page.
$v= $_POST["something"];
session_start();
$_SESSION['somevalue']=$v;
and in next page access SESSION variable using,
session_start();
if(isset($_SESSION['somevalue']))
echo $_SESSION['somevalue'];
Take a look. Below every thing should be on single php page
// first create a function
function getValue($key){
if(isset($_POST[$key]))
return $_POST[$key];
else
return "";
}
// process your form here
if(isset($_POST['first_name']){
// do your sql stuff here.
}
// now in html
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="first_name" value="<?php echo getValue("first_name"); ?>" />
<input type="submit" />
</form>
i have a page where i am getting the ques id and insert it in the database for that i am doing
url: */faq/faq_question_sol.php?ques= 62*
this ( $selected_ques= ($_GET['ques']); ) is working properly in the *faq_question_sol.php* but the *answer_submit_process.php* does not recognize it
my form
<form id="post-form" class="post-form" method="POST" action="answer_submit_process.php">
<input id="submit-button" type="submit" tabindex="120" name="submitbutton" value="Post Your Answer" />
</form>
and the *answer_submit_process.php* is
if(isset($_POST['submitbutton'])){
$userid = $_SESSION['userid']; // i have already started the session
$selected_ques= ($_GET['ques']);
$content = $_POST["content"] ;
$query="INSERT INTO `formanswer`( `user_id`,`questionid`,`content` ) VALUES ('{$userid}','{$selected_ques}','{$my_html}' ) ";
$result=mysql_query($query);
}
Quickest solution would be saving the value of $_GET['ques'] on a hidden field of the form and thus make it accessible in answer_submit_process.php.
Something like this:
if (isset($_GET['ques'])){
echo '<input type="hidden" name="ques" value="'.$_GET['ques'].'">';
}
And in answer_submit_process page the value could easily accessed by $_POST['ques']..
If you are sending via a form POST, then the variable from which you can get data is $_POST instead of $_GET.
Anyway, i wasn´t able to find any field relating to the ques variable on your form, where are they?
Add <input type="hidden" name="ques" value="<?php echo $_GET['ques'] ?>"/> to your form to temporarily store the variable, and then use the variable $_POST['ques'] in place of $_GET['ques'] in the processing page.
Alternatively, you could change the form action to answer_submit_process.php?ques=<?php echo $_GET['ques']; ?>.