HTML Radio button not showing up in $_POST - php

I have a forum the user fills out with 2 separate radio button categories they can fill out.
Permissions: private <input type="radio" name="permissions" value="private" /> public <input type="radio" name="permissions" value="public"/><br />
Category: default <input type="radio" name="cat" value"default" /> sport <input type="radio" name="cat" value"sport" />school <input type="radio" name="cat" value"school" />geeky <input type="radio" name="cat" value"geeky" />misc <input type="radio" name="cat" value"misc" /> funny <input type="radio" name="cat" value"funny" /><br />
For some reason the cat for category is not showing up in $_POST['cat']; Can you only have 1 set of radio buttons?

You have some mistypings.
value"default" should be value="default". same with that whole second line of code. There's no equal signs (=) between the attributes and the values.
Quick Tip: Don't put all those <input /> tags on one line. They'll still show up on the same line on your webpage, but it'll be easier to edit in your text editor. Speaking of which, if you had a text editor with syntax highlighting, you probably would've caught this problem. I suggest Notepad++ or Geany.
Category:
default <input type="radio" name="cat" value="default" />
sport <input type="radio" name="cat" value="sport" />
school <input type="radio" name="cat" value="school" />
geeky <input type="radio" name="cat" value="geeky" />
misc <input type="radio" name="cat" value="misc" />
funny <input type="radio" name="cat" value="funny" />

Make sure those fields are within the <form></form> tags.

Related

Saving Radio Button Inputs into Array

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" />

How can i fetching,viewing and updating checkbox values stored in db

i have a html form that has a more then 30 checkboxes. checkboxes are used to select for different service.
I want to store those all value in 1 Database field seperated by , those could be numeric or string. Code for inserting checkbox values is as follows:
<form method="post" action="posted.php">
Option 1: <input type="checkbox" name="type" value="1" /><br />
Option 2: <input type="checkbox" name="type" value="2" /><br />
Option 3: <input type="checkbox" name="type" value="3" /><br />
Option 4: <input type="checkbox" name="type" value="4" /><br />
Option 5: <input type="checkbox" name="type" value="5" /><br />
Option 6: <input type="checkbox" name="type" value="6" /><br />
Option 7: <input type="checkbox" name="type" value="7" /><br />
Option 8: <input type="checkbox" name="type" value="8" /><br />
Option 9: <input type="checkbox" name="type" value="9"/><br />
<input type="submit" value="Submit" />
</form>
After successful submission off course i need to fetch those data in the corespondent check box while update.
Please response me as soon as possible with complete PHP code.
You can use JSON. From this:
Option 1: <input type="checkbox" name="myval[option_name1]" /><br />
Option N: <input type="checkbox" name="myval[option_nameN]" /><br />
Store:
$model->field = json_encode($_POST['myval']);
Or comma-seporated (if you really want):
$model->field = implode(",", $_POST['myval']);

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

Recording values of radio buttons in a db(php)

I have a page of questions that have two options as radio buttons.Each Question has a form tag.Now i want to store the values checked by the user to a database through a single function in php.How to do it?
My html code is as follows:
<li>Question1 </li>
<br /><form>A.
<input type="radio" name="radio" id="1ARadioButton" value="1ARadioButton" />
<label for="1ARadioButton">Answer1</label><br /><br />
B.
<input type="radio" name="radio" id="1BRadioButton" value="1BRadioButton" />
<label for="1BRadioButton"> Answer2</label>
</form><br /><br />
<li>Question2</li>
<br /><form>A.
<input type="radio" name="radio" id="2ARadioButton" value="2ARadioButton" />
<label for="2ARadioButton">Answer1</label>
<br /><br />
B.
<input type="radio" name="radio" id="2BRadioButton" value="2BRadioButton" />
<label for="2BRadioButton">Answer2</label>
</form><br /><br />
<li>Question3</li>
<br /><form>A.
<input type="radio" name="radio" id="3ARadioButton" value="3ARadioButton" />
<label for="3ARadioButton">Answer1</label>
<br /><br />
B.
<input type="radio" name="radio" id="3BRadioButton" value="3BRadioButton" />
<label for="3BRadioButton">Answer2</label>
</form><br /><br />
You've got the same name on all your radio buttons, so you're only going to get the value of the LAST radio button that's checked off. You're going to be recording the same answer for ALL questions (which is usually C, right?).
element IDs are NOT used for form submissions. They're used only for DOM operations. On HTML forms, only the name and value type attributes are relevant for the form submission process.
What you should have is:
Question 1:
<input type="radio" name="question1" value="option_A" />
<input type="radio" name="question1" value="option_B" />
Question 2:
<input type="radio" name="question2" value="option_A" />
<input type="radio" name="question2" value="option_B" />
etc...
As for storing them in the database, that's the same as storing any other form-data in a database. Get the radio's value with $_POST['question1'] or whatever, and do the usual escaping/query building/inserting.

Add hidden input types based on radio button selected

Is it possible to conditionally add hidden input fields into a form?
eg I have a php form that is adding values to a table and if the appleID = 1 or 2 then I want 1 added to the fruits column of my table and if appleID =3 I want 1 added to the sweets column of my table. I thought I might be able to do something like the below but it is adding all hidden values no matter what I select. Or should I approach this a different way?
<input type="radio" value="1" name="appleID" />
<input type="hidden" value="1" name="fruits" />
<input type="hidden" value="0" name="sweets" />
<input type="radio" value="2" name="appleID" />
<input type="hidden" value="1" name="fruits" />
<input type="hidden" value="0" name="sweets" />
<input type="radio" value="3" name="appleID" />
<input type="hidden" value="0" name="fruits" />
<input type="hidden" value="1" name="sweets" />
Thanks I haven't done much with php so I will need to explore that option further. Thanks for the feedback.
I was also looking at something like the below. But PHP sounds likes the better option.
change field value when select radio buttons
You can either use all values in the radio buttons (1,1,0 - 2,1,0 - 3,0,1) and split them after receiving them in your PHP script or add/delete the hidden fields via JavaScript.
Split Example:
HTML:
<input type="radio" value="1,1,0" name="appleID" />
<input type="radio" value="2,1,0" name="appleID" />
<input type="radio" value="3,0,1" name="appleID" />
PHP:
if (!empty($_POST['appleID']))
{
list($appleID, $fruits, $sweets) = explode(",", $_POST['appleID']);
}
It is better to put that logic into the PHP script instead of using Javascript - because you have to do the validation anyway.

Categories