【发布时间】:2020-06-21 00:35:49
【问题描述】:
我的表单中有一个提交按钮,单击该按钮后,我的文本字段中的文本就会消失。我基本上需要保留文本,因为我有另一个按钮需要该文本字段中的文本。这两个按钮是两个不同功能的一部分,我想知道是否有办法在另一个按钮的功能中使用变量的值。
例如,下面是其中一个按钮的代码:
Enter customer name<input type="text" name="cname">
<input type="submit" name="sub" value="Display">
<?php
if(isset($_GET['sub'])){
display();
}
function display(){
include 'config.php';
$searchstr=$_GET['cname'];
echo "<input type='hidden' name='cname1' value=".$searchstr.">";
$td=$_GET['cname1'];
$sql="select * from info where cname='$searchstr';";
$result=mysqli_query($conn, $sql);
if($searchstr==""){
$message="Please enter a customer name";
echo "<script type='text/javascript'>alert('$message');
window.location.href='retreive.php';</script>";
}
else{
echo "<table id='info' border='1px solid black'><tr padding='2'><th>Customer Name</th><th>Purchase Order Date</th><th>Started?</th><th>Reason (If any)</th><th>Tentative start date</th><th>Progress</th><th>Current Status</th><th></tr>";
while ($row = $result->fetch_assoc()) {
$cname=$row['cname'];
$podate=$row['podate'];
$started=$row['started'];
$reason=$row['reason'];
$tentdate=$row['tentdate'];
$progress=$row['progress'];
$status=$row['status'];
echo "<tr><td>".$cname."</td><td>".$podate."</td><td>".$started."</td><td>".$reason."</td><td>".$tentdate."</td><td>".$progress."</td><td>".$status."</td></tr>";
}
echo "</table><br>";
}
这里一切正常,并根据需要显示表格。但请注意上面的变量$td。我需要在单击另一个按钮时显示该值,该按钮位于不同的功能中。
这是另一个按钮的代码:
echo "<input type='submit' name='fp' value='Finished payment'>";
if(isset($_GET['fp'])){
echo $td;
}
单击该按钮不显示任何内容,这意味着我无法在显示函数之外读取此变量。我尝试在 php 中查找全局变量,另一种解决方案是使用然后使用 Javascript,但我想使用 php,我需要在单击提交按钮后将文本保留在文本字段中,以便稍后阅读。
谢谢。
【问题讨论】:
-
您希望隐藏输入字段中的值显示在“fp”输入字段的值中吗?
-
您可以使用该 $_GET 信息创建一个变量,然后将其发布到另一个输入字段的值中。
-
我把隐藏字段放在那里,作为尝试同样事情的另一种方式。这也不起作用,因为我在某个地方出错了。
-
看看我的回答应该能帮到你
标签: php html button submit textfield