【问题标题】:how to create a form with three text input fields with only one submit button如何创建一个只有一个提交按钮的具有三个文本输入字段的表单
【发布时间】:2014-09-21 07:31:47
【问题描述】:

我正在尝试使用 3 个文本字段和一个提交按钮创建一个可以接受 3 种不同类型的查询请求的 html 表单。当该人在其中一个字段中输入数据并单击提交按钮时,它将根据输入查询数据库。我可以用一个文本框来做,但我不知道如何做 3 个文本框。此外,此人一次只能选择查询一个文本字段。

字段 1 是 student_id 字段 2 是 last_name 字段 3 是考试日期。

个人可以输入 student_id,并且只能输入 student_id,或者 last_name 并且只能输入姓氏,或者可以输入examDate 并且只能输入考试日期。

我遇到的一个问题是如何将 3 种不同类型的数据传递给 PHP 并在 select 语句中进行查询。看来我必须创建 3 个不同的选择查询语句,但是如何创建代码来识别要运行的查询?

这是我目前所拥有的:

<!DOCTYPE html>  
   <div id="form_wrap"><!-- start form wrap -->
     <div id="form_header">
       </div>
         <div id="form_body">
         <p>Search for a certification request (Enter one of the following):</p>
         <form action="search.php" method="post" name="information" id="information" 
        onsubmit="return(validate())">
        <div class="field"> 
            <label for = "Student_id"> Student ID:</label>
            <input type="text" name="search" id="search" />
        </div>
        <div class="field">
            <label for = "last_name"> Last Name:</label>     
            <input type="text" name="last" id="last" />
        </div>
        <div class="field">
            <label for = "examDate"> Exam Date:</label>  
            <input type="text" name="date" id="mm/dd/yyyy"  /> 
   <input type="submit" value="Search" />
    </form>
    </div>
</div>

这是php代码

     <?php
require 'security.php';
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Otherwise we connect to our Database
$db = new mysqli('localhost', 'root', '', 'corpmis_ddempsey');
//check connection
if($db->connect_errno) {
    die('sorry, we are having some problems');
    }else {
    echo 'connected';
}

$search = $_REQUEST['search'];
//If they did not enter a search term we give them an error
if ($search == "")
{
echo "<p>You forgot to enter a search term!!!";
exit;
}
// We perform a bit of filtering
 //$search = strtoupper($search);
$search = strip_tags($search);
$search = trim ($search);

//Now we search for our search term, in the field the user specified
//$result = $db->query("SELECT * FROM records WHERE student_id LIKE '$search'");
/*

");
*/
//And we display the results
    if($result = $db->query("SELECT * FROM records WHERE student_id LIKE '$search'" )){
        if($result->num_rows){
            while($row = $result->fetch_object()){
                $records[] = $row;
            }
        $result->free();
    }

}
?>
<!DOCTYPE html>
<html>

    <head>
    <title>Sample</title>
</head>
<body>
    <h3>People</h3>
    <?php
    if(!count($records)) {
    echo 'No records';

    } else {

    ?>
    <table>
        <thead>
            <tr>
                <th>student_id</th>
                <th>First name</th>
                <th>Last name</th>
                <th>email</th>
                <th>Major</th>
                <th>Exam Name</th>
                <th>Taken class</th>
                <th>Prepare</th>
                <th>MeasureUp Key</th>
                <th>Exam Date</th>
                <th>Request Made On</th>
            </tr>
            </thead>
            <tbody>
            <?php
            foreach($records as $r){
            ?>
                <tr>
                    <td><?php echo $r->student_id; ?></td>
                    <td><?php echo $r->first_name; ?></td>
                    <td><?php echo $r->last_name; ?></td>
                    <td><?php echo $r->email; ?></td>
                    <td><?php echo $r->major; ?></td>
                    <td><?php echo $r->examName?></td>
                    <td><?php echo $r->taken_class; ?></td>
                    <td><?php echo $r->prepare; ?></td>
                    <td><?php echo $r->MeasureUpKey; ?></td>
                    <td><?php echo $r->examDate; ?></td>
                    <td><?php echo $r->request_made; ?></td>
                </tr>
                <?php
                }

                ?>

            </tbody>
    </table>
    <?php
    }
    ?>

【问题讨论】:

  • 你可以在 where 子句中使用多个列,只需搜索即可,where col1 = 2 and col2 = whatever ...
  • 你可以轻松地做你想做的事。

标签: javascript php html mysqli


【解决方案1】:

实际上你不能。您可以识别哪个输入框具有值并根据该值创建查询。但是,可以填写多个输入并提交表单,这样就行不通了。 一种选择是使用下拉菜单并让用户选择要填充的输入。例如:

<select name="type">
    <option value="student_id">Student ID</option>
    <option value="last_name">Last name</option>
    <option value="date">Exam date</option>
</select>
<input name="typeValue" value="" />

现在您知道用户想要查询哪个字段。您可以使用例如:

<?php
    //Note that you should add checks using e.g. isset()
    $value = $_POST['typeValue'];

    if($_POST['type'] == "student_id")
    {
        //Query with $value on student_id
    }
    elseif($_POST['type'] == "last_name")
    {
        //Query with $value on last_name
    }
    elseif($_POST['type'] == "date")
    {
        //Query with $value on date
    }
?>

或者如果它在同一个表中并且option 值与表中的列名相同:

<?php
    //Note that you should add checks using e.g. isset()
    //Also note this is vulnerable for SQL injection, using prepared statements will solve that.
    $query = $db->query("SELECT * FROM records WHERE ".$_POST['type']." LIKE '%".$_POST['typeValue']."%'" );
?>

您也可以使用 Javascript 解决此问题。 javascript 的一个选项是确定哪个输入字段最后填充,并根据最后填充的输入字段创建查询。

【讨论】:

  • 谢谢 S.Pols 我会试试这个,看起来可以解决它。我还将输入代码以防止 sql 注入。顺便说一句,有些人建议使用 AJax,但我不知道该代码是的,但也计划学习它,只是还没有。
  • 有问题..当我测试代码以查看它是否有效时,当我知道它确实存在时它会返回没有结果..这是我根据您的建议使用的代码..我一定是做错了什么。
  • [代码] $records = array(); $typeValue = $_REQUEST['typeValue']; if ($typeValue == "") echo "

    你忘记输入搜索词了!!!";出口; $typeValue = strip_tags($typeValue); $typeValue = 修剪 ($typeValue); [/code]

  • [code] if(!empty($_POST)) { if(isset($_POST['last_name'],$_POST['student_id'],$_POST['examDate'])) ;{ $last_name = trim($_POST['last_name']); $student_id = trim($_POST['student_id']); $examDate = trim($_POST['examDate']); if(!empty($last_name) && !empty($student_id) && !empty($examDate)){ [/code]
  • [code] if($result = $db->query("SELECT * FROM records WHERE ".$_POST['type']." LIKE '%".$_POST['typeValue' ]."%'" )){ if($result->num_rows){ while($row = $result->fetch_object()){ $records[] = $row; } $result->free(); } } } } } [/code]
【解决方案2】:

有很多方法可以实现这一点,但您可以在此处使用 ajax 请求。将用户输入的值作为发布参数发送 ajax 发布请求到您的 sql 查询页面。

在查询页面,你可以将post参数传递到sql查询的where子句中。然后填写 ajax 成功响应表。

【讨论】:

    【解决方案3】:

    我的html代码

    [code]
    
    <!-- start javascript -->
    <script type="text/javascript">
    /*<![CDATA[ */
       function check(){
        if(document.lastname.last.value == "" || document.lastname.last.value == null)
        {
            alert("no last name entered");
            return false;
        }
    }
    function checkdate() {
    var date_regex = /^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/ ;
    if(!(date_regex.test(testDate)))
    {
    return false;
    }
    
    }
    
    function fieldSwap(image){
     var sb = document.getElementById('sb');
     if(sb.value == ""){
      sb.style.background = "url(images/"+image+") no-repeat";
      }
    }
    
    function buttonSwap(image){
     var sb = document.getElementById('sb');
     sb.src = "images/"+image;
    }
    function validate(){
    var x = document.information.search.value;
    if (x.length<10 ||  x.length>10){
    alert("student id is incorrect");
    return false;
    }
    }
    
    /*]]> */    
    </script>
    <!-- end javascript -->
    
    
    
    
    </head>
    
    
    <body>
    
    <div id="form_wrap"><!-- start form wrap -->
         <div id="form_header">
         </div>
         <div id="form_body">
         <p>Search for a certification request (Enter one of the following):</p>
         <form action="search.php" method="POST" name="information" id="information" onsubmit="return(validate())">
    
           <div class="field">  
                <select name="type">
                    <option value="student_id">Student ID</option>
                    <option value="last_name">Last name</option>
                    <option value="examdate">Exam date</option>
                </select>
                <input name="typeValue" value="" />
            <input type="submit" value="Search" />
         </form>
        </div>
    </div>  
    
        </div><!-- end form wrap -->
    
        </body>
        </html>
    
    [/code
    

    php 代码.. 仅测试 student_id 查询以查看我是否编码正确,然后将添加姓氏和考试日期的选择查询。

    [代码]

    $records = array();
    
    $typeValue = $_REQUEST['typeValue'];
    
    echo $typeValue;
    //If they did not enter a search term we give them an error
    if ($typeValue == "")
    {
    echo "<p>You forgot to enter a search term!!!";
    exit;
    }
    // We perform a bit of filtering
    //$typevalue = strtoupper($search);
    $typeValue = strip_tags($typeValue);
    $typeValue = trim ($typeValue);
    
         //Note that you should add checks using e.g. isset()
        $value = $_POST['typeValue'];
    
        if($_POST['type'] == "student_id")
        {
            //Query with $value on student_id
    
            if($result = $db->query("SELECT * FROM records WHERE student_id LIKE '$typeValue'" )){
                if($result->num_rows){
                    while($row = $result->fetch_object()){
                    }
                $result->free();
                }
            }
        }
        elseif($_POST['type'] == "last_name")
        {
            //Query with $value on last_name
        }
        elseif($_POST['type'] == "date")
        {
            //Query with $value on date
        }           
    
    
    /*}*/ echo "value of $value" .$value;
    
    //This counts the number or results - and if there wasn't any it gives them a     little     message explaining that
    $anymatches=$result;
    if ($anymatches == 0 )
    {
    echo "Sorry, but we can not find an entry to match your query...<br><br>";
    }
    
    //And we remind them what they searched for
    echo "<b>Results For:</b> " .$typeValue;
    //}
    ?>
    <!DOCTYPE html>
    <html>
    
        <head>
        <title>Search Result</title>
        </head>
        <body>
            <h3>Results</h3>
            <?php
            if(!count($records)) {
            echo 'No records';
    
            } else {
    
            ?>
            <table>
                <thead>
                    <tr>
                        <th>student_id</th>
                        <th>First name</th>
                        <th>Last name</th>
                        <th>email</th>
                        <th>Major</th>
                        <th>Exam Name</th>
                        <th>Taken class</th>
                        <th>Prepare</th>
                        <th>MeasureUp Key</th>
                        <th>Exam Date</th>
                        <th>Request Made On</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php
                    foreach($records as $r){
                    ?>
                        <tr>
                            <td><?php echo $r->student_id; ?></td>
                            <td><?php echo $r->first_name; ?></td>
                            <td><?php echo $r->last_name; ?></td>
                            <td><?php echo $r->email; ?></td>
                            <td><?php echo $r->major; ?></td>
                            <td><?php echo $r->examName?></td>
                            <td><?php echo $r->taken_class; ?></td>
                            <td><?php echo $r->prepare; ?></td>
                            <td><?php echo $r->MeasureUpKey; ?></td>
                            <td><?php echo $r->examDate; ?></td>
                            <td><?php echo $r->request_made; ?></td>
                        </tr>
                        <?php
                        }
    
                        ?>
    
                    </tbody>
            </table>
            <?php
            }
            ?>
    </html>
    

    [/代码]

    【讨论】:

    • 谢谢 s.Pol 我用你给我的东西弄明白了。现在我有另一个问题,我将开始一个新的帖子......所以这篇文章不会变得无关紧要。
    猜你喜欢
    • 2011-06-23
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-05
    相关资源
    最近更新 更多