www.webdeveloper.com
Results 1 to 5 of 5

Thread: Javascript onClick function

Hybrid View

  1. #1
    Join Date
    Mar 2012
    Posts
    22

    Exclamation Javascript onClick function

    Hi guys.

    I've been looking around the universe of Google but can't find anything that is applicable with the "type" of radio boxes I'm using.
    I did a little bit of brainstorming and what I came up with, I thought, should work 'in theory'. I just need a bit of a push in the right direction.

    What I'm doing is querying a database. The results are then displayed as radio boxes using a while loop (so there is no specific number of responses or prefixed names). Now, what I need to happen is that when a user clicks on a radio button, further information (specific to that ID) appears.

    I don't mind if this uses a GET function (?id=123) because I've spent a couple of hours moving everything away from POST so that data isn't lost when the page is refreshed.


    What i've done (and it doesn't respond at all!)

    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>

  2. #2
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    So you want this to redirect the whole page to a page that contains information regarding the radio button value that was clicked??

    Firstly, if the JavaScript code is contained within a .js external file, this will not work because the "<?" php opening tag, closing tag, and interim code will not be recognized (only .php files are processed by the PHP server.) The JavaScript code would have to be placed within a .php file.

    Secondly, with a JavaScript redirect, I don't think you can just do a redirect to "?var1=blah&var2=bleh&var3=kudo". You need to put in the whole URL, including the query string.
    |||||
    o . Q
    ___ "You live and you learn; or you don't live long." - Lazarus Long

    ^_^

  3. #3
    Join Date
    Mar 2012
    Posts
    22
    Quote Originally Posted by WolfShade View Post
    So you want this to redirect the whole page to a page that contains information regarding the radio button value that was clicked??

    Firstly, if the JavaScript code is contained within a .js external file, this will not work because the "<?" php opening tag, closing tag, and interim code will not be recognized (only .php files are processed by the PHP server.) The JavaScript code would have to be placed within a .php file.

    Secondly, with a JavaScript redirect, I don't think you can just do a redirect to "?var1=blah&var2=bleh&var3=kudo". You need to put in the whole URL, including the query string.
    Basically I just want javascript to recognise the radiobox click so that the user doesn't then have to press a Submit button as well. I had this working before with an option box however when the page reloaded the box would return to the first option rather than the option selected and I figured it might cause some confusion. Now with Radioboxes the javascript isn't quite the same, and this is what I just assumed would work. Infact, the PHP call (<?php $text ?>) does work, and the redirect does too (exactly as you see above). Just it doesn't seem get that far, I'm not confident that the javascript is even called (it must be a problem with my html).

  4. #4
    Join Date
    Mar 2012
    Posts
    22
    To clarify this bit (var room = "<?= $get_location; ?>" definitely works. I'm not sure about window.location though!

  5. #5
    Join Date
    Mar 2012
    Posts
    22
    Also the JS is inside of a .php file.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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
  •  
HTML5 Development Center



Recent Articles