Correct Way to Use $_POST - php

If I have three PHP pages, lets call them one.php, two.php, and three.php, and I use something like this on the first page:
<form name = "one" action = "two.php" onsubmit="return validateForm();" method ="post">
I understand that I can now use the variables I passed alone from one.php in two.php...
My question is, how would I use the variables from one.php in three.php?
I keep getting "undefined index" errors every time I try to call them as I would in two.php.
Thank you very much for your help!
Edit: I should have mentioned that I am using Sessions as well.
On the second page, I do something like this:
$_SESSION['name'] = $_POST['name'];
and it lets me pass on the name gathered from that first page to the second one, but when I attempt to do this on the third page, it just says "undefined index".

Instead of using post, you would need to use $_SESSION
so for example:
one.php
session_start();
$_SESSION["mypostvalue"] = $_POST["mypostvalue"];
$_SESSION["mypostvalue1"] = $_POST["mypostvalue1"];
three.php
session_start();
echo $_SESSION["mypostvalue"];
echo "<br />";
echo $_SESSION["mypostvalue1"];
Then once you are done with the session data, you need to get rid of it like this:
session_start();
$_SESSION = null;
session_destory();
Note Only use session_start() once per page, and before any data is sent to the browser such as html or white space.

I understand that I can now use the variables I passed alone from one.php in two.php...
Not as such.
Successful form controls in the form will submit their values to two.php. You can then access them via $_POST['name_of_control'].
If you generate form controls (possibly of type hidden) with values created from PHP variables (making sure to escape them to defend against XSS attacks) then they will appear in the form and be submitted.
My question is, how would I use the variables from one.php in three.php?
That depends on how the browser gets to three.php. You could generate another form and put the values in hidden inputs again.

$_POST is per request You should pass them to the third file using $_SESSION or $_GET.

Just make sure your input elements are named correctly. If you want to use $_POST["myvar"] in two.php, then the name attribute in one.php needs to be "myvar".
<form action="two.php" method="post">
<input type='text' value='test myvar' name='myvar' />
<input type='submit' value='go to 2' />
</form>
To use the variables from one.php in three.php, just pass them through as hidden inputs. That is, echo them back to the form in two.php:
<form action="three.php" method="post">
<input type='text' value='test var 2' name='myvar2' />
<?php echo "<input type='hidden' name='myvar' value='" . $_POST["myvar"] . "' />"; ?>
<input type='submit' value='go to 3' />
</form>

Related

Dynamically change html content based on form input after page refresh

I am currently attempting to create a simple test web page which takes in a user's input of their name through a form and then after the form is submitted, the page is refreshed and the page now ONLY displays "Hello NAME".
Now, after going to this specific URL, you are no longer able to enter the name again and you only see the "Hello NAME" phrase. Also, the page with the form and the final phrase must be reachable by the same URL - they must be the same page.
Any help would be greatly appreciated!
So from what I understand you want to get a value from $_GET or $_POST.
After it goes to the other url you can set the NAME set by the user on a $_SESSION variable, you just need to start the session at the start of the page session_start() and then add the name into the variable $_SESSION['name'] = $_POST['name'] or $_SESSION['name'] = $_GET['name']; depends on what you want to do or use. And don't forget about htmlentities() and strip_tags() to "sanitize" the variable or to make it more secure to use.
After that you should print something like
echo "Hello ". $_SESSION['name']; or anything like that.
Here's a very basic example of how to do that using a session and post/redirect/get.
<?php
session_start(); // be sure to start the session first
// **don't output anything before this**
// if the name has been added to the session, echo the hello message and exit the script
// (the form won't be displayed)
if (isset($_SESSION['name'])) {
echo 'Hello ' . $_SESSION['name'];
exit;
}
// if the name has been posted, save it to the session and redirect to the same page
// (the form won't be displayed)
if (isset($_POST['name'])) {
$_SESSION['name'] = $_POST['name'];
header('Location: ' . $_SERVER['REQUEST_URI']);
exit;
}
// If neither of those things are true, go ahead and show the form.
?>
<form method="post">
<input type="text" name="name">
<input type="submit" value="Submit">
</form>
Using PHP Sessions is one solution, and another one that you can use is using the GET Parameters.
<form action="page2.php" method="get">
<input type="text" name="user_name">
<button type="submit">Submit</button>
</form>
In the example above, what the form will do is submit the entered value (from the user_name input) to access it on page2.php file (this is the form action attribute).
In your page2.php file, you can access the value from the form by using
<?php
/** $_GET['user_name'] is the name of your input in the form */
echo "Hello " . $_GET['user_name'];
?>
NOTE: $_GET parameters is not recommended when accessing sensitive data, as this value is displayed and can be edited by the end-user in the URL.

Get parameter through GET and submit via POST (PHP)

I have got an issue to ask related with GET/POST.
I am trying to make a simple blog with posts and comments of them.
From each post I have got on main page, I would like to add a comment form in a new page that enables to save the post´s index to have a control of the commentaries.
I get this index value through GET in the new page but when I submit the form via POST I lose the reference to the index.
I read that is not possible to use both methods at the same time and I would like to know how can I keep a parameter from the main page and store it with the rest of the values in the new form.
Thanks a lot,
BR
http://localhost/simple_blog_new_comment.php?postIndex=xx
<form action='simple_blog_new_comment.php' method='POST'>
Commentary:<br>
<textarea onfocus='clearContent(this)' cols='30' rows='5' name="txt_comment">Enter the text here...</textarea><br>
Author: <input type='text' name='txt_comment_author'><br>
<input type='submit' name='btn_comment_submit'><br><br>
</form>
I found a solution for this problem I would like to share in case someone have the same trouble.
Finally I get working my "Posts" and "Comments" databases fixing the variable reference problem using $_SESSION superglobal variable.
It works like this:
session_start(); // This allows the use of $_SESSION superglobal var
$_SESSION['index'] = $_GET['postIndex']; // Save the variable into $_SESSION
With this superglobal variable you can keep the index variable as a cookie for using it as long as you keep the session opened.
More related info here: http://php.net/manual/es/reserved.variables.session.php
Thanks again! :D
I am not sure if I understood you question. I suppose you want to get the parameter by URL and send it through a form. I think you should do the next.
<?php
$index=$_REQUEST["Index"];
?>
<form action='simple_blog_new_comment.php' method='POST'>
Commentary:<br>
<textarea onfocus='clearContent(this)' cols='30' rows='5' name="txt_comment">Enter the text here...</textarea><br>
Author: <input type='text' name='txt_comment_author'><br>
<?php echo "<input type=hidden name=num_index value=" . $index . ">"; ?>
<input type='submit' name='btn_comment_submit'><br><br>
</form>
In the simple_blog_new_comment.php you will need this if you want to get the value of num_index.
<?php
$kk=$_REQUEST["num_index"];
echo $kk;
?>
I think you are looking for something similar. I hope it would be useful.

How to Passing and show form variables from 1st page to 3rd page without session

I need to pass and show variable from page one to third page how do I make it?
I mean i have 3 pages (sign_up.php, personal.php, full_detail.php).
for Example:
In sign_up.php page i have username, email, ect,..
And personal.php page i have address,city,state, ect ,..
Finally i want to show 1st page values and 2nd page values in full_detail.php.
Note:
- i did one page to next.
- how can i to pass variable from 1st to second page and same value to 3rd page.
Thank you for advance
Using POST/GET
Do it like this- If you don't want to do it using session.
GET
Using Anchor Tag <a> on first page sign_up.php write this-
Personal Page
Use header function for simply redirecting the php page to another page like this-
header("Location: personal.php?var1=value1&var2=value2");
POST
Also you can use HTML Tag to send data from one page to another like this-
<form action="personal.php" method="post">
<input type="hidden" name="var1" value="value1"/>
<input type="hidden" name="var2" value="value2"/>
<input type="submit" value="Personal Page">
</form>
Now on 2nd page i.e., personal.php in your case.
Catch these values like-
In case of GET Method:
$variable1 = $_GET['var1'];
$variable2 = $_GET['var2'];
In case of POST Method:
$variable1 = $_POST['var1'];
$variable2 = $_POST['var2'];
You can either send a POST request to the server on each step and then save the data in the session variable like so:
<?php
session_start();
$_SESSION['my_field'] = $_POST['my_field'];
?>
Or, and I would recommend this, you use javascript or a framework like jQuery to hide and show certain parts of your form. That would mean that you have all 3 parts on one PHP page and you only post to the server once.
By POST or GET
You can submit the form from one to next
Submit 1st form from 1st page to second page
keep all the form data in hidden fields on second page and submit second form to third. with all the values from 1st page and second page
use all the form data from 1st and second page on third page
this was you wont be needing a session
You can use hidden input boxes. E.g., you send the first page's values using a POST request, you can add the values like this (within your <form>) to your second page:
<?php
foreach ($_POST as $key => $value)
echo '<input type="hidden" name="'.htmlentities($key).'" value="'.htmlentities($value).'">';
?>
So all values are added to your second page's form without showing them.
Be careful: If you are sending a password from page 1 to page 2, you shouldn't do it this way. In this case a session would be strictly recommended.
If you want form validation then do POST method and send your variables to desired page. But if you wants to send the variables from the page where they are posted to another then I know what that is cookies..
do this on both pages:
signup.php
<form action='full_detail.php' method='POST'>
<input type='text' name='username'>
<input type='email' name='email'>
<input type='password' name='password'>
<input type='submit' name='submit' value='Sign up'>
</form>
On personal.php
<form action='full_detail.php' method='POST'>
<input type='text' name='address'>
<input type='text' name='city'>
<input type='text' name='state'>
<input type='submit' name='submit1' value='Send'>
</form>
Now you can receive your variables on the third page... Echo them out...
On full_detail.php
<?php
if(isset($_POST['submit'] || isset($_POST['submit1']))
{
echo all of your variables the simplest way.. You can pass them through url too
}

POST values not appearing in recaptcha

I have this page which contains a form. The values are POSTed over to the next page(check_values.php) which manipulates these values. Now I want to add a captcha. but it requires you to redirect the form to a verify.php which seems to clear out the posted variables when i redirect to check_values.php. I dont want to use session variables in this case. Are there any other method(s) to accomplish this.
You can use query strings too
In your verify.php page, add a query string to the location i.e.,
header('Location: check_values.php?captcha='.$postedValue.'');
Then in check_values.php, you can use $_GET to capture that value.
$value = $_GET['captcha'];
But make sure you sanitize all data coming from the query string
Yep, POST won't persist after a page request. You'll have to resend them somehow.
If you want to stick with POST, inside the validate page you could populate a form with the POSTed variables, and submit it using a Javascript. Something like
<form id='blah' method='post' action='check_values.php'>
<input type='hidden' value='<?php echo $_POST['var1']; ?>' />
<input type='hidden' value='<?php echo $_POST['var2']; ?>' />
</form>
<script type='text/javascript'>
document.getElementById("blah").submit();
</script>
Another option is stream_context_create, where you can send a redirect header with POST data. But this may only be useful for opening a stream, and the redirect could be difficult.
Of course, the easy way is just use a redirect header and send the data using GET, as #asprin explains.
The following works perfectly
http://www.jquery4u.com/forms/setup-user-friendly-captcha-jqueryphp/

Reusing a $_GET variable in a $_POST?

I have my index.php page that sends values over to edit.php via $_GET/html link. I can see the values go, including the table ID I want to use using echo statements. The problem is that I need the specific ID value used in another form using a submit form($_POST). I've tried different approaches including SESSION, making the $ID = $_GET.... I'm pulling my hair out.
Here is the order of things:
//Edit link on index.php sends the id='summary_id', which works
<td align="center">Edit</td>
//Edit.php grabs the variable and I set it to that variable
if(isset($_GET['id']))
echo "<pre>Value of GET \$_GET:</br>";print_r($_GET);echo"</pre>";
{
$summary_id = $_GET['id'];
$summary_id = $_POST['summary_id'];
But, when I try to use $summary_id in a form (using $_POST this time), I get no value. I'm not sure if what I'm trying to do is "legal". I've tried sending the summary_id as a hidden value in the form, but again, nothing is going through. I'm losing it after that initial $_GET.
You are doing wrong.You are overwriting the variable that is not available $_POST['summary_id']
if you really want to use post method use this
<form action="edit.php" method="POST">
<input type="hidden" value="<?php echo $row['summary_id'];?>" name="summary_id"/>
<input type="submit" value="EDIT"/>
</form>
or you can use header() to post the value
You are overwriting your own variable. Also, it's clear from this that you don't actually have PHP errors turned on.
You could use $_REQUEST instead as it contains both $_GET and $_POST.

Categories