473,661 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to use a radio button with php

348 Contributor
Hi everyone,

Can somebody please tell me or maybe show me an example of how to use multiple radio buttons in php?

I have a form with 1 textbox and 4 radio buttons. What I want to do is have a user, type in a search string and then check a radio button that will cooraspond to a field in a database. The problem I am having is that the input name must be the same for all of the radio buttons to ensure that only one button is checked. All four buttons must have the same name to be in the same group within the html.

If I make all of the names the same, then how do I differentiate between one POST value and another? I was thinking that maybe from the html button's value but I can't get php to see it.

Can someone please provide a sample or the direction I need to go in?

Thanks,

Frank
Sep 22 '07 #1
12 54414
bergy
89 New Member
How you tell is in the value of the variable (also set in the value attribute of the input tag). Look at the radio buttons below:
[html]
<input type="radio" name="radio" value="radio" />
<input type="radio" name="radio" value="radio2" />
<input type="radio" name="radio" value="radio3" />
[/html]
[php]echo $_POST['radio'];[/php]
If the first radio button was checked the code above would output "radio", if the second was checked the very same code would output "radio2" and if the third was checked the very same code would output "radio3".

So basically, one variable 3 different possible values for that variable depending what which radio button you've checked.
Sep 22 '07 #2
bergy
89 New Member
I assume you could just set the value of the radio buttons to correspond with the column name of your table so...

[html]
<input type="text" name="textBox" />
<input type="radio" name="updateFie ld" value="name" />
<input type="radio" name="updateFie ld" value="address" />
<input type="radio" name="updateFie ld" value="phoneNum ber" />
[/html]

[php]
$column = $_POST['updateField'];
$value = $_POST['textBox'];
mysql_query("up date myTable set ".$column." = `".$value."`;") ;
[/php]
Sep 22 '07 #3
fjm
348 Contributor
How you tell is in the value of the variable (also set in the value attribute of the input tag). Look at the radio buttons below:
[html]
<input type="radio" name="radio" value="radio" />
<input type="radio" name="radio" value="radio2" />
<input type="radio" name="radio" value="radio3" />
[/html]
[php]echo $_POST['radio'];[/php]
If the first radio button was checked the code above would output "radio", if the second was checked the very same code would output "radio2" and if the third was checked the very same code would output "radio3".

So basically, one variable 3 different possible values for that variable depending what which radio button you've checked.
Bergy,

Thank you very much for the explanation. I am going to try and see if I can get it working with what you gave me.

I have exactly what you have as far as all of the name values being the same and a different value assigned to each button but I can't get it to work. I'm sure this problem has much to do with my lack of php experience.


Thanks,

Frank
Sep 22 '07 #4
fjm
348 Contributor
I assume you could just set the value of the radio buttons to correspond with the column name of your table so...

[html]
<input type="text" name="textBox" />
<input type="radio" name="updateFie ld" value="name" />
<input type="radio" name="updateFie ld" value="address" />
<input type="radio" name="updateFie ld" value="phoneNum ber" />
[/html]

[php]
$column = $_POST['updateField'];
$value = $_POST['textBox'];
mysql_query("up date myTable set ".$column." = `".$value."`;") ;
[/php]
Hey Bergy,

That is exactly what I have done. Each value coorasponds to a field in my db.

Let me ask you this Bergy, if you wanted to SELECT a different column that would cooraspond to a different radio button in your example, how would you write your sql. Maybe that is where I am getting hung up.

I have 4 buttons relating to a vehicle search. make, model, vin and license_Plate

Each button has its own value such as
Expand|Select|Wrap|Line Numbers
  1. <input type="radio" name="vehicle" value="plate" />License Plate
the way I am doing it now is writing 4 different sql statements such as

SELECT * FROM vehicle where license_Plate = $_POST[vehicle]
SELECT * FROM vehicle where vin = $_POST[vehicle]

My issue is that I think I need to use an if conditional to find the correct sql code. I can only do that with its value. I'm sorry if I am not clear.

Frank
Sep 22 '07 #5
bergy
89 New Member
I just tested it to make sure and it does in fact work for me. When trying to figure out minor stuff like this, I like to just do a print_r(_POST); which will print everything posted from the form. Copy and paste the following code into a PHP file and you should get someting like [updateField] => name if you check the first radio button.

[php]
<form id="form1" name="form1" method="post" action="">
<input type="text" name="textBox" />
<input type="radio" name="updateFie ld" value="name" />
<input type="radio" name="updateFie ld" value="address" />
<input type="radio" name="updateFie ld" value="phoneNum ber" />
<input type="submit" name="button" id="button" value="Submit" />
</form>
<pre>
<?php
print_r($_POST) ;
?>
</pre>
[/php]
Sep 22 '07 #6
fjm
348 Contributor
I just tested it to make sure and it does in fact work for me. When trying to figure out minor stuff like this, I like to just do a print_r(_POST); which will print everything posted from the form. Copy and paste the following code into a PHP file and you should get someting like [updateField] => name if you check the first radio button.

[php]
<form id="form1" name="form1" method="post" action="">
<input type="text" name="textBox" />
<input type="radio" name="updateFie ld" value="name" />
<input type="radio" name="updateFie ld" value="address" />
<input type="radio" name="updateFie ld" value="phoneNum ber" />
<input type="submit" name="button" id="button" value="Submit" />
</form>
<pre>
<?php
print_r($_POST) ;
?>
</pre>
[/php]
Thanks for sticking with me Bergy. I have already used print_r($POST);

My output is the key which is vehicle and the value which is whatever button was pressed which is correct.

( [search] => [vehicle] => plate
Sep 22 '07 #7
bergy
89 New Member
The value in <input type="radio" value="somethin g"> should be the exact name of your column. So if your column's name is license_Plate you need to make it "license_Pl ate" not "plate" - this way you won't need any if statements.

I think your SQL is incorrect it shouldn't be:
SELECT * FROM vehicle where license_Plate = $_POST[vehicle]

But rather:
SELECT * FROM vehicle where $_POST['vehicle'] = 'searchTerm'

(edit - clarification) This way, if the radio button for license plate is checked, $_POST['vehicle'] = license_Plate and that is the column used in your SQL query. Here is the PHP i would use:

[php]
mysql_query("SE LECT * from vehicle where ".$_POST['vehicle']." like 'searchTerm'");
[/php]
I assume searchTerm would be replaced with the value from the textbox? So, $_POST['textBoxName']?
Sep 22 '07 #8
fjm
348 Contributor
[php] if(isset($_POST['vehicle'])){
if($_POST['plate']){
echo "plate";
}elseif($_POST['vin']){
echo "vin";

}else{
echo "none";
}
}[/php]

Here is the php
Sep 22 '07 #9
fjm
348 Contributor
The value in <input type="radio" value="somethin g"> should be the exact name of your column. So if your column's name is license_Plate you need to make it "license_Pl ate" not "plate" - this way you won't need any if statements.
I'm a bit confused Bergy. Sorry.. Ok, I can change my radio button values to exactly match the column names in the db. thats cool. But I am not following you on the sql. I have never seen sql written the way you are showing me. :)

SELECT * FROM vehicle where $_POST['vehicle'] = 'searchTerm'
the way I am reading this, the post global will output "vehicle" and the rest will follow.

vehicle = search_term??

I was looking to use the post global to match the column name in the db. Of course, if I can use the value, thats great too but I'm sorry, I don't quite understand how to do that with your example.

Ok, here is what I have in my html. I have 1 textbox and 4 radio buttons. I want to use the html to complete the sql statements.

So... If the user enters 2ERQ267 in the textbox for a license plate and then checks the license plate button, *I was thinking* to have an if conditional check to see if the license_Plate button was checked. If so, execute that sql. Else if html button VIN was checked, execute the VIN sql.

edit:

I have 4 different columns that I want to search on and 1 textbox to input the information into.
Sep 22 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
3119
by: John Davis | last post by:
I created a ASP.NET Web Form using VB.NET with a text box, 2 radio buttons. When the user click the first radio button, the text will change to uppercase. If the user clicks the other radio button, the text will change to lowercase. I added the following event, but still won't able to change the text to uppercase. Private Sub RadioButton1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles...
0
1803
by: vinay | last post by:
why are u not using the radiobutton list??? vinay >-----Original Message----- >I created a simple ASP.NET application with a text field, and 2 radio >buttons (uppercase and lowercase conversion). When the user enters a text in >text field, the user can click the radio button for case conversion. I set >the property of radio button to AutoPostBack=True, the
3
2065
by: Amelyan | last post by:
When we want radio button to belong to a group name we say, radio1.GroupName="GroupA". In this case, radio1 will be unselected if another radio button is selected in "GroupA". Is there a way (trick, custom RadioButton, or javascript) to make radio button (radio1) belong to 2 independent radio button groups instead of one? This would be an equivalent of sayting something like radio1.GroupName1 = "GroupA"; radio1.GroupName2 = "GroupB";
8
4680
by: stefano | last post by:
HI, I have aproblem with XHTML radio button. <form name=" form"> <input name="radio" type="radio" onclick="return false" /> <input name="radio" type="radio" onclick="return false" /> <input name="radio" type="radio" onclick="return false" /> </form> I want that when the radio is clicked, it is not checked. in other word I want the click has no effect on the radio.
7
2524
by: nathaniel.k.lee | last post by:
Is it not possible, in IE, to dynamically click a radio button? I'm grabbing some values from a database and using them to populate radio buttons on a page. I have alternate code for Firefox browsers using the setAttribute() function. Everything works as planned in Firefox but in IE, the buttons won't populate and, what's worse, I can't even click on them after everything loads. I see the slight shadow that indicates you're clicking on a...
9
2233
by: IchBin | last post by:
I can not see what the problem is with this script. I am just trying to set a radio button by calling setCheckedValue('abbr_letter', 'V'). Sorry I am new to javascript. <html> <head> <script type="text/javascript"> function setCheckedValue(radioObj, newValue) { if(!radioObj)
1
3221
by: IchBin | last post by:
I am trying to set the state of a radio button. I do not see what I am doing wrong. Sorry, I am new at this.. I need another set of eyes to look at this snip of code. I am trying to set the radio button with this link of code: echo 'SCRIPT language=JavaScript setCheckedValue("'.$_SESSION.'");</SCRIPT>'; //? <snip of code>
10
6074
by: IchBin | last post by:
I am trying to set the state of a radio button. I do not see what I am doing wrong. Sorry, I am new at this.. I need another set of eyes to look at this snip of code. I am trying to set the radio button with this link of code: echo 'SCRIPT language=JavaScript setCheckedValue("'.$_SESSION.'");</SCRIPT>'; //? <snip of code>
0
2294
by: jehugaleahsa | last post by:
Hello: I have radio buttons bound to boolean properties in a business object. private void bindRadioButton(RadioButton button, string propertyName) { Binding binding = button.DataBindings.Add("Checked", exclusionBindingSource, propertyName,
8
4608
by: photoboy | last post by:
I have racked by brain long enough on this, so now I need the help of someone who knows what they are doing. Here is what I am trying to achieve: First, I have two radio buttons (both unchecked) that need to be validated when the submit button is clicked. Instead of the standard alert window popping up (which I have now), I want the radio button background color to change from the table color (E2E2E2) to red (FF0000) for both buttons...
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8343
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8758
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4346
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.