Unfortunately I haven't been able to find exactly what I'm looking for online to resolve this.
At any rate, I have three radio buttons and I need to push the selection of a single button selected from the list into MySQL.
Currently using the below as my radio buttons & submit:
<input class="radiobutton" type="radio" name="1" id="1">1</div>
<input class="radiobutton" type="radio" name="2" id="2">2</div>
<input class="radiobutton" type="radio" name="3" id="3">3</div>
<input type="submit" name="submit" value="Update">`
Can someone help me with the syntax to grab the selected radio button and insert it into MySQL?
An example of the syntax would be
UPDATE tblwhatever.setting SET value='$x' where setting='Y';
Consider the following:
<input class="radiobutton" type="radio" name="radio" id="1" value="1" onclick="document.getElementById('2').checked = false;"/>
<input class="radiobutton" type="radio" name="radio" id="2" value="2" onclick="document.getElementById('1').checked = false;"/>
Note that i gave them the same name attribute. When the form is submitted we will retrieve $_POST['radio'] and the value will determine which radio button was selected. The onclick attribute will uncheck the other radio button if selected.. eg: if radio id="1" is being checked uncheck id="2"
<form method='POST' >
<input class="radiobutton" type="radio"
name="radio" id="1" value='1' checked>
<input class="radiobutton" type="radio"
name="radio" id="2" value='2'>
<input class="radiobutton" type="radio"
name="radio" id="3" value='3'>
<input type="submit" name="submit"
value="Update">`
</form>
in php code should be like this :
<?
$radio = $_POST['radio'];
$query = mysql_query('update "your table" set column1=$radio WHERE some_column=some_value);
?>
Related
I am working on a custom survey that is dynamically populated with database information and needs to be saved as an array. I can make everything work as it should except I would like to use a star rating system for many of the answers and it seems radio buttons won't work like this:
<input type="radio" name="surveyRating[]" value="1" />
<input type="radio" name="surveyRating[]" value="2" />
<input type="radio" name="surveyRating[]" value="3" />
<input type="radio" name="surveyRating[]" value="4" />
<input type="radio" name="surveyRating[]" value="5" />
Because there are multiple instances of this same code. This will be saved into an array so surveyRating will be used over and over again. If I click a radio button in one group, it changes to a radio in another group.
I have read I can possibly do this with checkboxes instead. Is that the best alternative? Or is there another option I should be looking at? I want the final product to be star ratings 1-5. Not sure if I can do that with checkboxes.
EDIT
Ok so using name="surveyRating[1]" is helping as far as the radio buttons not conflicting on the client side. However, it is not saving correctly the way I have my php set up now. Here is how it is saved currently. What do I need to change to make the [1] work appropriately. Currently it is only saving the last iteration.
$new = array();
$ratings = $_POST['surveyRating'];
$count = count( $ratings );
for ( $i = 0; $i < $count; $i++ ) {
if ( $ratings[$i] != '' ) {
$new[$i]['surveyRating'] = stripslashes( strip_tags( $ratings[$i] ) );
}
}
EDIT 2
So to demonstrate how I accomplished this using the answer below, I had to add [0] as the first iteration in the loop. I was using $items = 0; $items++ to dynamically add a number to each loop.
To make it start at 0, I set $items = -1 that way the first iteration is 0 instead of 1. Hope that makes sense.
Yep, it does because you have the name with []. That's for array. Remove it and you will get it without array:
<input type="radio" name="surveyRating" value="1" />
<input type="radio" name="surveyRating" value="2" />
<input type="radio" name="surveyRating" value="3" />
<input type="radio" name="surveyRating" value="4" />
<input type="radio" name="surveyRating" value="5" />
For radio buttons with several groups, you have to do something like this:
<!-- Group 1 -->
<input type="radio" name="surveyRating[1]" value="1" />
<input type="radio" name="surveyRating[1]" value="2" />
<input type="radio" name="surveyRating[1]" value="3" />
<input type="radio" name="surveyRating[1]" value="4" />
<input type="radio" name="surveyRating[1]" value="5" />
<!-- Group 2 -->
<input type="radio" name="surveyRating[2]" value="1" />
<input type="radio" name="surveyRating[2]" value="2" />
<input type="radio" name="surveyRating[2]" value="3" />
<input type="radio" name="surveyRating[2]" value="4" />
<input type="radio" name="surveyRating[2]" value="5" />
I have a radio button with name and value , and I looked for many solutions but still i can't insert my radio button value on my database. My query is perfectly fine working, here is my code
<form action="" method="POST">
<label class="radio-inline"><input type="radio" name="gender" value="male">Male</label>
<label class="radio-inline"><input type="radio" name="gender" value="female">Female</label>
<input type="submit" class="btn btn-default" name="submit">
</form>
what is inserting on my database is the name not the value. so the word gender is the one who is inserting on my database.
I tired to change my gender field type into Varchar and Char but still not working.
if(isset($_POST['submit'])){
$gender = mysqli_real_escape_string($con, $_POST['gender']);
$sql = "INSERT INTO information_table (gender)
VALUES ('gender')";
Did you try like this??
<input type="radio" name="number" value="1" />
1
<input type="radio" name="number" value="2" />
2
<input type="radio" name="number" value="3" />
3
<input type="radio" name="number" value="4" />
4
<input type="radio" name="number" value="5" />
5
<?php
if (isset($_POST['submit'])){
$number = $_POST['number'];
mysql_query("INSERT INTO table_name(number) VALUES ('$number')");
}
?>
I have a form, but I can not receive the data correctly.
I want to receive the id and the option you chose, how can I do this?
I tried to foreach, but could not.
<input type="hidden" id="Id_field[]" name="Id_field[]" value="1"/>
<input type="checkbox" id="option[]" name="option[]" class="validate[required]" value="yes" />Yes
<input type="checkbox" id="option[]" name="option[]" class="validate[required]" value="no" />No
<input type="hidden" id="Id_field[]" name="Id_field[]" value="2"/>
<input type="checkbox" id="option[]" name="option[]" class="validate[required]" value="yes" />Yes
<input type="checkbox" id="option[]" name="option[]" class="validate[required]" value="no" />No
Receiving correctly:
Id_field = 1
option = yes
Id_field = 2
option = no
First of all, id's need to be unique, so this isn't valid (but that doesn't matter for the question). What you can do is use the id in the name like this
<input type="checkbox" name="option[1]" class="validate[required]" value="yes" />Yes
<input type="checkbox" name="option[1]" class="validate[required]" value="no" />No
<input type="checkbox" name="option[2]" class="validate[required]" value="yes" />Yes
<input type="checkbox" name="option[2]" class="validate[required]" value="no" />No
Now you will know by the value if yes or no has been clicked and by the key the id. Also you might consider changing to radiobuttons, then one has to chose either one but can't answer both.
I have a drop down that's numbered 1-30. So if I select '2', my radio buttons will appear like this:
<input type="radio" id="yes-sdf-1" name="field[1][sdfradio]" value="Yes" />
<input type="radio" id="no-sdf-1" name="field[1][sdfradio]" value="No" />
<input type="radio" id="yes-sdf-2" name="field[2][sdfradio]" value="Yes" />
<input type="radio" id="no-sdf-2" name="field[2][sdfradio]" value="No" />
How do I get the individual values of the radio buttons because I want to set conditions that will only affect those with the same name.
Here is an example to give you an idea.
Try this:
$('input:radio').click(function(){
alert($(this).attr('name'));
});
This will return the name of the radio button.
JSFiddle
How to know the value of clicked button
<input type="radio" name="radio">Yes<br>
<input type="radio" name="radio">No
php part
$option1= $_POST['radio'];
When i echo it it always say's "ON".
You need to give a value attribute:
<input type="radio" name="radio" value="yes">Yes<br>
<input type="radio" name="radio" value="no">No
Try this:
<input type="radio" name="radio" value="Yes_value">Yes<br>
<input type="radio" name="radio" value="No_value">No
and in PHP
$option1 = $_POST['radio']; // "Yes_value" or "No_value"
Try This:
Write its value in input
<input type="radio" name="radio" value="Yes" >Yes<br>
<input type="radio" name="radio" value="No">No
Use value attribute
<input type="radio" name="radio" value="Yes" />
<input type="radio" name="radio" value="No" />
assigned different value on the radio button.
<input type="radio" name="radio" value="Yes">Yes<br>
<input type="radio" name="radio" value="No">No
<form>
<input type="radio" name="radio" value="Yes">Yes
<input type="radio" name="radio" value="No">No <br>
<input type="radio" name="radio2" value="Yes">Yes
<input type="radio" name="radio2" value="No">No <br>
<input type="radio" name="radio3">No Value
<input type="submit" value="submit">
</form>
<?php
print_r($_GET);
I suggest you put this in a new file and just run it, it will help you get a deeper understanding of how the radio button work in forms.
Play around, see the difference that u get with different names, and different values.
As you can see, a radio element with no value can only be on or blank (blank wont even be set in PHP)