Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript onClick function

    Hi guys.

    I have made a PHP script that queries a database and uses the reponse to display multiple radio boxes (using a while loop). What I would like to do is have the page URI updated when a radiobox is clicked, without the user having to press on a "Submit" button or something. This is where Javascript comes into play.

    As this is a while loop, there is no specific number of responses or prefixed names. I've found a lot of stuff on Google with functions calling ID's, and then if the ID matches do a predefined response. Of course that wouldn't work in this situation, and to be frank I don't need anything near as complex.

    What I'm looking to do is call a Javascript function to redirect the page to "...?location=<get location from PHP var>&id=<get ID from checkbox ID/name>".

    When the page reloads, I can then use the PHP $_GET["id"] function to display data. It sounds SO simple in theory! Can anyone help?

    What i've done (and failed):

    Code:
    echo "<form>";
    
    while ($explode_count_dec > "0"):
    $while_query = mysql_query("SELECT * FROM printers WHERE ID LIKE '$explodedata[$explode_number]'");
    $while_result = mysql_fetch_assoc($while_query);
    
    echo "<input type='radio' value='".$explodedata[$explode_number]."' name='printer' onClick='showmore(".$explode_number.")' />".$while_result["Model"]." <BR /> ";
    
    ++$explode_number;
    --$explode_count_dec;
    endwhile;
    
    echo "</form>";
    and the JS:

    Code:
    <script type="text/javascript">
    function showmore($text) {
      var room = "<?= $get_location; ?>";
      window.location = "?location=" + room + "&id=" + <?php $text; ?> // Couldn't do just $text here? I wish I knew JS..
    }
    </script>
    Last edited by faaal; 03-23-2012 at 11:28 AM.

  • #2
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Made original post much more clear!
    Last edited by faaal; 03-23-2012 at 11:27 AM.

  • #3
    New Coder
    Join Date
    Feb 2012
    Posts
    22
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Since it is a client side issue with javascript, then it would be nice if you look into the source code of resulting html of that php code, to make it clear.

  • #4
    New Coder
    Join Date
    Mar 2012
    Posts
    13
    Thanks
    0
    Thanked 1 Time in 1 Post
    your javascript isn't going to recognize the $text variable your passing as a php variable so it makes no sense to include the php in your function


    also i wouldn't recommend using the $get_location in the javascript either.

    you can eliminate the javascript function by echoing the entire thing with your php i'm not sure if this will affect your $getlocation function wherever that information is coming from

    this is all you need to do:
    PHP Code:

    echo "<input type='radio' value='".$explodedata[$explode_number]."' name='printer' onClick='window.location=?location=$get_location&id=$explode_number' />".$while_result["Model"]." <BR /> "
    or just change this

    Code:
    <script type="text/javascript">
    function showmore($text) {
      var room = "<?= $get_location; ?>";
      window.location = "?location=" + room + "&id=" + <?php $text; ?> // Couldn't do just $text here? I wish I knew JS..
    }
    </script>
    to this....

    Code:
    <script type="text/javascript">
    function showmore(text) {
      var room = "<?= $get_location; ?>";
      window.location = "?location=" + room + "&id=" + text;   
    }
    </script>


  •  

    Tags for this Thread

    Posting Permissions

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