简体   繁体   中英

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

I have a little problem with the output of my PHP script.

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

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.

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.

<?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.

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 works by having a second PHP file that does the next bit of work, and returns the result to 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. 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

Now, this is very important: File1 RECEIVES the new code inside the AJAX routine's success: function. INSIDE THIS SUCCESS FUNCTION it can use jQuery to update a div in File1. 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.

Anyway, this should answer your immediate question:


FILE 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. If change name, must also change name of file in the AJAX code segment above:

<?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. Please copy/paste it into files on your own server and see how it works. It's really very simple.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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