...

View Full Version : Javascript onClick function



faaal
03-23-2012, 11:08 AM
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):




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:



<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>

faaal
03-23-2012, 12:22 PM
Made original post much more clear!

bores
03-23-2012, 05:01 PM
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.

SamHuff
03-23-2012, 06:13 PM
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:



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


<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....


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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum