如何使用三个文本输入字段创建一个只有一个提交按钮的表单

时间:2014-09-21 07:31:47

标签: javascript php html html5 mysqli

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

字段1是student_id字段2是last_name字段3是exam_date。

人可以输入student_id,只能输入student_id或last_name,只能输入姓氏,或者考试日期和考试日期。

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

这是我到目前为止所做的:

<!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
    }
    ?>

3 个答案:

答案 0 :(得分:0)

其实你做不到。您可以识别哪个输入框具有值并根据该值创建查询。但是,可以填写多个输入并提交表单,这样就无法工作了。 一种选择是使用下拉列表,让用户选择要填充的输入。例如:

<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的选项是识别哪个输入字段作为最后填充,并根据最后填充的输入字段创建查询。

答案 1 :(得分:0)

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

在查询页面上,您可以将该post参数传递给sql查询的where子句。然后填写关于ajax成功响应的表格。

答案 2 :(得分:0)

我的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>

[/代码]