简体   繁体   中英

PHP option tag and array

Just working on a small University project to develop an e-commerce website. I have been provided some code and I am altering this to my needs.

I currently have a problem with getting some arrays to work within an option and select tag. The difference being each of these arrays has an action associated with it eg, moving onto another page.

The problem seems to be this action is not being used within the option tags. As seen here http://mkiddr.com/phptests/shopping/

Here is my original code before editing:

<body>
<div id="wrapper">
<div id="header">
    <p><a href="index.php">Home</a> | Browse:
    <?php
        $q="SELECT c_id, c_name FROM sc_cat";
        $result = mysqli_query($_SESSION['conn'],$q);
        while ($row = mysqli_fetch_row($result)){
            echo "<a href='category.php?id=$row[0]'>$row[1]</a> ";
        }
        unset($q); //unset query variable
        mysqli_free_result($result); //free result
        ?>
</div>
<div id="content"><!-- note this is an opening tag -->

Here is my code edited (doesn't work)

<body>
<div id="wrapper">
<div id="header">
    <p><a href="index.php">Home</a> | Browse:

    <select>
    <?php
        $q="SELECT CategoryID, CategoryName FROM ProductCategories";
        $result = mysqli_query($_SESSION['conn'],$q);
        while ($row = mysqli_fetch_row($result)){

            echo "<option value='category.php?id=".$row[0]."'>".$row[1]."</a></option>";
            //display categories

            }
        unset($q); //unset query variable
        mysqli_free_result($result); //free result
    ?>
    </select>

</div>
<div id="content"><!-- note this is an opening tag -->

Would really appreciate if someone could give us a hand with this!

The problem is that option tag has no "action" to execute. You must use javascript to redirect user to selected option. Change SELECT tag to

<select onchange="window.location = this.value;">

try swopping out your while loop for the following:

   while ($row = $result->fetch_row()) {

        echo "<option value='category.php?id=".$row[0]."'>".$row[1]."</a></option>";
        //display categories

        }

And see if this should work

As @Stormherz mentions also to have this load automatically on selection you need to place the onclick method to the option on the select tag;

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