Radio button $_POST - php

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)

Related

PHP form arrays not received post

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.

Submitting HTML Form Values as Array (To PHP)

I've got a lot of radio boxes on a single page within my website.
I know it is possible to submit all of the options as an array that can be directly manipulated by PHP.
<input type="radio" name="SOMETHING_HERE" value="1" />
There are several radio groups and I'd like all of them to submit to one array.
All I'd like to know is the syntax which has to be used in the name="".
Use like this
<input type="radio" name="optionname[]" value="1" />
<input type="radio" name="optionname[]" value="2" />
.
.
You can have your form in either of the following ways.
<input type="radio" name="name[]" value="1" />
<input type="radio" name="name[]" value="2" />
Or
<input type="radio" name="question['question1']" value="1" />
<input type="radio" name="question['question2']" value="2" />
There by you setting the array definition yourself. Hope this answers your query.
HTML looks like this:
<input type="radio" name="name[]" value="1" />
<input type="radio" name="name[]" value="2" />
PHP accesses it like this:
<?php
$_POST['name'] = array('1','2');
Some things to note:
Order in array as compared to as it exists in HTML is not guaranteed

Multiple radio button array for php form

I'm new to this. I'm studying web development and have to create a php response form for a questionnaire which then inputs into a database. I'm having trouble with the radio buttons. I can't create the right code that makes an array and displays the answers in the response form/page.
This is my code:
<form name="modulequestionnaire" method="post" action="tania.responseform.php" />
<p><i>Rate each question from 6 to 1, six being strongly
agree and one being strongly disagree.</i></p>
1. I think the module guide/student handbook provided enough information about the
module content, organisation and assessment.<br/>
6<input type="radio" name="answer[1]" value="6"> 5<input type="radio" name="answer[1]" value="5">
4<input type="radio" name="answer[1]" value="4"> 3<input type="radio" name="answer[1]" value="3">
2<input type="radio" name="answer[1]" value="2"> 1<input type="radio" name="answer[1]" value="1">
</p>
2.The module was well organised.<br/>
6<input type="radio" name="answer[2]" value="6"> 5<input type="radio" name="answer[2]" value="5">
4<input type="radio" name="answer[2]" value="4"> 3<input type="radio" name="answer[2]" value="3">
2<input type="radio" name="answer[2]" value="2"> 1<input type="radio" name="answer[2]" value="1">
</p>
3.The Learning Resource Centre provided adequate materials for the module.<br/>
6<input type="radio" name="answer[3]" value="6"> 5<input type="radio" name="answer[3]" value="5">
4<input type="radio" name="answer[3]" value="4"> 3<input type="radio" name="answer[3]" value="3">
2<input type="radio" name="answer[3]" value="2"> 1<input type="radio" name="answer[3]" value="1">
</p>
I know the answer can relate to the isset function but I don't know how to code it.
Could someone possibly teach or help me out here?
When you're unsure of how to handle the HTML markup that you've set up, you should var_dump($_POST) the values that are sent to the PHP handler page so you know what the format will look like, that way you can proceed from there.
When I created your HTML and tested it with a var_dump and some random selections, the output was
array(2) { ["answer"]=> array(3) { [1]=> string(1) "5" [2]=> string(1) "3" [3]=> string(1) "4" } ["submit"]=> string(6) "Submit" }
Notice that there is an array within the $_POST['answer'] variable. So you should foreach over each element in that array to handle each respective value:
foreach ($_POST['answer'] as $answer) {
// do stuff with the answer
}
If you need to work with the answer number that you defined in the POST array, you can foreach with a key:
foreach ($_POST['answer'] as $answerNum => $answer) {
// do stuff with $answerNum and $answer
}
You can, of course, access your answer by its number directly:
if (!empty($_POST['answer'][1])) { // To ensure that the value is being sent
// do stuff with $_POST['answer'][1]
}
I don't imagine that this is quite what you are looking to do. If instead you give the three questions different names:
<form name="modulequestionnaire" method="post" action="tania.responseform.php" />
<p><i>Rate each question from 6 to 1, six being strongly
agree and one being strongly disagree.</i></p>
1. I think the module guide/student handbook provided enough information about the
module content, organisation and assessment.<br/>
6<input type="radio" name="answer1" value="6"> 5<input type="radio" name="answer1" value="5">
4<input type="radio" name="answer1" value="4"> 3<input type="radio" name="answer1" value="3">
2<input type="radio" name="answer1" value="2"> 1<input type="radio" name="answer1" value="1">
</p>
2.The module was well organised.<br/>
6<input type="radio" name="answer2" value="6"> 5<input type="radio" name="answer2" value="5">
4<input type="radio" name="answer2" value="4"> 3<input type="radio" name="answer2" value="3">
2<input type="radio" name="answer2" value="2"> 1<input type="radio" name="answer2" value="1">
</p>
3.The Learning Resource Centre provided adequate materials for the module.<br/>
6<input type="radio" name="answer3" value="6"> 5<input type="radio" name="answer3" value="5">
4<input type="radio" name="answer3" value="4"> 3<input type="radio" name="answer3" value="3">
2<input type="radio" name="answer3" value="2"> 1<input type="radio" name="answer3" value="1">
</p>
then in php all you will need to do is find the values using the $_POST variable as such
<?php
echo $_POST['answer1'];
echo $_POST['answer2'];
echo $_POST['answer3'];
?>

Inserting the selection of radio buttons into MySQL

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);
?>

how to group radio buttons based on same class instead of same name

Is there a way to group the set of radio buttons that share the same class.
I want to be able to be able to check the radio button from each set.
Currently it allows me to check only one radio button (as I know because of same name)
Is there a JQuery way either ?
Example:
SET A
<input type="radio" name="item[]" class="a" value="1"><br>
<input type="radio" name="item[]" class="a" value="2"><br>
<input type="radio" name="item[]" class="a" value="3"><br>
<input type="radio" name="item[]" class="a" value="4"><br>
SET B
<input type="radio" name="item[]" class="b" value="5"><br>
<input type="radio" name="item[]" class="b" value="6"><br>
<input type="radio" name="item[]" class="b" value="7"><br>
<input type="radio" name="item[]" class="b" value="8"><br>
Ok so you are trying to have multiple array entries, but using 1 array.
Since html interprets them as having the same name, you will have to add the numbers into the array key yourself.
So something like this should work.
<input type="radio" name="item[0]" class="a" value="1" />
<input type="radio" name="item[0]" class="a" value="2" />
<input type="radio" name="item[0]" class="a" value="3" />
<input type="radio" name="item[0]" class="a" value="4" />
<input type="radio" name="item[0]" class="a" value="5" />
<input type="radio" name="item[1]" class="b" value="6" />
<input type="radio" name="item[1]" class="b" value="7" />
<input type="radio" name="item[1]" class="b" value="8" />
<input type="radio" name="item[1]" class="b" value="9" />
<input type="radio" name="item[1]" class="b" value="10" />
$('#container').on('click', 'input[type="radio"]', function(){
this.name = this.className;
});​
This should solves it.
Live DEMO
Can you not name the sets differently?
SET A
< input type="radio" name="item1[]" class="a" value="1">
< input type="radio" name="item1[]" class="a" value="2">
< input type="radio" name="item1[]" class="a" value="3">
< input type="radio" name="item1[]" class="a" value="4">
SET B
< input type="radio" name="item2[]" class="b" value="5">
< input type="radio" name="item2[]" class="b" value="6">
< input type="radio" name="item2[]" class="b" value="7">
< input type="radio" name="item2[]" class="b" value="8">

Categories