首页 > 解决方案 > 清除 PHP 回显值输入字段

问题描述

我有一个表单,它通过 php 的 echo 函数显示从表中单击的值。回显逻辑如下:

     //reading input
     if(isset($_POST['add'])){
        $scrid=$_POST['sc'];
        $desc=mysqli_real_escape_string($_POST['desc']);
        $cb=mysqli_real_escape_string($_POST['created']);
        $version=mysqli_real_escape_string($_POST['version']);
        if($scrid !=0){
            if(empty($desc)){$result="Description cannot be Empty";}
            elseif(empty($cb)){$result="Created By cannot be Empty";}
            elseif(empty($version)){$result="Version cannot be Empty";}
            else{
                echo $t."empty";
                $sql="INSERT INTO testmaster(ScreenID,Description,createdBy,version) VALUES('$scrid','$desc','$cb','$version');";
                if(mysqli_query($link,$sql)){                         
                         header('Location: testmaster.php');
               }else{
                    echo "Query failed";
                   }
            }
        }
    }
                      //passing table values,,,,, works perfectly fine...
                    if(mysqli_num_rows($sol)>0){
                   while($row=mysqli_fetch_assoc($sol)){
                            echo"<tr>";

                            echo"<td><a href=testmaster.php?testid=".$row['tid'].">".$row['Description']."</a></td>";
                            echo"<td>".$row['createdBy']."</td>";
                            echo"<td>".$row['version']."</td>";
                            echo "</tr>";
                    }
                }      
                }
        if(isset($_GET['testid'])){
        $t=$_GET['testid'];
        $query="SELECT * FROM testmaster WHERE tid='$t';";
        if($sol=mysqli_query($link,$query)){
            $modif=mysqli_fetch_assoc($sol);
            $d=$modif['Description'];
            $c=$modif['createdBy'];
            $v=$modif['version'];
            $sc=$modif['ScreenID'];

        }

//These are the input fields



echo'<td><textarea name="desc" class="form-rounded" cols=50 rows=2 
    placeholder="Description">'.$d;
    echo'</textarea></td>';
    echo'<td><input type="text" name="created" class="form-control" 
    placeholder="Created By" value='.$c.'>';
    echo'</td>';

//cancel


echo "<input type='reset' class='btn button2' name='cancel' id='cancel' value='Cancel' style='height:12px; text-align:center; margin-left:10px;' >" ; 

取消按钮和在 JQuery 中清除文本字段值中指定的 javascript 代码都不起作用。

我想这是因为我附和它。任何人都可以帮我吗?提前致谢.... :)

PS所有输入字段和取消按钮都在里面......

标签: phphtml

解决方案


如果您在代码中包含了 JQuery,那么您可以这样做:

  1. clear-onclick(或其他名称)类添加到您希望为空的所有字段
  2. onclick处理程序添加到按钮

表格代码

 echo'<td><textarea name="desc" class="form-rounded clear-onclick" cols=50 rows=2 
placeholder="Description">'.$d;
echo'</textarea></td>';
echo'<td><input type="text" name="created" class="form-control clear-onclick" 
placeholder="Created By" value='.$c.'>';
echo'</td>';

按钮代码

 <input type='reset' class='btn button2' name='cancel' id='cancel' onclick="javascript: $('.clear-onclick').val(null)" value='Cancel' style='height:12px; text-align:center; margin-left:10px;' >

您可以按如下方式包含 jQuery:

echo '<script src="https://code.jquery.com/jquery-3.3.1.js"></script>';

推荐阅读