php post form to another php
I want to post a search value from index.php to search.php,
index.php
<form action="search.php?val=" method="post">
and search.php
<?php echo $_GET['v开发者_运维知识库al']?>
or
<?php echo $_POST['val']?>
I would like to retain 'val' value in the URL, but search.php could not capture the value of 'val' which appear to be "val=".
Create a text field by the name of val
inside the form on index file eg:
<form action="search.php" method="post">
<input type="text" name="val" value="search string" />
<!-- more form stuff -->
</form>
And you can now get it like:
<?php echo $_POST['val']?>
If however, you want that value in url you should modify the form's method to get
like:
<form action="search.php" method="get">
<input type="text" name="val" value="search string" />
<!-- more form stuff -->
</form>
and then you can get its value on search page like:
<?php echo $_GET['val']?>
Alternatively, you can specify search string directly in the action
attribute of the form
and in this case you don't need the val
field:
<form action="search.php?val=<?php echo $search_string;?>" method="get">
<!-- more form stuff -->
</form>
and then you can get its value on search page like:
<?php echo $_GET['val']?>
What you are looking to achieve can be described as URL parameter passing or query string. The idea is that you can pass values through the URL, denoted by ?variable=value
. Important parts of URL parameter passing is retrieving the value by using $_GET
.
Therefore, change this line of code,
<form action="search.php?val=" method="post">
to this line of code,
<form action="search.php" method="get">
You do not need to add the action
to include, ?val
, PHP will do that for you as long as you have an input field with the name
attribute as val
.
<form action="search.php" method="get">
<input type="text" name="val"/>
<input type="submit" />
</form>
Once submitted you should have the URL which looks like,
http://www.example.com/search.php?val=your_text_here
Which from inside search.php
you can access your query string using, $_GET
superglobal.
<?php
echo $_GET['val'];
?>
You can use both. If you have <form action="search.php?val=...">, then you can access it just as $_GET["val"]. Any other form elements will however show up in $_POST[] for your example.
Btw, in cases where the parameter might be submitted by different forms, use $_REQUEST. It captures both GET and POST content.
精彩评论