drcrc28428 2016-04-07 17:46
浏览 39
已采纳

如何通过按钮在没有和输入的情况下在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'];
?>
  • 写回答

1条回答 默认 最新

  • drgbpq5930 2016-04-07 17:54
    关注

    You could try session. For example,

     // index.php
    
     $_SESSION['variabletopass'] = "London";
    
     //searchresults.php
    
     echo  $_SESSION['variabletopass']; // Prints London
    

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式