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
12 54390
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.
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="updateField" value="name" />
<input type="radio" name="updateField" value="address" />
<input type="radio" name="updateField" value="phoneNumber" />
[/html]
[php]
$column = $_POST['updateField'];
$value = $_POST['textBox'];
mysql_query("update myTable set ".$column." = `".$value."`;");
[/php]
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
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="updateField" value="name" />
<input type="radio" name="updateField" value="address" />
<input type="radio" name="updateField" value="phoneNumber" />
[/html]
[php]
$column = $_POST['updateField'];
$value = $_POST['textBox'];
mysql_query("update 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 - <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
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="updateField" value="name" />
<input type="radio" name="updateField" value="address" />
<input type="radio" name="updateField" value="phoneNumber" />
<input type="submit" name="button" id="button" value="Submit" />
</form>
<pre>
<?php
print_r($_POST);
?>
</pre>
[/php]
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="updateField" value="name" />
<input type="radio" name="updateField" value="address" />
<input type="radio" name="updateField" value="phoneNumber" />
<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
The value in <input type="radio" value="something"> should be the exact name of your column. So if your column's name is license_Plate you need to make it "license_Plate" 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("SELECT * from vehicle where ".$_POST['vehicle']." like 'searchTerm'");
[/php]
I assume searchTerm would be replaced with the value from the textbox? So, $_POST['textBoxName']?
[php] if(isset($_POST['vehicle'])){
if($_POST['plate']){
echo "plate";
}elseif($_POST['vin']){
echo "vin";
}else{
echo "none";
}
}[/php]
Here is the php
The value in <input type="radio" value="something"> should be the exact name of your column. So if your column's name is license_Plate you need to make it "license_Plate" 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.
Bergy,
This works.
[php] if(isset($_POST['vehicle'])){
if($_POST['vehicle'] == "plate"){
echo "plate"; [/php]
Is that what you were saying I should do?
Fjm,
You will be using the _POST['vehicle'] as the column name inside of SQL. PHP evaluates the statement before it goes to MySQL...
SELECT * FROM table_name WHERE column_name = value
The above statement is what you're trying to accomplish? Based on what you've said, your table_name is "vehicle", you want to use radio buttons to drive the column_name, and I'm assuming that value is coming from a text box on the form?
If my above assumptions are correct, and you set your radio buttons values to your column names, your SQL statement in PHP would look like this:
[php]"SELECT * FROM vehicle WHERE ".$_POST['vehicle']." = 'something';"[/php]
The 'something' would be replaced by the POST variable for your textbox. And based on the above post of yours, I'm assuming all of your radio buttons are named "vehicle" (which can be a little confusing I guess since that is the same name of the table).
If you set the value of your raido buttons to the names of your 3 columns, all of the following could be the result of your MySQL statement (after PHP parses it, BEFORE it goes to MySQL):
SELECT * FROM vehicle WHERE license_Plate = value
SELECT * FROM vehicle WHERE vin = value
SELECT * FROM vehicle WHERE make = value
SELECT * FROM vehicle WHERE model = value
etc...
I hope this makes it clear - or maybe this isn't what you're trying to do - I'm not sure.
Fjm,
You will be using the _POST['vehicle'] as the column name inside of SQL. PHP evaluates the statement before it goes to MySQL...
SELECT * FROM table_name WHERE column_name = value
The above statement is what you're trying to accomplish? Based on what you've said, your table_name is "vehicle", you want to use radio buttons to drive the column_name, and I'm assuming that value is coming from a text box on the form?
If my above assumptions are correct, and you set your radio buttons values to your column names, your SQL statement in PHP would look like this:
[php]"SELECT * FROM vehicle WHERE ".$_POST['vehicle']." = 'something';"[/php]
The 'something' would be replaced by the POST variable for your textbox. And based on the above post of yours, I'm assuming all of your radio buttons are named "vehicle" (which can be a little confusing I guess since that is the same name of the table).
If you set the value of your raido buttons to the names of your 3 columns, all of the following could be the result of your MySQL statement (after PHP parses it, BEFORE it goes to MySQL):
SELECT * FROM vehicle WHERE license_Plate = value
SELECT * FROM vehicle WHERE vin = value
SELECT * FROM vehicle WHERE make = value
SELECT * FROM vehicle WHERE model = value
etc...
I hope this makes it clear - or maybe this isn't what you're trying to do - I'm not sure.
Bergy,
Thank you again for clairifying your example. You are right on the money with what I needed to acomplish.
With your examples last night, I was able to get it to work.
What was really confusing me was using the word "vehicle" in the html because it was also the same name for the table.
I studied your example last night and played with it and was able to get it working. Once I got it, I looked at it a bit closer and I now can see exactly how it works.
Thanks for helping me Bergy!
Frank
Sign in to post your reply or Sign up for a free account.
Similar topics
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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 =...
|
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)...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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...
| |