SitePoint Sponsor

User Tag List

Page 1 of 3 123 LastLast
Results 1 to 25 of 52

Thread: Need Help

  1. #1
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need Help

    I have this form where the dropdown list contains names of Video Games...The list menu will contain the MAPS or BOARDS that are associated with that game. So when the user selects say...Counter-Strike the list menu is populated with all the possible MAPS that are associated with the game...The user can select all the maps he wants and then click SUBMIT...at that time...the Game name all the maps...and other stuff entered from the form...get posted on a db... I am creating a RECENT MATCHES for clan site.

    I am thinking I need a javascript..But I don't know how to implement the js with the php.

  2. #2
    SitePoint Evangelist djdykes's Avatar
    Join Date
    Feb 2005
    Location
    Chester, Cheshire
    Posts
    565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah i think JS would be the way to go with something like this... or .NET (boo!) you need to automatically postback the form right?

  3. #3
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,034
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)

    PHP with Javascript

    To use PHP variables in javascript do something like this
    PHP Code:
    <script .......
    var jsVar = <?php echo $phpVar?>;
    .....

  4. #4
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yes thats a javascript job.

    You'll need to make an array for each item in your first dropbox, with an element for each option you want in the second dropbox when selected.

    Name your form and both dropboxes and the javascript can then update the second dropbox contents from the array when an item has been chosen from the first dropbox.

    Example
    Code:
    <script language="JavaScript">
    <!--
    var games=document.formname.games.options.length
    var game=new Array(games)
    for (i=0; i<games; i++)
    game[i]=new Array()
    
    game[0][0]=new Option("optiontext","optionvalue")
    game[0][1]=new Option("optiontext","optionvalue")
    game[0][2]=new Option("optiontext","optionvalue")
    game[1][0]=new Option("optiontext","optionvalue")
    game[1][1]=new Option("optiontext","optionvalue")
    game[1][2]=new Option("optiontext","optionvalue")
    game[1][3]=new Option("optiontext","optionvalue")
    game[2][0]=new Option("optiontext","optionvalue")
    game[2][1]=new Option("optiontext","optionvalue")
    game[2][2]=new Option("optiontext","optionvalue")
    game[2][3]=new Option("optiontext","optionvalue")
    game[3][0]=new Option("optiontext","optionvalue")
    game[3][1]=new Option("optiontext","optionvalue")
    game[3][2]=new Option("optiontext","optionvalue")
    game[3][3]=new Option("optiontext","optionvalue")
    game[4][0]=new Option("optiontext","optionvalue")
    game[4][1]=new Option("optiontext","optionvalue")
    
    
    var temp=document.formname.boards
    function getboards(x){
    for (m=temp.options.length-1;m>0;m--)
    temp.options[m]=null
    for (i=0;i<game[x].length;i++){
    temp.options[i]=new Option(game[x][i].text,game[x][i].value)
    }
    temp.options[0].selected=true
    }
    //-->
    </script>
    In the above I assume that the form will be named 'formname'
    the first dropbox will be named 'games'
    the second dropbox will be named 'boards'
    change optiontext and optionvalue to suit your data

    The handler
    Code:
    onChange="getboards(this.options.selectedIndex)"
    should be added to the select statement for the games dropbox, this will invoke the javascript when you change the value.

    Hope it helps

    Terry

    Edit:
    Forgot to say, once you get it working you can script a douple loop in PHP to build your array from the contents of the database.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  5. #5
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ACtually...I appreciate your help guys...But I have decided to NOT use a javascript approach..Instead I am keeping it with JUST php...Here is how I am doing it..
    First I am using a paging method to better fit my needs..

    my main code
    Code:
    <?php
    ###Matches Input Script###
    require_once ('database.php');
    require_once ('func2.inc.php');
    if (!$_REQUEST['m'] == "1" || !$_REQUEST['m'] == "2" || !$_REQUEST['m'] == "3"){
    echo "<form name=match_game method=post action=?m=2>";
    dropdownform();
    echo "<input type=submit name=next value=\"Next Step\">";
    echo "</form>";
    }
    #########################Start of M-2###############################
    if ($_REQUEST['m'] == "2") {
    
    $result = mysql_query("SELECT gameid,game_name FROM games WHERE gameid = '$game'");
    if (!$result) {
       echo 'Could not run query: ' . mysql_error();
       exit;
    }
    $row = mysql_fetch_row($result);
    
    echo "Maps Available for <strong>" . $row[1];
    echo"</strong>";
    echo "<form name=match_maps method=post action=?m=3>";
    echo "<input type=text value=\"$row[1]\"><br>";
    ListMenuForm ();
    echo "<input type=submit name=next value=\"Next Step\">";
    echo "</form>";
    }
    
    
    
    ?>
    And here is my func1.inc.php
    Code:
    <?php
    
    function dropdownform ()
    {
        $query = mysql_query( "SELECT * FROM `games` ORDER BY `gameid` ASC");
    
        echo "<select name=\"game\">";
    
        while ( $array = mysql_fetch_array( $query ) )
        {
    
             echo "      <option value={$array['gameid']}>{$array['game_name']}</option>";
        }
    
        echo "</select>";
    }
    
    
    
    
    function ListMenuForm ()
    {
        $query = mysql_query( "SELECT map FROM maps WHERE game_id = '$game'");
        
        echo "<select name=\"map\" size=\"5\" multiple>";
    
        while ( $array = mysql_fetch_array( $query ) )
        {
          $row1 = mysql_fetch_row($result);
             echo "      <option>{$row1[1]}</option>";
        }
    
        echo "</select>";
        
    }
    ?>
    I am having trouble with the function ListMenuForm () for some reason I cannot insert the correct data.

  6. #6
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thats cluncky, and cheating !!!!
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  7. #7
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you mean clunky? lol

  8. #8
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Refreshing screen when no need to, breaks the flow of the user, hence clunky !

    The difference between a round wheel and a triangular one.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  9. #9
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    UK
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    some nice xml http requests would be sweet for that job, but whatever works

  10. #10
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's just the javascript approach scares me...I don't know javascript therefore I am not able to do it...I am better off doing this and figuring this out then trying to worry about javascript lol.

    The thing is...is that it doesn't work correctly...I am having a hard time displaying the MAPS in the list menu. I think it has to do something with the query or something along those lines.

  11. #11
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mandes I was reading your javascript...If I place my dropdown function in that wouldn't it work?

  12. #12
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ugh! Screw it...I will just do something else! LMFAO! Problem fixed.

  13. #13
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Your dropdown function gives you the games dropbox.

    (By the way, why are you escaping all the " in the select code. This is code, only need to escape characters that are going to be saved or passed to other forms)

    You will need to add a name="games" and the onchange handler I gave above to the select statement. Then just create the second dropbox with name="boards"

    As I said above make sure that the form has a name="formname" statement too.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  14. #14
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I do that because there are SPACES in the names of the games..IE: Rainbow Six 3. If you don't make it \"\" then it only displays the FIRST part of the name.

  15. #15
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok so my php script has
    Code:
    <?php
    ###Matches Input Script###
    require_once ('database.php');
    require_once ('func2.inc.php');
    include ('test1.js');
    echo "<form name=formname method=post>";
    dropdownform();
    echo "<select name=\"boards\" id=\"boards\">";
    echo "<input type=submit name=next value=\"Next Step\">";
    echo "</form>";
    ?>
    and my func2.inc.php has
    Code:
    function dropdownform ()
    {
        $query = mysql_query( "SELECT * FROM `games` ORDER BY `gameid` ASC");
    
        echo "<select name=\"games\" onChange=\"getboards(this.options.selectedIndex)\">";
    
        while ( $array = mysql_fetch_array( $query ) )
        {
    
             echo "      <option value={$array['gameid']}>{$array['game_name']}</option>";
        }
    
        echo "</select>";
    }
    I am sorry if I sound really stupid...I have just never dealt with js before

  16. #16
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I don't see that !!

    You've put the slashes in the "name=....." part not the data part, how can that make a difference ??

    Nearly all my dropboxes have spaces in the description text, never had to compensate for it.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  17. #17
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    All looks OK, (except for the slashes)
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  18. #18
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    UK
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As far as i can see the slashes are in the right place, what do you mean, Mandes?

    Mav3n needs to escape the " in the attribute values in the echo command as he is encapsulating the entire string in " as well. If Mav3n was using single quotes around the string then there wouldn't be a need to, but that looks good to me.

  19. #19
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,034
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)

    buggy bugs

    What kind of error messages are you getting? What does the screen output look like? Can you echo $varValues to pinpoint the problem area?

  20. #20
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ShEx
    As far as i can see the slashes are in the right place, what do you mean, Mandes?

    Mav3n needs to escape the " in the attribute values in the echo command as he is encapsulating the entire string in " as well. If Mav3n was using single quotes around the string then there wouldn't be a need to, but that looks good to me.
    My appologies Mav3n, (thanks ShEx).

    I didn't notice the double quotes around the whole statement, probably because I don't program like that myself. I stand corrected.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  21. #21
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not getting any error messages...The GAMES dropdown box is populating just fine....But I think that's because I am still using the query in my func2.inc.php...

    This will take me some time..But I think that it will be well worth it in the end.

    Again...No error messages
    Main Page
    Code:
    <?php
    ###Matches Input Script###
    require_once ('database.php');
    require_once ('func2.inc.php');
    include ('test1.js');
    echo "<form name=formname method=post>";
    dropdownform();
    echo "<br>";
    ListMenuForm ();
    echo "<input type=submit name=next value=\"Next Step\">";
    echo "</form>";
    ?>
    func2.inc.php
    Code:
    <?php
    
    function dropdownform ()
    {
        $query = mysql_query( "SELECT * FROM `games` ORDER BY `gameid` ASC");
    
        echo "<select name=\"games\" onChange=\"getboards(this.options.selectedIndex)\">";
    
        while ( $array = mysql_fetch_array( $query ) )
        {
    
             echo "      <option value={$array['gameid']}>{$array['game_name']}</option>";
        }
    
        echo "</select>";
    }
    
    
    
    
    function ListMenuForm ()
    {
        $query = mysql_query( "SELECT map FROM maps WHERE game_id = '$game'");
        
        echo "<select name=\"boards\" size=\"5\" multiple>";
    
        while ( $array = mysql_fetch_array( $query ) )
        {
          $row1 = mysql_fetch_row($result);
             echo "      <option value={$game}>{$row1[1]}</option>";
        }
    
        echo "</select>";
        
    }
    ?>
    Then the javascript
    Code:
    <script language="JavaScript">
    <!--
    var games=document.formname.games.options.length
    var game=new Array(games)
    for (i=0; i<games; i++)
    game[i]=new Array()
    
    game[0][0]=new Option("optiontext","optionvalue")
    game[0][1]=new Option("optiontext","optionvalue")
    game[0][2]=new Option("optiontext","optionvalue")
    game[1][0]=new Option("optiontext","optionvalue")
    game[1][1]=new Option("optiontext","optionvalue")
    game[1][2]=new Option("optiontext","optionvalue")
    game[1][3]=new Option("optiontext","optionvalue")
    game[2][0]=new Option("optiontext","optionvalue")
    game[2][1]=new Option("optiontext","optionvalue")
    game[2][2]=new Option("optiontext","optionvalue")
    game[2][3]=new Option("optiontext","optionvalue")
    game[3][0]=new Option("optiontext","optionvalue")
    game[3][1]=new Option("optiontext","optionvalue")
    game[3][2]=new Option("optiontext","optionvalue")
    game[3][3]=new Option("optiontext","optionvalue")
    game[4][0]=new Option("optiontext","optionvalue")
    game[4][1]=new Option("optiontext","optionvalue")
    
    
    var temp=document.formname.boards
    function getboards(x){
    for (m=temp.options.length-1;m>0;m--)
    temp.options[m]=null
    for (i=0;i<game[x].length;i++){
    temp.options[i]=new Option(game[x][i].text,game[x][i].value)
    }
    temp.options[0].selected=true
    }
    //-->
    </script>
    Like I said..I know nothing about js..so maybe I am missing a whole lot of stuff! lol

  22. #22
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    OK, look at the source code produced in your browser (right mouse, view source). The javascript should look just like your js code above ?
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  23. #23
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    View-> page source printed...
    Code:
    <script language="JavaScript">
    <!--
    var games=document.formname.games.options.length
    var game=new Array(games)
    for (i=0; i<games; i++)
    game[i]=new Array()
    
    game[0][0]=new Option("optiontext","optionvalue")
    game[0][1]=new Option("optiontext","optionvalue")
    game[0][2]=new Option("optiontext","optionvalue")
    game[1][0]=new Option("optiontext","optionvalue")
    game[1][1]=new Option("optiontext","optionvalue")
    game[1][2]=new Option("optiontext","optionvalue")
    game[1][3]=new Option("optiontext","optionvalue")
    game[2][0]=new Option("optiontext","optionvalue")
    game[2][1]=new Option("optiontext","optionvalue")
    game[2][2]=new Option("optiontext","optionvalue")
    game[2][3]=new Option("optiontext","optionvalue")
    game[3][0]=new Option("optiontext","optionvalue")
    game[3][1]=new Option("optiontext","optionvalue")
    game[3][2]=new Option("optiontext","optionvalue")
    game[3][3]=new Option("optiontext","optionvalue")
    game[4][0]=new Option("optiontext","optionvalue")
    game[4][1]=new Option("optiontext","optionvalue")
    
    
    var temp=document.formname.boards
    function getboards(x){
    for (m=temp.options.length-1;m>0;m--)
    temp.options[m]=null
    for (i=0;i<game[x].length;i++){
    temp.options[i]=new Option(game[x][i].text,game[x][i].value)
    }
    temp.options[0].selected=true
    }
    //-->
    </script>
    <form name="formname" method="post"><select name="games" onChange="getboards(this.options.selectedIndex)">      <option value=1>Rainbow Six 3</option>      <option value=2>Ghost Recon: Summit Strike</option></select><br><select name="boards" size="5" multiple></select><br><input type=submit name=submit value="Submit"></form>

  24. #24
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,034
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)

    bug bugging

    The problem then is wth MAPS?
    PHP Code:
    function ListMenuForm () {
        
    $query mysql_query"SELECT map FROM maps WHERE game_id = '$game'");
        echo 
    "<select name=\"boards\" size=\"5\" multiple>";
        while ( 
    $array mysql_fetch_array$query ) )
        {
          
    $row1 mysql_fetch_row($result);
             echo 
    "      <option value={$game}>{$row1[1]}</option>";
        }
        echo 
    "</select>";

    The query is selecting "map" - one value
    the array "$row1[1]" is a non existant value
    Maybe $row1[0] or $row1['map'] would work?

  25. #25
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function ListMenuForm ()
    {
        $query = mysql_query( "SELECT game_id,map FROM maps WHERE game_id = '$game'");
        $row = mysql_fetch_row($query);
        
        echo "<select name=\"boards\" size=\"5\" multiple>";
    
        while ( $array = mysql_fetch_array( $query ) )
        {
             echo "      <option value={$game}>{$row[map]}</option>";
        }
    
        echo "</select>";
        
    }


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
  •