1

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.

1 Answer 1

1

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" />
Sign up to request clarification or add additional context in comments.

8 Comments

Ok that makes more sense. I will be saving these like so: $ratings = $_POST['surveyRating']; so it NEEDS to be an array.
see my edit above, I am now unable to save my results.
@RiotAct What do you get when you do print_r($_POST)?
Array ( [surveyTalk] => Array ( [0] => Test [1] => Test Title ) [surveyRating] => Array ( [1] => 2 [2] => 1 ) the survey ratings should be connected with the survey talks in the way I have it set up.
Yes it is skipping ahead to [1] instead of [0] and the array objects don't match up because of it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.