提交表单(过滤内容)后,结果表将显示在同一页面上。我该怎么做?

时间:2017-01-04 06:29:50

标签: javascript php jquery forms session

我想就如何进行编码(关于我的问题)提出一些建议。

无论如何,我有一个搜索表单,其中包含一个选择表单和一个文本框。提交后,将根据表单中的筛选结果显示一个表格。

我是否必须使用会话,或者我只需要在我的PHP代码中使用$ _POST变量?或者是javascript?

如果是$ _POST或$ _SESSION,我是否需要创建另一个php文件?我已经这样做了,但它在我的表单页面上显示了很多错误。

我希望表格显示在同一页面上。

我的表格                     

                    <div class="form-group">
                        Find
                        <select name="selected" class="form-control" required data-error="Please select from the list">
                            <option value="">--Select--</option>
                            <option value="story">Stories</option>
                            <option value="poem">Poems</option>
                            <option value="user">Users</option>
                        </select>
                        <div class="help-block with-errors"></div>

                    </div>


                    <div class="form-group">
                        With
                        <input type="text" class="form-control" id="contains" name="contains" required

                               data-error="Please enter a value"/>
                        <div class="help-block with-errors"></div>
                    </div>
                    <div class="col-md-6 col-xs-6">
                        <input type="submit" class="btn btn-primary btn-block" id="submitBtn"
                               value="Search"/></div>
                    <div class="col-md-6 col-xs-6">
                        <input type="reset" class="btn btn-default btn-block" value="Clear Fields"/></div>
                </form>

我已经创建了一个单独的php文件来存储我的所有$ _SESSIONS和$ _POST,但不是很确定。

<?php
session_start();
include("dbconn.php");
$selected = $_POST['selected'];
$searchContent = $_POST['content'];
$_SESSION['selected'] = $selected;
$_SESSION['contains'] = $searchContent;

if ($selected == "story" || $selected == "poem") {
    if (isset($_SESSION['username'])) {
        $querySearchSP = "select * from storypoem where content like '%$searchContent%' and type='poem'";
    } else if (!isset($_SESSION['username'])) {
        $querySearchSP = "select * from storypoem where content like '%$searchContent%' and type='poem' and progress='Completed'";
    }
    $resultSearchSP = mysqli_query($link, $querySearchSP);
    $searchResultSP = mysqli_num_rows($resultSearchSP);

    while ($rowStoryPoem = mysqli_fetch_array($searchResultSP)) {
        $arrStoryPoem[] = $rowStoryPoem;
        $_SESSION['title'] = $rowStoryPoem['title'];
        $_SESSION['progress'] = $rowStoryPoem['progress'];
        $_SESSION['currentlyCompleted'] = $rowStoryPoem['currentlycompleted'];
    }
} else if ($selected == "user") {
    $querySearchUser = "select * from user u, storypoem sp where content like '%$searchContent%' and u.storypoem_id=sp.id";
    $resultSearchUser = mysqli_query($link, $querySearchUser);
    $searchResultUser = mysqli_num_rows($resultSearchUser);

    while ($rowUser = mysqli_fetch_array($searchResultUser)) {
        $arrUser[] = $rowUser;
        $_SESSION['username'] = $rowUser['username'];
        $_SESSION['works'] = $rowStoryPoem['title'];
    }
}
?> 

非常感谢您的帮助。感谢。

1 个答案:

答案 0 :(得分:0)

对于同一页面,请遵循以下方法:

<form action="" name="frm">
// your html elements
<input type="submit" name="btn" value="Search" />
</form>

<?php
if(isset($_REQUEST['btn']))
{
    // put your mysql code here and for that response create a result table by using looping on the result set
}
?>