简体   繁体   English

如何把每个 <select><option>将组合框转换为PHP中的变量

[英]how to put every<select><option> combobox into a variable in PHP

I have a little problem with the output of my PHP script. 我的PHP脚本的输出有点问题。

The purpose of this page is to select one of the ticket status options, such as: 此页面的目的是选择票证状态选项之一,例如:

  • Open Tickets (display tickets from database with the status "New Tickets" OR "Under Review") 打开工单(显示状态为“新工单”或“正在审核”的数据库工单)
  • New Tickets (display tickets from database with the status "New Tickets") 新票证(显示状态为“新票证”的数据库中的票证)
  • Under Review (display tickets from database with the status "Under Review") 审核中(显示状态为“审核中”的数据库票证)
  • Closed Tickets (display tickets from database with the status (Closed Tickets) 已关闭的票证(显示状态为已关闭的票证的数据库中的票证)

After user selects a status, code must display the correct Tickets with chosen status. 用户选择状态后,代码必须显示具有所选状态的正确票证。

So what i tried to do is place a number variable after every option and then try to make it so that when something is selected, refresh the page and send the variable of the option with the page refresh - except that it doesn't work, lol. 因此,我试图做的是在每个选项之后放置一个数字变量,然后尝试使其变为选中状态,从而刷新页面,并在页面刷新时发送该选项的变量-除非它不起作用,大声笑。

I've been brainstorming some time with different ideas to let it check the variable it obtains from selecting a status in the combobox, yet i haven't figured it out which is pretty frustrating. 我一直在集思广益,用不同的想法来检查它从组合框中选择状态所获得的变量,但是我还没有弄清楚这是多么令人沮丧。

Important points: 要点:

  • The script has to work on the same page. 该脚本必须在同一页面上工作。 Making a new page for every different status is a lot of work and not very efficiënt. 为每个不同的状态创建一个新页面是一项繁重的工作,而且效率不高。
  • PHP is the main script language so i prefer the answer in PHP PHP是主要的脚本语言,所以我更喜欢PHP中的答案

Here below is the current code: 下面是当前代码:

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

?>

Two ideas: 两个想法:

a) You can do this by implementing some javascript, which switches display status of each ticket after selection changed. a)您可以通过实现一些javascript来实现,这些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) Redirect to this very page with some url parameter indiciates which ticket is selected, and display the corresponding one. 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>

Here is an UNTESTED (I don't have your database) example of how AJAX will work for you. 这是AJAX如何为您工作的一个未测试(我没有您的数据库)示例。

Forgive me if you already know this, but AJAX is nothing fancy - it's a simple method that allows you to call another PHP file from javascript. 如果您已经知道这一点,请原谅我,但是AJAX没什么花哨的-它是一种简单的方法,允许您从javascript调用另一个PHP文件。

AJAX works by having a second PHP file that does the next bit of work, and returns the result to javascript. AJAX的工作原理是拥有另一个PHP文件,该文件将完成下一部分工作,并将结果返回给javascript。 Here's how the flow goes: 流程如下:

In File1, javascript/jQuery grabs the option selected by the user and "calls" (AJAX) the 2nd PHP file, passing along the user's choice. 在File1中,javascript / jQuery抓住用户选择的选项,并“调用”(AJAX)第二个PHP文件,同时传递用户的选择。 The code that makes that happen is under this bit: 实现这一目标的代码在此位下:

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

File2 (named, in this example, "another_php_file.php") (a) receives the POSTed value sent from File1, (b) performs the database lookup, (c) builds the new HTML, and (d) ECHOs the constructed code (in a variable I called $x) back to File1 File2(在此示例中命名为“ another_php_file.php”)(a)接收从File1发送的POSTed值,(b)执行数据库查找,(c)构建新的HTML,以及(d)回显所构造的代码(在我称为$ x的变量中)返回File1

Now, this is very important: File1 RECEIVES the new code inside the AJAX routine's success: function. 现在,这非常重要:File1接收AJAX例程success:函数中的新代码success:函数。 INSIDE THIS SUCCESS FUNCTION it can use jQuery to update a div in File1. 在此成功功能中,它可以使用jQuery更新File1中的div。 If you want the user to push other buttons that you can use to do another AJAX lookup, the next AJAX lookup must also be inside this success function. 如果您希望用户按下可用于执行其他AJAX查找的其他按钮,则下一个AJAX查找也必须位于此成功函数之内。

Anyway, this should answer your immediate question: 无论如何,这应该可以回答您的直接问题:


FILE 1 文件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 - must be called "another_php_file.php" in this example. 文件2-在此示例中必须称为“ another_php_file.php”。 If change name, must also change name of file in the AJAX code segment above: 如果要更改名称,还必须在上面的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;

At first glance this may all look pretty complicated, so here is another example of AJAX , much simplified. 乍一看,这看起来似乎很复杂,所以这里是AJAX的另一个示例 ,它大大简化了。 Please copy/paste it into files on your own server and see how it works. 请复制/粘贴到您自己的服务器上的文件中,并查看其工作方式。 It's really very simple. 这真的非常简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM