SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict -Ice-php's Avatar
    Join Date
    May 2002
    Location
    UK
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    <Select> and OnChange

    Hey
    first off, to sitepointforums.com, nice update
    second is my PHP issue
    I have a dynamic select box and when a name is chosen it refreshes the page with the new variables (well it should)
    the select box is called 'challenge'
    and my onChange command has the following variable
    ?bla=bla&bla2=bla2&challenge=<? echo $challenge; ?>
    $challenge should contain the value of the select box
    but it doesnt
    although i may be wrong, it might not be supposed to contain the value and if not what will ?
    do you understand this?
    here is my full <select> code :
    PHP Code:
    <select name="challenge" class="text" id="challenge" style="background-color: <?php echo"$color1?>; border: 1 solid <?php echo"$color3?>" onChange="window.location.href = 'report.php?name=<? echo $name?>&encryptid=<? echo $encryptid?>&ladder=<? echo $ladder2?>&challenge=<? echo $challenge?>'">
                      <?php 
      
          $db 
    mysql_connect($databaseserver$databaseuser$databasepass);
          
    mysql_select_db($databasename,$db);
    $sql="SELECT * FROM challenge WHERE name = '$name'";
    $result=mysql_query($sql,$db);
    $num mysql_num_rows($result);
    $cur 1;
    echo 
    "<ol>";
    while (
    $row mysql_fetch_array($result)) {
    $theirname $row["theirname"];
    if(
    $theirname == ""){
    $theirname "--------";
    }
    ?>
                      <option><? echo "$theirname"?></option>
                      <? $cur++;
    } echo 
    "</ol>";
       
       
    $sql2="SELECT * FROM challenge WHERE theirname = '$name'";
    $result2=mysql_query($sql2,$db);
    $num2 mysql_num_rows($result);
    $cur2 1;
    echo 
    "<ol>";
    while (
    $row2 mysql_fetch_array($result2)) {
    $theirname2 $row2["name"];
    if(
    $theirname2 == ""){
    $theirname2 "--------";
    }
    PHP Code:
    [left]
    ?>
                      <option><? echo "$theirname2"?></option>
                      <?  $cur2++;
    } echo 
    "</ol>";
      if(
    $challenge != ""){
      
    $option "<option selected><? echo $HTTP_GET_VARS[$challenge]; ?></option>";
      }else{
      
    $option "";
      }
      echo 
    "$option";
      
    ?>  
                    </select>[/left]
    Thanks
    -Ice

  2. #2
    My precious!!! astericks's Avatar
    Join Date
    Mar 2002
    Location
    Vancouver, BC
    Posts
    1,971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You missed an escape character. Change

    <p align="left\"> to <p align=\"left\">
    It might work
    Last edited by astericks; Feb 27, 2003 at 13:30.

  3. #3
    SitePoint Addict -Ice-php's Avatar
    Join Date
    May 2002
    Location
    UK
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry
    this script put in that tag
    it isnt in my script :S
    -Ice

  4. #4
    My precious!!! astericks's Avatar
    Join Date
    Mar 2002
    Location
    Vancouver, BC
    Posts
    1,971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    haha, silly me. it would've given you a parse error i guess, if that was in your code.

    i dont tihnk this is the solution, I'm just pointing out that you might have made a mistake somewhere.

    $result2=mysql_query($sql2,$db);
    $num2 = mysql_num_rows($result
    );


    shouldnt it be

    $result2=mysql_query($sql2,$db);
    $num2 = mysql_num_rows($result2
    );




    $result was changed to $result2.

  5. #5
    SitePoint Addict -Ice-php's Avatar
    Join Date
    May 2002
    Location
    UK
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for spotting that but it didnt solve it and it wouldnt becasue i dont actually call $num2 at anytime
    i jsut forgot to remove it :S
    -Ice

  6. #6
    My precious!!! astericks's Avatar
    Join Date
    Mar 2002
    Location
    Vancouver, BC
    Posts
    1,971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by -Ice-php
    Thanks for spotting that but it didnt solve it and it wouldnt becasue i dont actually call $num2 at anytime
    i jsut forgot to remove it :S
    yeah, i knew it wasnt gonna solve it ...just thought you might be passing that variable somewhere else and end up not getting the right results.

    Hopefully, someone will help you out. I'm quite new to php. Good luck.

  7. #7
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since JavaScript is client side and PHP is server side (as probably know) you need to use
    JS to collect the selected value and then but that into the url on a redirect to the same page.

    So I've "made" (mainly copying and pasting from w3schools.com) a JS function that collect the
    selected value an redirect the page with the other values you want in the url.

    Put this JS function inside the <head> tag of your page.
    Code:
    <script type="text/javascript">
    function setChallenge(name, encryptid, ladder2) {
    	option=document.forms[0].challenge.options[document.forms[0].challenge.selectedIndex].text
    	window.location = "report.php?name="+name+"&encryptid="+encryptid+"&ladder="+ladder2+"&challenge="+option;
    }
    </script>
    And this is your script when calling that function. Hopfully it will work.
    PHP Code:
    <?php
    echo "<select name=\"challenge\" class=\"text\" id=\"challenge\" onchange=\"setChallenge('$name', '$encryptid', '$ladder2')\" style=\"background-color: $color1; border: 1 solid $color3\">";
    $db mysql_connect($databaseserver$databaseuser$databasepass) or die(mysql_error);
    mysql_select_db($databasename,$db);
    $sql="SELECT * FROM challenge WHERE name = '$name'";
    $result=mysql_query($sql,$db) or die(mysql_error);
    $num mysql_num_rows($result);
    $cur 1;
    echo 
    '<ol>';
    while (
    $row mysql_fetch_array($result)) {
        
    $theirname $row["theirname"];
        if(
    $theirname == "" ) {
            
    $theirname "--------";
        }
        echo 
    "<option>$theirname</option>";
        
    $cur++;
    }
    echo 
    '</ol>';
     
    $sql2="SELECT * FROM challenge WHERE theirname = '$name'";
    $result2=mysql_query($sql2,$db) or die(mysql_error);
    $num2 mysql_num_rows($result);
    $cur2 1;
    echo 
    '<ol>';
    while (
    $row2 mysql_fetch_array($result2)) {
        
    $theirname2 $row2["name"];
        if(
    $theirname2 == "" ) {
            
    $theirname2 "--------";
        }
        echo 
    "</p><p align=\"left\">";


        echo 
    "<option>$theirname2</option>";
        
    $cur2++;
    }
    echo 
    "</ol>";
    if(
    $challenge != "" ){
        
    $option "<option selected><? echo $HTTP_GET_VARS[$challenge]; </option>";
    } else {
        
    $option "";
    }
    echo 
    "$option";
    echo 
    '</select>';
    -Helge


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
  •