Jump to content

Recommended Posts

Hey!

 

Im trying to do a chained select dropdown. 1 dropdown decides what should be shown in the second one...i think you all are familiar with this.

Anyway..im following this guide: http://www.yourinspirationweb.com/en/how-to-create-chained-select-with-php-and-jquery/

But something is wrong, i cant get it to work properly. 

 

My first dropdown is populating correct, but the second one is not working. If i choose something in the first dropdown, the second one just stops at "wait...". I think the problem is somewhere in add_project.php, but im not sure. Here is my code:

 

add_project.php:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){

$("select#category").change(function(){
    var id = $("select#category option:selected").attr('value');
    $.post("select_type.php", {id:id}, function(data){
        $("select#type").html(data);
        });
    });
 
            });
        </script>

<?php include "select.class.php"; ?>
 
    <form id="select_form">
        Choose a category:<br />
        <select id="category">
            <?php echo $opt->ShowCategory(); ?>
        </select>
            <br /><br />
 
           choose a type:<br />
            <select id="type">
                <option value="0">choose...</option>
            </select>
            <br /><br />
            <input type="submit" value="confirm" />
        </form>
        <div id="result"></div>

select.class.php:

<?php
class SelectList
{

      public function ShowCategory()
        {
            $sql = mysql_query("SELECT * FROM `projectCategory`");
          //  $res = mysql_query($sql,$this->conn);
            $category = '<option value="0">choose...</option>';
            while($row = mysql_fetch_array($sql))
            {
                $category .= '<option value="' . $row['categoryId'] . '">' . $row['categoryName'] . '</option>';
            }
            return $category;
        } 
 
        public function ShowType()
        {
            $query = mysql_query("SELECT huvudProjectId, huvudProjectName FROM `huvudProject` WHERE `categoryProject` = categoryId=$_POST[id]");
            $type = '<option value="0">choose...</option>';
            while($row = mysql_fetch_array($query))
            {
                $type .= '<option value="' . $row['huvudProjectId'] . '">' . $row['huvudProjectName'] . '</option>';
            }
            return $type;
        }
}
 
$opt = new SelectList();
?>

select_type.php:

<?php
include "select.class.php";
echo $opt->ShowType();
?>

Hope someone can help me with this! I've tried SO many hours but I cant find the problem.

Link to comment
https://forums.phpfreaks.com/topic/283722-problem-with-chained-select/
Share on other sites

I know that the query is OK, because i've run the method outside and it works. The second dropdown gets populated correctly. So the problem should be in the jquery-code i guess.

For me, it seems that the method never gets exececuted.

Edited by stirrah

So why would you post your question in the "PHP Applications" board? A board designed for questions relating to applications built with PHP?

 

Oh...I'm so sorry, lol. Im so used to browse this forum...is it possible to move this thread or should a make another one?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.