![](/img/trans.png)
[英]how to pass table row values to next page in input text field after clicking submit button
[英]Text disppears from text field after clicking submit button html php. Need the text to stay
我的表單中有一個提交按鈕,單擊該按鈕時,文本字段中的文本會消失。 我基本上需要保留文本,因為我有另一個按鈕需要該文本字段中的文本。 這兩個按鈕是兩個不同功能的一部分,我想知道是否有辦法在另一個按鈕的功能中使用一個按鈕的功能中的變量值。
例如,這是其中一個按鈕的代碼:
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 和單擊提交按鈕后,我需要將文本保留在文本字段中,以便稍后閱讀。
謝謝你。
您可以簡單地為該值創建一個變量,然后將該變量放在另一個輸入的值部分。
$name= 'Enter Name';
if(isset($_GET['cname'])){
$name = $_GET['cname'];
}
然后
<input type="text" name="cname" value="<?=$name?>">
使用占位符屬性:
<input type="text" placeholder="Enter Name" name="cname" value="<?php echo $name; ?>
在處理$_GET['cname']
之前,輸入字段中的值設置為您希望顯示的文本,或者只是將其保留為空。 然后,一旦$_GET['cname']
已發布,您檢查它是否已設置,然后將該變量設置為等於用戶在另一個字段中輸入的返回信息 - $td。
<?php
$cname = ''; // or $cname = 'Enter a Name';
if(isset($_GET['sub'])){
display();
}
function display(){
include 'config.php';
$searchstr=$_GET['cname'];
$td=$_GET['cname'];
$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{
//use isset to see if that has been posted
if(isset($_GET['cname'])){
$name = $_GET['cname'];
//$name is now set to the value you wish to display in the value of the other input
//call on the value like so <input type="text" name="cname" value="<?=$name?>">
}
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>";
}
php fiddle:僅適用於 48 小時 - http://main.xfiddle.com/8f416cc9/input.php
提交表單后,用戶輸入的文本消失了? 這是瀏覽器的默認行為。 為了避免這種情況,要么使用 jQuery 提交表單,要么將輸入的值存儲到變量中並回顯。
Jquery的示例代碼:
$("#myform").submit(function(e) {
//prevent Default functionality
e.preventDefault();
//get the action-url of the form
var actionurl = e.currentTarget.action;
//do your own request an handle the results
$.ajax({
url: actionurl,
type: 'post',
dataType: 'application/json',
data: $("#myform").serialize(),
success: function(data) {
... do something with the data...
}
});
});
==如果不使用Jquery==
PHP:
$myValue = $_POST['my_input_name'];
HTML:
<input type="submit" name="sub" value="<?= $myValue ?>">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.