【问题标题】:Text disppears from text field after clicking submit button html php. Need the text to stay单击提交按钮 html php 后,文本从文本字段中消失。需要文字留下来
【发布时间】: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


【解决方案1】:

您可以简单地为该值创建一个变量,然后将该变量放在另一个输入的值部分。

$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

【讨论】:

  • 好吧,我的错。我似乎错过了问题中的某些内容。将立即对其进行编辑。基本上,'cname' 已经有一个值,它是我在文本字段中输入的值。我的文本字段的名称为“cname”,因为我现在将在问题中进行编辑。为不完整的问题道歉。
  • 在上面编辑的代码中,我有Enter customer name&lt;input type="text" name="cname"&gt;这行我希望在单击第一个提交按钮后文本保留在该输入字段中。再次为这个不准确的问题道歉。
  • Enter customer name&lt;input type="text" name="cname"&gt; 在输入标签中添加值部分,然后将其设置为来自您的帖子的变量。 &lt;input type="text" name="cname" value="&lt;?=$cname"&gt; 在 value 部分中,您可以调用 if 语句或声明来自 php 文件中评估的变量。我的示例在这里显示了一个设置为张贴变量的变量。正如我的回答所示。
  • 你也可以使用张贴的变量,只要记住要确保它是先设置的。 &lt;input type="text" name="cname" value="&lt;?php if(isset($_POST['cname'])){ echo $_POST['cname']; }esle{ echo 'Enter a name'; } ?&gt;
【解决方案2】:

提交表单后,用户输入的文字消失了?这是浏览器的默认行为。为了避免这种情况,要么使用 jQuery 提交表单,要么将输入的值存储到变量和 echo 中。

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 ?>">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-29
    • 2021-03-27
    • 2010-12-10
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多