0

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'];
?>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Martin D
  • 43
  • 2
  • Have a look at this: http://stackoverflow.com/questions/871858/php-pass-variable-to-next-page In short: You can use "global" variables like `$_SESSION` for that. – ahuemmer Apr 07 '16 at 17:53

1 Answers1

0

You could try session. For example,

 // index.php

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

 //searchresults.php

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

Hope this helps.

Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32