Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php post form to another php

Tags:

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['val']?>

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=".

like image 581
proyb2 Avatar asked Jun 28 '26 03:06

proyb2


1 Answers

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']?>
like image 78
Sarfraz Avatar answered Jun 29 '26 16:06

Sarfraz