如何将每个<select> <option>组合框放入PHP </option> </select>中的变量中

时间:2013-06-06 15:39:34

标签: php html variables

我的PHP脚本输出有点问题。

此页面的目的是选择其中一个故障单状态选项,例如:

  • 打开故障单(显示状态为“新故障单”或“正在审核”的数据库中的故障单)
  • 新票证(显示状态为“新票证”的数据库票证)
  • 正在审核(显示状态为“审核中”的数据库中的故障单)
  • 已关闭的门票(显示来自数据库的门票,状态为(已关闭的门票)

用户选择状态后,代码必须显示具有所选状态的正确票证。

所以我试图做的是在每个选项之后放置一个数字变量然后尝试使它在选择某些内容时刷新页面并通过页面刷新发送选项的变量 - 除了它没有'工作,哈哈。

我一直在用不同的想法集思广益,让它检查从组合框中选择一个状态所获得的变量,但我还没弄清楚这是非常令人沮丧的。

重点:

  • 脚本必须在同一页面上工作。为每个不同的状态创建一个新页面是很多工作而且效率不高。
  • PHP是主要的脚本语言,所以我更喜欢PHP中的答案

以下是当前代码:

<?PHP
session_start();
include('connection.php');

if ($_SESSION['staff'] >='6')
{

    echo "<select name='type' id='type'>
    <option value='Open Tickets'>Open Tickets</option>" . $int= '1' . "
    <option value='New Tickets'>New Tickets</option>" . $int= '2' . "
    <option value='Under Review'>Under Review</option>" . $int = '3' . "
    <option value='Closed Tickets'>Closed Tickets</option>" . $int = '4' . "
    </select><BR>"; 
    echo "Current listed support tickets: <BR><BR>";

        if ($int = '1')
        {
            $result1 = mysql_query("SELECT * FROM support WHERE status = 'New Ticket' OR 'Under Review'");
            while ($row1 = mysql_fetch_array($result1))
            {
                $ticket1 = $row1['TicketID'];
                $user1 = $row1['name'];
                echo "Ticket ID: " . $row1['TicketID'] . "<BR>" . $row1['type'] . "<BR>Subject: " . $row1['subject'] . "<BR>Ticket Status: " . $row1['status'] . "<BR>";
                echo "<form action='detail_ticket.php?t=$ticket1' method='post' name='detail_t' id='detail_t'><input type='submit' name='detail_b' id='detail_b' value='View Ticket!'></form>";
                echo "<form action='reply_ticket.php?r=$user1' method='post' name='r_ticket' id='r_ticket'><input type='submit' name='reply_b' id='reply_b' value='Reply on Ticket!'></form>";
            }

        }elseif ($int = '2')
        {

            $result2 = mysql_query("SELECT * FROM support WHERE status = 'New Ticket'");
            while ($row2 = mysql_fetch_array($result2))
            {
                $ticket2 = $row2['TicketID'];
                $user2 = $row2['name'];
                echo "Ticket ID: " . $row2['TicketID'] . "<BR>" . $row2['type'] . "<BR>Subject: " . $row2['subject'] . "<BR>Ticket Status: " . $row2['status'] . "<BR>";
                echo "<form action='detail_ticket.php?t=$ticket2' method='post' name='detail_t' id='detail_t'><input type='submit' name='detail_b' id='detail_b' value='View Ticket!'></form>";
                echo "<form action='reply_ticket.php?r=$user2' method='post' name='r_ticket' id='r_ticket'><input type='submit' name='reply_b' id='reply_b' value='Reply on Ticket!'></form>";
            }

        }elseif ($int = '3')
        {
            $result3 = mysql_query("SELECT * FROM support WHERE status = 'New Ticket'");
            while ($row3 = mysql_fetch_array($result3))
            {
                $ticket3 = $row3['TicketID'];
                $user3 = $row3['name'];
                echo "Ticket ID: " . $row3['TicketID'] . "<BR>" . $row3['type'] . "<BR>Subject: " . $row3['subject'] . "<BR>Ticket Status: " . $row3['status'] . "<BR>";
                echo "<form action='detail_ticket.php?t=$ticket3' method='post' name='detail_t' id='detail_t'><input type='submit' name='detail_b' id='detail_b' value='View Ticket!'></form>";
                echo "<form action='reply_ticket.php?r=$user3' method='post' name='r_ticket' id='r_ticket'><input type='submit' name='reply_b' id='reply_b' value='Reply on Ticket!'></form>";
            }

        }elseif ($int = '4')
        {
            $result4 = mysql_query("SELECT * FROM support WHERE status = 'Closed'");
            while ($row4 = mysql_fetch_array($result4))
            {
                $ticket4 = $row4['TicketID'];
                $user4 = $row4['name'];
                echo "Ticket ID: " . $row4['TicketID'] . "<BR>" . $row4['type'] . "<BR>Subject: " . $row4['subject'] . "<BR>Ticket Status: " . $row4['status'] . "<BR>";
                echo "<form action='detail_ticket.php?t=$ticket4' method='post' name='detail_t' id='detail_t'><input type='submit' name='detail_b' id='detail_b' value='View Ticket!'></form>";
                echo "<form action='reply_ticket.php?r=$user4' method='post' name='r_ticket' id='r_ticket'><input type='submit' name='reply_b' id='reply_b' value='Reply on Ticket!'></form>";
            }
        }


    echo "Click <A href='overview_page.php'>HERE</A> to return to the overview page.<BR>";
    echo "Click <A href='uitlogen_user.php'>HERE</A> to logout!<BR>";
}else
{
    echo "You need to be an GM or Admin to view this page!<BR>";
    echo "Click <A href='uitlogen_user.php'>HERE</A> to login!";
}

?>

2 个答案:

答案 0 :(得分:0)

两个想法:

a)您可以通过实施一些javascript来实现这一点,该选项会在选择更改后切换每个票证的显示状态。     

if ($_SESSION['staff'] >='6')
{

    echo "<select name='type' id='type' onchange='typeChanged()' onpropertychange='typeChanged()'>
    <option value='tab1'>Open Tickets</option>
    <option value='tab2'>New Tickets</option>
    <option value='tab3'>Under Review</option>
    <option value='tab4'>Closed Tickets</option>
    </select><BR>"; 
    echo "Current listed support tickets: <BR><BR>";

    echo "<div id="tab1" name="tabs">";
            $result1 = mysql_query("SELECT * FROM support WHERE status = 'New Ticket' OR 'Under Review'");
            while ($row1 = mysql_fetch_array($result1))
            {
                $ticket1 = $row1['TicketID'];
                $user1 = $row1['name'];
                echo "Ticket ID: " . $row1['TicketID'] . "<BR>" . $row1['type'] . "<BR>Subject: " . $row1['subject'] . "<BR>Ticket Status: " . $row1['status'] . "<BR>";
                echo "<form action='detail_ticket.php?t=$ticket1' method='post' name='detail_t' id='detail_t'><input type='submit' name='detail_b' id='detail_b' value='View Ticket!'></form>";
                echo "<form action='reply_ticket.php?r=$user1' method='post' name='r_ticket' id='r_ticket'><input type='submit' name='reply_b' id='reply_b' value='Reply on Ticket!'></form>";
            }
    echo "</div>";

    echo "<div id="tab2" name="tabs">";
            $result2 = mysql_query("SELECT * FROM support WHERE status = 'New Ticket'");
            while ($row2 = mysql_fetch_array($result2))
            {
                $ticket2 = $row2['TicketID'];
                $user2 = $row2['name'];
                echo "Ticket ID: " . $row2['TicketID'] . "<BR>" . $row2['type'] . "<BR>Subject: " . $row2['subject'] . "<BR>Ticket Status: " . $row2['status'] . "<BR>";
                echo "<form action='detail_ticket.php?t=$ticket2' method='post' name='detail_t' id='detail_t'><input type='submit' name='detail_b' id='detail_b' value='View Ticket!'></form>";
                echo "<form action='reply_ticket.php?r=$user2' method='post' name='r_ticket' id='r_ticket'><input type='submit' name='reply_b' id='reply_b' value='Reply on Ticket!'></form>";
            }
    echo "</div>";

    echo "<div id="tab3" name="tabs">";
            $result3 = mysql_query("SELECT * FROM support WHERE status = 'New Ticket'");
            while ($row3 = mysql_fetch_array($result3))
            {
                $ticket3 = $row3['TicketID'];
                $user3 = $row3['name'];
                echo "Ticket ID: " . $row3['TicketID'] . "<BR>" . $row3['type'] . "<BR>Subject: " . $row3['subject'] . "<BR>Ticket Status: " . $row3['status'] . "<BR>";
                echo "<form action='detail_ticket.php?t=$ticket3' method='post' name='detail_t' id='detail_t'><input type='submit' name='detail_b' id='detail_b' value='View Ticket!'></form>";
                echo "<form action='reply_ticket.php?r=$user3' method='post' name='r_ticket' id='r_ticket'><input type='submit' name='reply_b' id='reply_b' value='Reply on Ticket!'></form>";
            }
    echo "</div>";

    echo "<div id="tab4" name="tabs">";
            $result4 = mysql_query("SELECT * FROM support WHERE status = 'Closed'");
            while ($row4 = mysql_fetch_array($result4))
            {
                $ticket4 = $row4['TicketID'];
                $user4 = $row4['name'];
                echo "Ticket ID: " . $row4['TicketID'] . "<BR>" . $row4['type'] . "<BR>Subject: " . $row4['subject'] . "<BR>Ticket Status: " . $row4['status'] . "<BR>";
                echo "<form action='detail_ticket.php?t=$ticket4' method='post' name='detail_t' id='detail_t'><input type='submit' name='detail_b' id='detail_b' value='View Ticket!'></form>";
                echo "<form action='reply_ticket.php?r=$user4' method='post' name='r_ticket' id='r_ticket'><input type='submit' name='reply_b' id='reply_b' value='Reply on Ticket!'></form>";
            }
    echo "</div>";


    echo "Click <A href='overview_page.php'>HERE</A> to return to the overview page.<BR>";
    echo "Click <A href='uitlogen_user.php'>HERE</A> to logout!<BR>";
}else
{
    echo "You need to be an GM or Admin to view this page!<BR>";
    echo "Click <A href='uitlogen_user.php'>HERE</A> to login!";
}

?>

<script type="text/javascript">
function typeChanged()
{
    var chosen = document.getElementById('type').value;
    var tabs = document.getElementsByName('tabs');
    for (var i in tabs)
    {
        tabs[i].style.display = 'none';
    }
    document.getElementById(chosen).style.display = 'block';
}
</script>

b)重定向到此页面,其中一些url参数指示选择了哪个故障单,并显示相应的故障单。

<?PHP
session_start();
include('connection.php');

if ($_SESSION['staff'] >='6')
{
    $chosen = isset($_GET['chosen']) ? $_GET['chosen'] : 'tab1';

    echo "<select name='type' id='type' onchange='typeChanged()' onpropertychange='typeChanged()'>
    <option value='tab1'>Open Tickets</option>
    <option value='tab2'>New Tickets</option>
    <option value='tab3'>Under Review</option>
    <option value='tab4'>Closed Tickets</option>
    </select><BR>"; 
    echo "Current listed support tickets: <BR><BR>";

    if ($chosen == 'tab1')
    {
            $result1 = mysql_query("SELECT * FROM support WHERE status = 'New Ticket' OR 'Under Review'");
            while ($row1 = mysql_fetch_array($result1))
            {
                $ticket1 = $row1['TicketID'];
                $user1 = $row1['name'];
                echo "Ticket ID: " . $row1['TicketID'] . "<BR>" . $row1['type'] . "<BR>Subject: " . $row1['subject'] . "<BR>Ticket Status: " . $row1['status'] . "<BR>";
                echo "<form action='detail_ticket.php?t=$ticket1' method='post' name='detail_t' id='detail_t'><input type='submit' name='detail_b' id='detail_b' value='View Ticket!'></form>";
                echo "<form action='reply_ticket.php?r=$user1' method='post' name='r_ticket' id='r_ticket'><input type='submit' name='reply_b' id='reply_b' value='Reply on Ticket!'></form>";
            }
    }

    if ($chosen == 'tab2')
    {
            $result2 = mysql_query("SELECT * FROM support WHERE status = 'New Ticket'");
            while ($row2 = mysql_fetch_array($result2))
            {
                $ticket2 = $row2['TicketID'];
                $user2 = $row2['name'];
                echo "Ticket ID: " . $row2['TicketID'] . "<BR>" . $row2['type'] . "<BR>Subject: " . $row2['subject'] . "<BR>Ticket Status: " . $row2['status'] . "<BR>";
                echo "<form action='detail_ticket.php?t=$ticket2' method='post' name='detail_t' id='detail_t'><input type='submit' name='detail_b' id='detail_b' value='View Ticket!'></form>";
                echo "<form action='reply_ticket.php?r=$user2' method='post' name='r_ticket' id='r_ticket'><input type='submit' name='reply_b' id='reply_b' value='Reply on Ticket!'></form>";
            }
    }

    if ($chosen == 'tab3')
    {
            $result3 = mysql_query("SELECT * FROM support WHERE status = 'New Ticket'");
            while ($row3 = mysql_fetch_array($result3))
            {
                $ticket3 = $row3['TicketID'];
                $user3 = $row3['name'];
                echo "Ticket ID: " . $row3['TicketID'] . "<BR>" . $row3['type'] . "<BR>Subject: " . $row3['subject'] . "<BR>Ticket Status: " . $row3['status'] . "<BR>";
                echo "<form action='detail_ticket.php?t=$ticket3' method='post' name='detail_t' id='detail_t'><input type='submit' name='detail_b' id='detail_b' value='View Ticket!'></form>";
                echo "<form action='reply_ticket.php?r=$user3' method='post' name='r_ticket' id='r_ticket'><input type='submit' name='reply_b' id='reply_b' value='Reply on Ticket!'></form>";
            }
    }

    if ($chosen == 'tab4')
    {
            $result4 = mysql_query("SELECT * FROM support WHERE status = 'Closed'");
            while ($row4 = mysql_fetch_array($result4))
            {
                $ticket4 = $row4['TicketID'];
                $user4 = $row4['name'];
                echo "Ticket ID: " . $row4['TicketID'] . "<BR>" . $row4['type'] . "<BR>Subject: " . $row4['subject'] . "<BR>Ticket Status: " . $row4['status'] . "<BR>";
                echo "<form action='detail_ticket.php?t=$ticket4' method='post' name='detail_t' id='detail_t'><input type='submit' name='detail_b' id='detail_b' value='View Ticket!'></form>";
                echo "<form action='reply_ticket.php?r=$user4' method='post' name='r_ticket' id='r_ticket'><input type='submit' name='reply_b' id='reply_b' value='Reply on Ticket!'></form>";
            }
    }


    echo "Click <A href='overview_page.php'>HERE</A> to return to the overview page.<BR>";
    echo "Click <A href='uitlogen_user.php'>HERE</A> to logout!<BR>";
}else
{
    echo "You need to be an GM or Admin to view this page!<BR>";
    echo "Click <A href='uitlogen_user.php'>HERE</A> to login!";
}

?>

<script type="text/javascript">
function typeChanged()
{
    var chosen = document.getElementById('type').value;
    window.location.href = 'FILENAME.php?chosen=' + chosen;
}
</script>

答案 1 :(得分:0)

这是一个UNTESTED(我没有你的数据库)AJAX如何为你工作的例子。

原谅我,如果你已经知道这一点,但AJAX并不是什么花哨的 - 这是一个简单的方法,允许你从javascript调用另一个PHP文件。

AJAX通过使用第二个PHP文件来完成下一步的工作,并将结果返回给javascript。流程如下:

在File1中,javascript / jQuery抓取用户选择的选项,并“调用”(AJAX)第二个PHP文件,传递用户的选择。实现这一目标的代码就是这样:

$('#type').change( function() { //grab the new value, send to File2 via AJAX });

File2(在本例中命名为“another_php_file.php”)(a)接收从File1发送的POSTed值,(b)执行数据库查找,(c)构建新的HTML,以及(d)ECHO构造的代码(在一个名为$ x的变量中)返回File1

现在,这非常重要:File1在AJAX例程的success:函数中接收新代码。在这个成功功能中,它可以使用jQuery来更新File1中的div。如果您希望用户按下可用于执行另一个AJAX查找的其他按钮,则下一个AJAX查找也必须位于此成功函数内。

无论如何,这应该回答你当前的问题:


文件1

<?php
    session_start();
    include('connection.php');
?>
<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
        <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />

        <script type="text/javascript">
            $(document).ready(function() {

                $('#type').change( function() {
                    var chosen = $(this).val();
                    alert('You chose: ' + chosen);

                    $.ajax({
                        type: "POST",
                        url: "another_php_file.php",
                        data: 'theOption=' + chosen,
                        success: function(whatigot) {
//alert('Server-side response: ' + whatigot);
                            $('#report').html(whatigot);
                            $('#theButton').click(function() {
                                alert('You clicked the button');
                            });
                        } //END success fn
                    }); //END $.ajax
                }); //END dropdown change event
            }); //END $(document).ready()

        </script>
    </head>
<body>

<?php

if ($_SESSION['staff'] >='6')
{
    //Note: the value text string MUST MATCH the value stored in the database, because it is this exact string you will search for in the 2nd php (AJAX) file.
    echo "<select name='type' id='type'>
        <option value='Open Ticket'>Open Tickets</option>
        <option value='New Ticket'>New Tickets</option>
        <option value='Under Review'>Under Review</option>
        <option value='Closed'>Closed Tickets</option>
    </select><br><br>
    <div id='report'></div>"; 

}else
{
    echo "You need to be an GM or Admin to view this page!<BR>";
    echo "Click <A href='uitlogen_user.php'>HERE</A> to login!";
}

?>

FILE 2 - 在此示例中必须调用“another_php_file.php”。如果更改名称,还必须在上面的AJAX代码段中更改文件名:

<?php
$chosen = $_POST['theOption'];
//die('You clicked: ' . $chosen);

if ($chosen == 'New Ticket') {
    $result = mysql_query("SELECT * FROM support WHERE status = '$chosen' OR 'Under Review'");
    while ($row1 = mysql_fetch_array($result1))
    {
        $ticket1 = $row1['TicketID'];
        $user1 = $row1['name'];
        $x =  "Ticket ID: " . $row1['TicketID'] . "<BR>" . $row1['type'] . "<BR>Subject: " . $row1['subject'] . "<BR>Ticket Status: " . $row1['status'] . "<BR>";
        $x .= "<form action='detail_ticket.php?t=$ticket1' method='post' name='detail_t' id='detail_t'><input type='submit' name='detail_b' id='detail_b' value='View Ticket!'></form>";
        $x .= "<form action='reply_ticket.php?r=$user1' method='post' name='r_ticket' id='r_ticket'><input type='submit' name='reply_b' id='reply_b' value='Reply on Ticket!'></form>";
    }
}else{
    $result = mysql_query("SELECT * FROM support WHERE status = '$chosen'");
    while ($row1 = mysql_fetch_array($result1))
    {
        $ticket1 = $row1['TicketID'];
        $user1 = $row1['name'];
        $x = "Ticket ID: " . $row1['TicketID'] . "<BR>" . $row1['type'] . "<BR>Subject: " . $row1['subject'] . "<BR>Ticket Status: " . $row1['status'] . "<BR>";
        $x .= "<form action='detail_ticket.php?t=$ticket1' method='post' name='detail_t' id='detail_t'><input type='submit' name='detail_b' id='detail_b' value='View Ticket!'></form>";
        $x .= "<form action='reply_ticket.php?r=$user1' method='post' name='r_ticket' id='r_ticket'><input type='submit' name='reply_b' id='reply_b' value='Reply on Ticket!'></form>";
    }
}

$x .= "Click <A href='overview_page.php'>HERE</A> to return to the overview page.<br>";
$x .= "Click <A href='uitlogen_user.php'>HERE</A> to logout!<br><br>";
$x .= "<input type='button' id='theButton' value='Click Me Example'>";

echo $x;

乍一看,这看起来可能都很复杂,所以here is another example of AJAX非常简化。请将其复制/粘贴到您自己的服务器上的文件中,看看它是如何工作的。这真的很简单。