Results 1 to 10 of 10

Thread: How to show content in dropdown list

  1. #1
    Join Date
    Dec 2009
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to show content in dropdown list

    Hi Guys,

    I have 3 Dynamic Drop Down List.

    Brand -> Category -> Item

    if you choose Acer -> Brand
    it will only show what type of Acer Category -> Category
    after choosing what category, it will show in the last drop down list the
    available acer item.

    my problem is,
    after clicking the Item.
    i want to show it's CONTENT of the item, in my black space.


    Here's my javascript for URL:

    Code:
    <SCRIPT language=JavaScript>
    function reload(form)
    {
    var brand1=form.brand.options[form.brand.options.selectedIndex].value; 
    self.location='products.php?brand=' + brand1 ;
    }
    function reload3(form)
    {
    var brand1=form.brand.options[form.brand.options.selectedIndex].value; 
    var category2=form.category.options[form.category.options.selectedIndex].value; 
    
    self.location='products.php?brand=' + brand1 + '&model=' + category2 ;
    }
    
    </script>

    Here's my code in the 3 Drop Down List:

    PHP Code:
    <?php
    $quer2
    =mysql_query("SELECT DISTINCT brand,content_id FROM tblbrand order by brand"); 

    $brand=$HTTP_GET_VARS['brand']; 
    if(isset(
    $brand) and strlen($brand) > 0){
    $quer=mysql_query("SELECT DISTINCT category,category_id FROM tblcategory where content_id=$brand order by category"); 
    } else {
    $quer=mysql_query("SELECT DISTINCT category,category_id FROM tblcategory order by category"); } 




    $model=$HTTP_GET_VARS['model']; 
    if(isset(
    $model) and strlen($model) > 0){
    $quer3=mysql_query("SELECT DISTINCT model_id, category_id, model, content FROM tblmodel where category_id=$model order by model"); 
    } else {
    $quer3=mysql_query("SELECT DISTINCT model_id, category_id, model, content FROM tblmodel order by model"); } 




    echo 
    "<form method=get name=f1>";

    echo 
    "<select name='brand' onchange=\"reload(this.form)\"><option value=''>Select Brand</option>";
    while(
    $noticia2 mysql_fetch_array($quer2)) { 
    if(
    $noticia2['content_id']==@$brand){echo "<option selected value='$noticia2[content_id]'>$noticia2[brand]</option>"."<BR>";}
    else{echo  
    "<option value='$noticia2[content_id]'>$noticia2[brand]</option>";}
    }
    echo 
    "</select>";



    echo 
    " > <select name='category' onchange=\"reload3(this.form)\" ><option value=''>Select Category</option>";
    while(
    $noticia mysql_fetch_array($quer)) { 
    if(
    $noticia['category_id']==@$model){echo "<option selected value='$noticia[category_id]'>$noticia[category]</option>"."<BR>";}
    else{echo  
    "<option value='$noticia[category_id]'>$noticia[category]</option>";}
    }
    echo 
    "</select>";



    echo 
    " > <select name='model' ><option value=''>Select Model</option>";
    while(
    $noticia mysql_fetch_array($quer3)) { 
    echo  
    "<option value='$noticia[model]'>$noticia[model]</option>";
    }
    echo 
    "</select>";


    echo 
    "<input type=submit value='Search'></form>";

    ?>

    .......................................................................................................

    Under tblmodel in mysql, where my content is.

    ......................................................................................................
    How can i command to show its content in my black space?

    Hope somebody will help me with my dilemma.

    Thanks!
    Last edited by djr33; 12-16-2009 at 07:35 PM.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    That's a lot of code to look through. I don't really see what you want.
    Can you post an example of what you get and explain more what you want?
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Dec 2009
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Here's are the 3 dynamic drop down list.

    where i selected:

    Logitech as the Brand
    Keyboard Mouse Combo as the Category
    Internet Pro Desktop as the Model



    then after click search, the black space below will appear the model that was search.

    i would like to see in that black space are the content of the model.
    not the model name again but the content of it.

    here is my tblmodel database.



    hope you could help me.

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Not sure this is really my area of expertise, so maybe someone else will answer. I'm not thinking clearly at the moment (bit sick), so I'll try to take a look later if someone else doesn't have an answer.

    To be sure I know what you want:
    You are trying to get the search to work based on the three dropdown menus working together, so you just need to get information out of the database that matches three options chosen in the menu? Isn't that just a question of searching based on the parameters and getting the right values? Name is all you need, so that you then can get the related information (perhaps with another query if needed).
    If it's more complex, what exactly is the point at which you are stuck?
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Dec 2009
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by djr33 View Post
    Not sure this is really my area of expertise, so maybe someone else will answer. I'm not thinking clearly at the moment (bit sick), so I'll try to take a look later if someone else doesn't have an answer.

    To be sure I know what you want:
    You are trying to get the search to work based on the three dropdown menus working together, so you just need to get information out of the database that matches three options chosen in the menu? Isn't that just a question of searching based on the parameters and getting the right values? Name is all you need, so that you then can get the related information (perhaps with another query if needed).
    If it's more complex, what exactly is the point at which you are stuck?
    yup, based on the three drop down list i want to have the content field inside of tblmodel.

    i just want to have the content on the last selected drop down list and the content should not appear on the URL.

    hope you could help me, i dont know what to do.

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    There are two methods for submitting forms: "get" and "post". Get places the values in the url, and post sends them as separate data with the page request.
    <form method="post" ....>
    That is all you need to hide it from the url.

    Once you do that, the values sent will be available in php as:
    $_POST['field_name']

    Just query the database using standard techniques to find information that matches the search:
    SELECT * FROM `...` WHERE `a`='$a' AND `b`='$b' AND `c`='$c';
    (a-c are the fields sent from the form)

    Then get the data and echo the values of whatever you want (mysql_fetch_array(), for example) wherever you want in the html code.

    Note: you should escape/check the data sent by the user, because it could be a potential security risk:
    1. Before ever using user submitted data in a query, you should always run mysql_real_escape_string() on it, so that you don't run into problems with someone submitting harmful data that could potentially delete or modify your database.
    2. You should also allow for error checking in case an item is not found. (mysql_num_rows() will let you know if there were any results found, and if so you can display them; if not, just display "not found"). Even if your dropdowns don't have any possible combinations that would not be found in the database, then the user could submit random data as a hacking attempt, etc.

    Does that answer everything?
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Dec 2009
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by djr33 View Post
    There are two methods for submitting forms: "get" and "post". Get places the values in the url, and post sends them as separate data with the page request.
    <form method="post" ....>
    That is all you need to hide it from the url.

    Once you do that, the values sent will be available in php as:
    $_POST['field_name']

    Just query the database using standard techniques to find information that matches the search:
    SELECT * FROM `...` WHERE `a`='$a' AND `b`='$b' AND `c`='$c';
    (a-c are the fields sent from the form)

    Then get the data and echo the values of whatever you want (mysql_fetch_array(), for example) wherever you want in the html code.

    Note: you should escape/check the data sent by the user, because it could be a potential security risk:
    1. Before ever using user submitted data in a query, you should always run mysql_real_escape_string() on it, so that you don't run into problems with someone submitting harmful data that could potentially delete or modify your database.
    2. You should also allow for error checking in case an item is not found. (mysql_num_rows() will let you know if there were any results found, and if so you can display them; if not, just display "not found"). Even if your dropdowns don't have any possible combinations that would not be found in the database, then the user could submit random data as a hacking attempt, etc.

    Does that answer everything?
    i don't know how to execute in programming.

    can you edit the code above so i could try it?

    i'm just a beginner in php.

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    In that case, I recommend a good tutorial. One that I liked when I was learning (and still refer to) is here:
    http://php-mysql-tutorial.com
    Note that the best/beginning articles are the last ones when you look at them, since the newest articles are first-- don't be put off by the slightly confusing new layout-- it's a good tutorial.

    You could also pay someone to set this up, but it's difficult to work within a system for volunteer work. The code is not hard, but the setup within your code might be. All of the steps are listed above, so you just need to implement them.

    You need to use a method="post" form, and then all the data will be available in php as $_POST['fieldname'], as I said above.
    Queries in mysql are done with mysql_query(), and you can find lots of info about how to use that at php.net and the tutorial above.

    Nothing you are doing is unusual, so there is lots of info to get you started if you look for it.

    The only advanced part of this is dealing with escaping data to prevent security issues in the database, so if you want to post your code someone here can point out what needs to be done there.
    In short, you should not use any user-submitted data directly in a query. Instead, escape it (that means removing dangerous characters). The easiest way to do that is to use the following for everything submitted by the user:
    $variable = mysql_real_escape_string($variable);

    That should be applied in the following line (and all like it) from your original code:
    $brand=$HTTP_GET_VARS['brand'];


    Also, re-reading your first post, I'm not clear on whether you want the form to submit and the page to reload or if you want the page to NOT reload and the product to appear.
    Forms must be submitted for the PHP to automatically work. The only way around that is using Javascript (through the method called "Ajax") to do a background request to the server and get the results. That's an entirely different issue and would use the same PHP (except it would only output the extra content for the Javascript to add to the page).
    In this case I don't see any reason you should use Ajax, but if you are sure you want to, there are also plenty of tutorials on that. Start with the other code, then add in Ajax as the last step once it's all working.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #9
    Join Date
    Dec 2009
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hey thanks for the help but i finally figured it out.

    now my only problem is when i want to update the content.

  10. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Again, look at the same tutorial and there's plenty of information for the "UPDATE" command in mysql.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •