【问题标题】:Clear PHP echoed values input fields清除 PHP 回显值输入字段
【发布时间】:2018-11-02 09:09:05
【问题描述】:

我有一个表单,它通过 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;' >" ; 

取消按钮和Clear text field value in JQuery 中指定的javascript 代码都不起作用。

我猜是因为我附和了它。 有人可以帮我吗? 在此先感谢.... :)

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

【问题讨论】:

  • 你的cancel按钮javascript代码在哪里?给我们看看。
  • 你有&lt;form&gt;标签吗?没有一个重置输入类型不会有太大作用。
  • @iainn.. 是的,我愿意。
  • @SurkaavS 对,你能用 complete 代码更新问题吗?如果您发布的只是摘录,那么任何人都很难帮助您。
  • 正在使用不安全的查询。这个问题有 2 个赞成票,因为....?清楚吗?它是否在 singke 引用的字符串中回显变量?

标签: php html


【解决方案1】:

如果您在代码中包含了 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>';

【讨论】:

  • 你能指定我应该在哪里添加这个jquery吗?我对所有这些都很陌生....谢谢:)
  • 您可以在回显表单之前添加它。如果您在 html 的 和 之间添加它会更好。
  • 谢谢:) 但我也试过了,但没有帮助! :(
  • 我看到你正在使用 Bootstrap,也许已经包含了 jQuery。查看开发人员控制台中的错误。 developers.google.com/web/tools/chrome-devtools
  • 在表单中,重置按钮已经完成了这项工作。 w3schools.com/tags/tryit.asp?filename=tryhtml5_input_type_reset 问题是,问题中没有发布&lt;form&gt; 标签,所以不清楚。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-30
  • 1970-01-01
相关资源
最近更新 更多