Add data to $_POST and submit to another page - php

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.

Related

How do i make form variable to display on all my site pages?

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'];

How do I use get method to input values to form field with PHP

I have the following code snippet of my fields I have in my form:
<input id="username" type="text" placeholder="E-mail Address" value="" name="username"></input>
This is what I have in my input field. Is there anybody who will tell me how to get input values to the field using a url? e.g https://mysite?username=ken and it will show "ken" in the input field?
In your HTML, add the input field like this:
<input type="text" name="username" value="<?php echo htmlspecialchars($_GET['username']); ?>" />
Basically, the value attribute of the text field needs to be set to:
<?php echo $_GET['username']; ?>
The code right above this is how you would output a get variable in php whether you are putting it in a text field or not.
To access get variables, always use:
$_GET['variable_name'];
Then you can assign it to variables or pass it as a function parameter.
**However, I strongly do not recommend passing sensitive information like usernames and passwords through GET variables. **
First off, users could change the URL hence changing the variable. They could also accidentally share the URL with someone and that could give someone else access to their account. I would recommend that you create a cookie on their machine that is set to a random ID, and then in a MySQL database, associate that ID with a username so that you know the user can't accidentally share their account or change their username through the URL.
You can do it like this, make an isset in your php form input that can catch your ken variable from GET post, never forget the method="get" inside the form tag and if you are planning on submitting on the same page you can use action="<?php echo $_SERVER['PHP_SELF']; ?>" inside your form tag.. hope this helps, here is your code.. ^_^
<form id="form" name="form" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<p>Input</p>
<div>
<input type="text" name="nameoffield" id="nameoffield" value="<?php if(isset($_GET['ken'])){echo $_GET['ken'];} ?>"> <br />
</div>
</fieldset>
<div>
<button type="submit" value="Submit" name="submit">
Submit
</button>
</div>
</form>
The <input> tag and other fields of form must be in a <form>tag.
<form action = "https://mysite" method = "get">
<input id = "username" type = "text" placeholder = "E-mail Address" name = "username" value = "<?php echo $_GET['username']; ?>" />
</form>
In the above code, form tag specifies that the method of submission is 'GET' and the action that will be taken on submission is URL to which your form data will be submitted and processed.
Now assuming that your form is in the same URL to which you are submitting your form, you will get the GET value in the same page (or URL), so in the input text field set the value which is obtained by GET method and use it.
All the GET key-value pairs are stored in an associative array $_GET from which you can access the value of a given key by using that as the index of the array.
e.g. Key is username in this case, so to get the value of the username, $_GET['username'] was used.

PHP - store input variable for static reuse

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.

How can I get/extract the name value of a submit button?

When I create i form - I do something like this:
<form name="form-name" method="post" action="?<?=$_SERVER['QUERY_STRING']?>">
[...some elements...]
<input type="submit" name="form-name" value="button">
</form>
Now I need to get the value of the name="" of the submit button, and not the actual value="".
In this case : "form-name".
And here's why:
When I submit a form; I write the action to database - and therefor need the name of the form submitted.
I know I can just have a hidden field with the form name. But I would like to make it simpler by just extracting the name from the submit button because I have a couple of other hidden form elements that I need to add on every single form I create to make my template system work.
And no javascript...
So, let's say your HTML form is this:
<form name="form-name" method="post" action="">
<input type="submit" name="form-name" value="button">
</form>
And you want to get what is inside name="form-name" in this case the form-name
Well, then in the PHP side you can, treat the $_POST global as associative array, and extract the key from it like this:
<?php
if(isset($_POST)){
foreach($_POST as $key=>$each){
echo $key; // this will output "form-name"
}
}
I might have come up with a solution to my question...
Here's a example form:
<form name="vehicle-vinNr" method="post" action="?<?=$_SERVER['QUERY_STRING']?>">
<input type="hidden" name="hello" value="world" readonly>
<input type="text" name="element">
<input type="submit" name="vehicle-vinNr" value="send">
</form>
First I need to extract and place the element-names into a new array:
<?php
if ($_POST){
foreach($_POST as $_FORM_ELEMENT_name => $_FORM_ELEMENT_value){
$_FORM_ELEMENT_names[] = $_FORM_ELEMENT_name;
}
}
?>
In this case the array now contains:
hello
element
vehicle-vinNr
If the submit-button is, and always is, the last element in the form - this would work:
$_FORM_name = end($_FORM_ELEMENT_names); // vehicle-vinNr
But sometimes the submit-button is not the last element, so I needed to make a change:
If I always start the name of the submit-button with submit_ - e.g. submit__vehicle-vinNr or with multiple submit buttons for different actions like submit_update__vehicle-vinNr/submit_delete_vehicle-vinNr I can just do this:
if ($_POST){
foreach($_POST as $_FORM_ELEMENT_name => $_FORM_ELEMENT_value){
if(strstr($_FORM_ELEMENT_name,'submit_')){
$_FORM_ELEMENT_submit_name = explode('__',$_FORM_ELEMENT_name);
$_FORM_name = $_FORM_ELEMENT_submit_name[1]; // vehicle-vinNr
}
}
}
This is the solution I came up with - any thoughts?

How to get url id from another page by form action page

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']; ?>.

Categories