views:

379

answers:

5

Hello,

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.

A: 

That's easy, just comment them out:

$a = 'variable 1';
//$b = 'variable 2';
$c = 'variable 2';

You see? I assigned $a, then I passed right over $b, and finally assigned $c. All inside one PHP page!!

Langdon
i think you misunderstood my question, i want to pass a variable in a form to table in one php page. You see?
garcon1986
Passing variables has a specific technical meaning that has nothing to do with ignoring them.
David Dorward
I 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.
Langdon
@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).
David Dorward
-1 It's better not to post anything than obvious non-answers. Please have a look on the FAQ entry on this topic: http://meta.stackoverflow.com/questions/17782/why-do-stackers-consistently-vote-down-humorous-responses
soulmerge
@Thanks david, i tried to add more details about this question. I hope you can see it. Thanks for your advice.
garcon1986
@Langdon, I posted more detalis, i'm sorry for leaving the question without context. I hope you can understand it very well now. Thanks.
garcon1986
@Langdon, Thanks also, but please refer to the post of Robert. And maybe you'll understand my need.
garcon1986
+3  A: 

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;
}
Robert Greiner
I think you have answered my question, Thanks a lot Robert.
garcon1986
no problem, I hope it's what you were looking for
Robert Greiner
For `<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 set `register_globals = on` in php.ini.
garcon1986
@garcon, thanks I knew that but I did it wrong anyway. Time for some more tea I guess :P
Robert Greiner
A: 
<?php
$value = 'bla';
function getValue($string)
{
return ($_GET[$string]);
}
$value = getValue('bla');
?>
<html>

//...
<body>
<form>
<input type="text" value="<?php echo $value; ?>" />
</form>
<tr>
<td>
<tr><?php echo $value; ?></tr>
<td>
</body>
</html>

It is totally unimportant which order you use. You can put the html part into a PHP String with PHP variables inside, and echo it, php will automatically assign the vars inside. Or you seperate HTML and PHP as seen above.

Try to work with template engines, e.g. SMARTY (smarty.com)

daemonfire300
@daemonfire300, it's a good advice. Thanks.
garcon1986
+2  A: 

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
}
dnagirl
+2  A: 

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&amp;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.

SjB
@SjB, Yes, this is the common way of passing variables between pages. Thanks.
garcon1986