表格内部Div,在不同的Div中显示结果

时间:2014-09-11 19:20:34

标签: php html

我有一个带有两个嵌套div的div语句。第一个嵌套div是一个包含下拉菜单的表单,该菜单允许此人选择基本的学校科目:

<form id="request" action="<?php echo $_SERVER['PHP_SELF']?> method="post">
        <div id='d2'>

            <p id='p2'>Subject: 
                <select id="s" name="subject">
                    <option value="English">English</option>
                    <option value="Social Studies">Social Studies</option>
                    <option value="Math">Math</option>
                    <option value="Science">Science</option>
                </select>
            </p>
            <input type="button" value="Find"/>

        </div>
</form>

第二个嵌套div将使用PHP打印出来,这是一个可以帮助学生用户的先前初始化的辅导老师,以及允许该人选择特定导师的链接。请原谅我不那么出色的格式,我还是初学者:

<div id='div3'>
for ($i=0; $i<count($tutors); $i++)
{
      echo "<div>'".$tutors[$i]."'</div><br/>"
      . '<a href = "SelectedTutor.php">Choose me</a>' . "<br/>";
}
</div>

数组在php类的最开始时通过连接到MySQL然后从数据库中拔出导师用户和导师进入学生用户选择的主题来初始化。再次,请原谅我有任何错误,我还在学习:

<?php 
if ($_SERVER['REQUEST_METHOD']=='POST')
{
    $sub = $_POST['subject'];

    $con = mysqli_connect("127.0.0.1", "root", "//removed", "mydb");
    $msg = "";
    if (mysqli_connect_errno())
    {
        $msg = "Failed to connect to MySQL: " . mysqli_connect_error();
        echo $msg;
    }    
    $query = mysqli_query($con, "SELECT * FROM User WHERE Role = tutor AND Subject ='".$sub."'");
    $tutors = array();
    while ($row = mysqli_fetch_assoc($query))
    {
        $tutors[] = $row["Username"];
    }
}
else
{
    echo "Failed form.";
}
?>

现在最让我感到压力的问题是失败的表单始终显示在屏幕上。我怀疑这是因为表单已嵌套在div中。有没有办法解决?或者它是一个不同的问题?

我也想知道代码是否确实会在第二个嵌套div中显示我想要的内容,或者是否也存在错误(稍后我会设置样式)。

2 个答案:

答案 0 :(得分:0)

我基于我的解决方案。根据以下假设。根据你的帖子中的这些行。 第二个嵌套div将使用PHP打印出来,这是一个可以帮助学生用户的先前初始化的辅导老师,以及允许该人选择特定导师的链接。请原谅我不太精彩的格式,我还是初学者:&gt;&gt;&gt;&gt;&gt;&gt;这些行来自你的帖子。

请仔细阅读代码中的注释。在那里,我解释我的变化和建议。

此代码

<!--

<form id="request" action="<?php //echo $_SERVER['PHP_SELF']?> method="post">-->       

    /*
  * The line above is wrong and as you may understand by the comments of other user, 
    you dont need to give anything in the action as you are posting it on the same 
  * page. so you can delete it. and add this line below one.
  */  

        <form action="" method="post">
            <div id='d2'>

            <p id='p2'>Subject: 
                <select id="s" name="subject">
                    <option value="English">English</option>
                    <option value="Social Studies">Social Studies</option>
                    <option value="Math">Math</option>
                    <option value="Science">Science</option>
                </select>
            </p>
            <!--<input type="button" value="Find"/>--->
            <input type="submit" value="Find"/>

        </div>
</form>

<div id='div3'>
    <?php 
//I am leaving these php tag for the reference only that you used it in your original code. 
//You dont need those lines

?>

</div>

<?php 
if ($_SERVER['REQUEST_METHOD']=='POST')
{

    $sub = $_POST['subject'];

    //$con = mysqli_connect("127.0.0.1", "root", "//removed", "mydb");
    $con = mysqli_connect("127.0.0.1", "root", "", "mydb");
    $msg = "";
    if (mysqli_connect_errno())
    {
        $msg = "Failed to connect to MySQL: " . mysqli_connect_error();
        echo $msg;
    }    
    $query = mysqli_query($con, "SELECT * FROM User WHERE Role = 'tutor' AND Subject ='".$sub."'")or die(mysqli_error($con));
   // $tutors = array(); You dont need that line either. 
    while ($row = mysqli_fetch_assoc($query))
    {
        $tutors = $row["username"];

      echo "<div>'".$tutors."'</div><br/>"
      . '<a href = "SelectedTutor.php">Choose me</a>' . "<br/>";





/*
 * **Here is the suggestion**. The link you are giving to 
 * the user is always going to be SelectedTutor.php. 
 * I dont think this what you want. you may want to 
 * show tutor profile or wanna do some thing else when
 *  somebody click on the link. Lets say you want show 
 * the tutor profile. Than you have to run another query. 
 * and should be able to generate link accordingly. 
 * I am giving you hint how you can do it in case you want to do it.
 */ 



 /*
     *  you should change that line to this line link one to this
     * echo "<div>'".$tutors."'</div><br/>"
     * . '<a href = "SelectedTutor.php?tutor='.$tutors.'">Choose me</a>' . "<br/>"; 
     * If you notcie i added this parth after SelectedTutor.php, this one ?tutor='.$tutors.'
     * Than your url will be different when ever user click on the link. 
     * Hope i did not confused you
     */

    }
}

else
{
    echo "Failed form.";
}
?>

你问为什么你会收到失败表格的消息。简而言之,为什么你的else语句正在运行。了解看下面的扩展。

if ($_SERVER['REQUEST_METHOD']=='POST')
{
//I removed the code just left the basic shell so you can understand


}

else
{
    echo "Failed form.";

}

如果您阅读上述代码,则每次运行代码时都会理解为什么会收到失败的表单消息。单击“提交”时,您将不会收到此消息。 原因:原因是这样的,在那些行中你说如果Request方法发布,那么运行这段代码。而且Else回应了这个消息。意味着只要你的请求方法没有运行此else语句。

但问题是你只需在点击按钮后发送帖子请求。在此之前,没有发布请求。这就是为什么每次运行脚本时都会收到此消息,而当您单击“提交”时它会消失。因为你发送邮件请求。

英语不是第一语言如果我困惑你让我知道我会再解释一下。

答案 1 :(得分:0)

虽然你已经请求过PHP / HTML的帮助,但这对jQuery / AJAX来说真的很重要。不过不用担心 - 它比你想象的要简单得多。

使用AJAX的优势在于您保持在同一页面上,并且页面不会刷新 - 但是下拉选择被发送到PHP文件(您指定),查询MySQL数据库和响应(以HTML格式)返回。

在AJAX成功函数中,(你听到了吗?在成功函数内部!)你会收到另一个PHP文件返回的HTML,然后你将返回的HTML插入到现有的DIV中。

以下是一些简单的例子:

AJAX request callback using jQuery


(1)您不需要将DIV d2放入<form>标签。没有必要使用AJAX

(2)您的jQuery代码如下所示:

<script type="text/javascript">
    $('#s').change(function(){
        var sub = $(this).val();
        $.ajax({
            type: 'post',
             url: 'my_php_file.php',
            data: 'subj=' +sub,
            success: function(resp){
                $('#div3').html(resp);
            }
        });
    });
</script>

上述脚本可以放在文档的任何位置,也可以放在头标记中,也可以包含在单独的文档中。

(3)根据“AJAX请求回调...”链接中的第一个示例,您必须引用jQuery库。

(4)不需要Find按钮,因为下拉值更改后代码将立即触发。与服务器通信需要几微秒,并将导师列表放在div3 div中。

(5)页面上必须已存在div3 div(但它可以为空)。

(6)PHP文件(在上面的代码中称为my_php_file.php)将与您编写的完全一样,除了它将创建一个包含要插入div3 div的HTML的输出变量。例如:

<?php 
    if ($_SERVER['REQUEST_METHOD']=='POST'){
        $sub = $_POST['subj'];

        $con = mysqli_connect("127.0.0.1", "root", "//removed", "mydb");
        $msg = "";
        if (mysqli_connect_errno()){
            $msg = "Failed to connect to MySQL: " . mysqli_connect_error();
            echo $msg;
        }    
        $query = mysqli_query($con, "SELECT * FROM User WHERE Role = tutor AND Subject ='".$sub."'");
        $tutors = array();
        while ($row = mysqli_fetch_assoc($query)) {
            $tutors[] = $row["Username"];
        }

        $out = '';
        for ($i=0; $i<count($tutors); $i++) {
              $out .= '<div>' .$tutors[$i]. '</div><br/>
                        <a href = "SelectedTutor.php">Choose me</a><br/>
                    ';
        }

    }else{
        $out = "Failed form.";
    }

    echo $out;
?>

以上所有代码均未经过测试,但可以工作......