Jump to content

How to display text dynamically on HTML depending on radio button


jwhite68

Recommended Posts

I have a HTML form (combined with PHP script) which presents 3 radio buttons.

 

When either of the first 2 radio buttons are selected, I want to display a piece of text on the form - eg. Amount : $100.

 

If the user selects the third radio button, I dont want to display this text.

 

How can I achieve this?

 

<td width="217"><input type="radio" id="type" name="type" checked="checked" value="single" style="width:15px">
Option 1</td>
<td>	
<input type="radio" id="type" name="type" value="multiples" style="width:15px"> Option 2 </td>
<td width="136"><input type="radio" id="type" name="type" value="multiples" style="width:15px"> Option3</td>'
</tr>
</table>
<div>
Select option
<? if  (<what do I put here?> { ?>
Amount: $100 ?>
<? } ?>

 

You see that I want to display the text Amount: $100, if the condition shown by <what do I put here> is true.

Can anyone advise.  So, on selection of the first or second radio button, I want to display the Amount, but if the third radio button is selected I dont want to display Amount text.

 

Link to comment
Share on other sites

You can setup a condition to check which button was pressed, but PHP is a server side language. Each button would have to be a submit button, and the user would have to press the button, and the client would make another request to the server.  In other words, the page would have to be reloaded every single time.  It's more secure, if that's what you need, but if security is not all that important (as in, you're not gonna lose sleep over whether some dude rightclick > source's it to see the price, the more efficient method would be to do this with javascript.

 

Or you could mix it up with ajax.  It's a middle road in that you use javascript to handle the event (the clicking) and it makes a (smaller) request to the server for the price.  That way, the user cannot right click > source for the price..but it's not as secure as straight php because ajax can be disabled just like javascript, making your page unworkable.

Link to comment
Share on other sites

Well first off, it depends on which route you want to go: strictly PHP, ajax, or javascript?

 

If you're going the PHP route, you need to change your radio inputs to submit inputs, assign unique name/values to them, and make a condition at the top of your script to check which one was pushed, and dynamically display (or not display) your price. 

 

If you're wanting to go the ajax or javascript route...well, I'm not an ajax or javascript dude; you'd probably find better help going to one of those forums.

Link to comment
Share on other sites

I'm sorry, but we aren't really here to write your code for you.  Making a submit button is basic html.  Making a condition to check which button was pushed is basic php.  If you do not know how to do those things, then...no offense, but I have to say that I think you're putting the cart before the horse.  Even if I were to write the code out for you, you wouldn't understand it, and you wouldn't be able to really add to it/alter it etc... 

 

I suggest that you search for some beginner php and html tutorials (the proverbial "hello world" ones), or if you really aren't interested in getting your hands dirty, hire a freelancer. 

 

Again, I'm not trying to be mean here; just looking at it objectively and honestly.  If you wish to go read some tutorials and you're fiddling with your code and you're having trouble with something, we're more than happy to help you fix your bugs.  But we aren't here to do your work for you (for free, lol). 

 

Well, maybe someone out there is more generous than I. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.