如何通过按钮在没有和输入的情况下在PHP中将变量从一个文件传递到另一个文件

如何通过按钮在没有和输入的情况下在PHP中将变量从一个文件传递到另一个文件

问题描述:

I have a search functionality that it works as shown below, but I would like to be able to pass a variable from a php file to another file without having an input like I have in my search function.

This is search functionality that works:

I have the following index.html file

<html>
<body>
<p>Search</p>
<form name"form1" method="post" action="searchresults.php">
<input name="search" type="text" size="40" maxlength="50"> 
<input type="submit" name="submit" value="Search">
</form>
</body>
</html>

and I have my searchresults.php file

<?php   
 $nameofcity = $_POST['search'];
?>

Something like this is what I would like to have but not sure how to do it.

index.php file

<html>
<body>
<p>Search</p>
<?php
$variabletopass = "London";
echo '<form name"form1" method="post" action="searchresults.php">';
echo '<input type="submit" name="submit" value="Search">';
</form>
?>
</body>
</html>

my searchresults.php file where I want the $nameofcity to be equal to the value of $variabletopass, in the example = London.

<?php   
 $nameofcity = $_POST['search'];
?>

You could try session. For example,

 // index.php

 $_SESSION['variabletopass'] = "London";

 //searchresults.php

 echo  $_SESSION['variabletopass']; // Prints London

Hope this helps.