how to pass variables inside one page
Solution 1
EDIT: This is how you pass a user entered value from a form to a PHP page.
Here is a generic form:
<form method="post" action="<?php echo $_SERVER['$PHP_SELF'];?>">
<input type="text" size="12" maxlength="12" name="name">
...
...
</form>
Now for the PHP code:
<?php
$name = $_POST["name"];
?>
Note: you can change the post type between POST and GET and change the action option to send the form input to a different PHP page.
Are you talking about passing a variable to a function?
$name = 'Robert';
printName($name);
function printName($n) {
echo $n;
}
Solution 2
All form variables end up in the $_POST or $_GET superglobal array (depending on the form method). If your script both displays the form and processes it, a standard method goes something like this:
if(isset($_POST['submit'])){
//validate $_POST variables from form
//if validation works do action
//else output errors and output form again
} else {
//output form
}
Solution 3
GET it from the URL
The quickest (but most limited) way to transfer variables is by a method called GET. With GET, you append the variables onto the URL of the page you want the variables to be transferred to:
http://www.matthom.com/contact.php?id=301&name=Matthom
The example above would give the contact.php page two variables to utilize: id, and name, whose values are 301, and Matthom, respectively.
You can add as many variables to the URL as you’d like.
Beware – sometimes you don’t want your variables to be shown "out in the open." Also, you are limited to 255 characters in the URL, so the variables can’t contain too much information.
From contact.php, you can GET these two variables via PHP:
GRAB THE VARIABLES FROM THE URL
$id = $_GET['id'];
$name =$_GET['name'];
POST it from a FORM
Another way to transfer variables, and by far the more robust way, is to grab them from a form.
Let’s say this is your form field code:
<form action="process.php" method="post">
<input type="text" size="25" name="searchtype" />
<input type="text" size="25" name="searchterm" />
</form>
These two input boxes allow users to enter information. At process.php, you can grab the variables in the same way:
GRAB THE VARIABLES FROM THE FORM
$searchtype = $_POST['searchtype'];
$searchterm = $_POST['searchterm'];
Notice the use of $_POST[]
over $_GET[]
. This is an important distinction.
SUN Jiangong
Updated on July 31, 2022Comments
-
SUN Jiangong almost 2 years
I want to know how to pass variables from a form to a php page.
Thanks.
Edit:
I have some input and a submit button in a form. I want my php page to search the database and display a relevant table after I click the submit button. And all the actions here are in one page.
So I want to know how to pass variables from a form to a php script on the same page. I'm sorry for leaving so little detail.
Can you understand my question now? Thanks again.
-
SUN Jiangong over 14 yearsi think you misunderstood my question, i want to pass a variable in a form to table in one php page. You see?
-
Quentin over 14 yearsPassing variables has a specific technical meaning that has nothing to do with ignoring them.
-
Langdon over 14 yearsI didn't misunderstand your question. I just read into it and made ridiculous assumptions since you decided to leave out any sort of context or details.
-
Quentin over 14 years@garcon1986 — based on that comment, I think you might have your terminology or understanding of the interaction between server side code and the user's browser wrong. Please update your question and elaborate on precisely what you are trying to achieve (in lots of detail).
-
soulmerge over 14 years-1 It's better not to post anything than obvious non-answers. Please have a look on the FAQ entry on this topic: meta.stackexchange.com/questions/17782/…
-
SUN Jiangong over 14 years@Thanks david, i tried to add more details about this question. I hope you can see it. Thanks for your advice.
-
SUN Jiangong over 14 years@Langdon, I posted more detalis, i'm sorry for leaving the question without context. I hope you can understand it very well now. Thanks.
-
SUN Jiangong over 14 yearsI think you have answered my question, Thanks a lot Robert.
-
SUN Jiangong over 14 years@SjB, Yes, this is the common way of passing variables between pages. Thanks.
-
SUN Jiangong over 14 years@Langdon, Thanks also, but please refer to the post of Robert. And maybe you'll understand my need.
-
SUN Jiangong over 14 yearsFor
<form method="post" action="<?php echo $PHP_SELF;?>">
, I suggest modifying it to<form method="post" action="<?php echo $_SERVER['$PHP_SELF'];?>">
. Because you have to setregister_globals = on
in php.ini. -
Robert Greiner over 14 years@garcon, thanks I knew that but I did it wrong anyway. Time for some more tea I guess :P