/    Sign up×
Community /Pin to ProfileBookmark

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

[/CODE]

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>
[/CODE]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@WolfShadeMar 26.2012 — 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.
Copy linkTweet thisAlerts:
@faaalauthorMar 26.2012 — 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.[/QUOTE]


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).
Copy linkTweet thisAlerts:
@faaalauthorMar 26.2012 — To clarify this bit (var room = "<?= $get_location; ?>"? definitely works. I'm not sure about window.location though!
Copy linkTweet thisAlerts:
@faaalauthorMar 26.2012 — Also the JS is inside of a .php file.
×

Success!

Help @faaal spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...