Passing variables between PHP forms. - php

This is probably a really easy question but its early so yeah...
Basically I have a form:
<form name="varsForm" action="step2.php" id="formID" method="post">
And as I understand it within this for I created some hidden variables. as follows:
<input type="hidden" id="typeid" name="typeid" value="1" />
Because step2.php is set as an action, I though I was correct in assuming that the hidden variables would be passed to step2.php. However when I try to call them I am confronted with errors. I try and call them simply as follows:
<?php echo $_GET['typeid']; ?>
But it says that caseid is an undefined index, I assume I am not calling it correctly, anyone just put me right please?

You are submitting form via POST method, try $_POST['typeid'];
Alternatively change method to GET.

Since you are using method="post" in form, you should use
<?php echo $_POST['typeid']; ?>
$_GET in PHP is used when you use HTTP GET method (method="get" in form tag).

You're using the $_GET array while you're posting your infos.
You should use the $_POST array, or even the $_REQUEST array which handles both POST and GET.

You have method="post" so the data is placed in the message body and not the query string. It will be accessible via $_POST not $_GET.

You are using method="post" so look in $_POST :)
<?php echo $_POST['typeid']; ?>
HTH

Since you are using HTTP POST method in your <form> you need to give the code as:
<?php echo $_POST['typeid']; ?>
Else, in the HTML change this:
<form method="get">

You have method attribute in the form set to POST so values passed to step2.php will not be available in $_GET , it will be available in $_POST so to access the value you need to use $_POST['typeid'] .
And also Some times to avoid warnings OR notifications regarding index ( such as undefined index ) , you can first check for its existence and then process
Some what like this
if (array_key_exists('typeid', $_POST) )
{
$typeid = $_POST['type_id'];
// And do what ever you want to do with this value
}

Related

Can we POST values coming from url

I want to POST values coming from a url .
My url is xxx.com/aa.php?name=RAM
On aa.php page I have written like this
<?php $NAME=$_POST["name"]; ?>
but its value is getting null .
but when using GET Method its values is getting as 'RAM'.
How can I retrieve values using POST from a url ? or is it not possible?
Use $_GET instead of $_POST
<?php $NAME=$_GET["name"]; ?>
If you are not sure about $_GET & $_POST method then you can try $_REQUEST also.
$NAME=$_GET["name"]; //work in get method
$NAME=$_POST["name"]; //work in post method
$NAME=$_REQUEST["name"]; //work in both method
When the parameter is in the URL it is a GET parameter.
You can not fetch a GET parameter from the $_POST array, but the $_GET array.
You can also use the $_REQUEST Array to get both POST and GET variables.
In your case, the GET variable with the key name is RAM, as it should be.
edit:
Worth to mention is that the $_REQUEST array pretty much is a concatenation of $_POST, $_GET and $_COOKIE, so it might behave unexpected if any of the others (than the one you are after) are using the same key names.
I would recommend using the type you are actually wanting, in this case, the $_GET list.
The only solution to pass the data with hidden method is either you should use curl or using form submission with post method like
<form name="" action="aa.php" method="post">
<input typ="hidden" name="name" value="RAM">
<input type="submit" name="submit" value="submit">
</form>
then you can get this as
$_POST['name']
on aa.php page
if you are not sure about your method i.e. $_GET or $_POST.you should use $_REQUEST.
$NAME=$_REQUEST["name"];
for more information:http://www.tutorialspoint.com/php/php_get_post.htm

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.

how top use GET and POST simultaneusly

I have a page in php that gets from the url such as this code example
<form action="welcome.php" method="get">
//some interesting code here
</form>
The issue now is that when i do the GET depending on its value I need to do a POST,
How can I use action="get" and action="post" in the same page?, I am kind of new to PHP so I am not sure if I can use two tags ("i dont think so but please correct me if i am wrong").
PS: I am getting to the same page "welcome.php" and posting to itself again, and depending on the value I am going to show different content.
Thank you
Your question is a little unclear, but if I'm understanding it correctly, you can certainly pass GET variables via the form's action:
<form action="welcome.php?these=are&get=variables" method="post">
//some interesting code here
</form>
You may access both through $_REQUEST if necessary. So no matter what your method type is, $_REQUEST will contain all of your submitted values. This way you don't need to determine whether it was by post, or by get that the data was submitted.
You will have to trap the $_GET value as a hidden var, so when the post is submitted again, your $_GET value will be available:
if (array_key_exists('myVar', $_GET)) {
echo '<input type="hidden" name="myVar" value="' . $_GET['myVar'] .'" />
}
// Now when you resubmit the form you should have access to 'myVar'
echo $_REQUEST['myVar'];
You could also add the get value to the session super global:
if (array_key_exists('myVar', $_GET)) {
$_SESSION['myVar'] = $_GET['myVar'];
}
// Now when you resubmit the form you should have access to 'myVar'
echo $_SESSION['myVar'];

Retrieve password from a html-form

I have a html-form which looks like this:
<form action="lib/AdminPage.php" method="post" id="adminLogin">
Admin-Login: <input type="password" name="pw" value="" class="pw">
<input type="submit" value="Login">
AdminPage.php contains the following lines:
<?php
echo($_GET['pw']);
echo($_GET['adminLogin']);
echo($_GET['Login']);
echo($_GET['id']);
echo($_GET['value']);
echo($_GET['name']);
echo($pw);
echo($_GET["pw"]);
echo($_GET["adminLogin"]);
echo($_GET["Login"]);
echo($_GET["id"]);
echo($_GET["value"]);
echo($_GET["name"]);
?>
None of the echoes works, it's always an "Undefined index" or "Undefined variable" with echo($pw)
How can I retrieve the entered string from the from?
Regards
Use $_POST instead of $_GET, because you have method="post" in your form.
You are sending the form using POST but looking for GET variables. Replace $_GET with $_POST.
Since you have method="post" in your form <form action="lib/AdminPage.php" method="post" id="adminLogin">, variables are accessed as _POST variables. So, you should do
echo $_POST['pw'];
All you need to do is this:
echo $_POST['pw'];
Your HTML form has method="post" in it so you get all information from inputs via $_POST instead of $_GET.
use the $_POST array, not $_GET
Your form uses the POST HTTP method, hence this should work.
echo($_POST['pw']);
echo($_POST['adminLogin']);
echo($_POST['Login']);
echo($_POST['id']);
echo($_POST['value']);
echo($_POST['name']);
You need to post the values. But, you are trying to get those through $_GET.
Try changing everywhere $_GET by $_POST
Change like this :
echo($_POST['pw']);
echo($_POST['adminLogin']);
echo($_POST['Login']);
echo($_POST['id']);
echo($_POST['value']);
echo($_POST['name']);

$_GET and $_POST

I have a form and the method is set to post on the action page when I use $_POST i dont get the value but if I use $_GET or $_REQUEST I do.
This does not make sense. Could someone just clarify it for me?
The code of the form is
<form action="create.php" method"POST">
Just realized I am missing the = after method.
It sounds like you've misplaced or mistyped the method attribute and your form is defaulting to HTTP GET. The form should look like this:
<form method="post" action="file.html">
What's the method set to in the HTML for your form, eg:
<form method="POST" ...>
In PHP ini file, the default setting GPC (Get, Post, Cookie) and Request array has that in itself. And make sure that you really the the POST in the action attribute.
It looks like you typoed your HTML:
<form action="create.php" method"POST">
should be
<form action="create.php" method="POST">
You're missing an equal sign.
<form action="create.php" method="POST">
your missing equal sign after the method
POST and GET are different methods to transfer form data, they both use different ways to send the entered values to your application and have to be handled differently. PHP uses $_POST for the values submitted by a form with method="post" and $_GET for values submitted by a form without a method or with method="get". $_REQUEST is a combination of $_POST and $_GET.
The easiest to see difference is:
Parameters submitted with GET appear in the adress bar, i.e.
http://example.com/index.php?page=home
passes the key page with the value home to $_GET.
Post parameters do not appear in the adress bar.
Your method attribute is wrong, should be:
<form action="create.php" method="POST">
Hehe :-)
<form action="create.php" method="POST">
Your sloppy way of writing is not good for coding...
The error seems to be the missing "=" :)
BTW, the $_REQUEST variable isn't just a combination of $_POST and $_GET, it's an associative array that by default contains the contents of $_GET, $_POST and $_COOKIE. ;)

Categories