Collecting multiple data using a checkbox in a PHP form

I have a PHP questionnaire on which I receive two pieces of information for each question.

 1. An answer from a scale from 1 to 5

 2. The importance assigned for this question from the part of the user on a scale let say from 1 to 3.

      

How can I connect the received response with a user_id variable that matches in the table from the database with the user id.

 echo '<label ' . ($response['response'] == NULL ? 'class="error"' : '') . ' for="' . $response['response_id'] . '">' . $response['topic_name'] . ':</label>';
    echo '<input type="radio" id="' . $response['response_id'] . '" name="' . $response['response_id'] . '" value="6" ' . ($response['response'] == 6 ? 'checked="checked"' : '') . ' />All';
    echo '<input type="radio" id="' . $response['response_id'] . '" name="' . $response['response_id'] . '" value="1" ' . ($response['response'] == 1 ? 'checked="checked"' : '') . ' />Love ';
    echo '<input type="radio" id="' . $response['response_id'] . '" name="' . $response['response_id'] . '" value="2" ' . ($response['response'] == 2 ? 'checked="checked"' : '') . ' />Hate';
    echo '<input type="radio" id="' . $response['response_id'] . '" name="' . $response['response_id'] . '" value="3" ' . ($response['response'] == 3 ? 'checked="checked"' : '') . ' />Maybe';
    echo '<input type="radio" id="' . $response['response_id'] . '" name="' . $response['response_id'] . '" value="4" ' . ($response['response'] == 4 ? 'checked="checked"' : '') . ' />Super';
    echo '<input type="radio" id="' . $response['response_id'] . '" name="' . $response['response_id'] . '" value="5" ' . ($response['response'] == 5 ? 'checked="checked"' : '') . ' />All<br />';
    $hidden_param=$response['response_id'];
    echo '<input type="radio" id="' . $hidden_param . '" name="' . $hidden_param . '" value="6" ' . ($response['importance'] == 6 ? 'checked="checked"' : '') . ' />Mandatory';
    echo '<input type="radio" id="' . $hidden_param . '" name="' . $hidden_param . '" value="1" ' . ($response['importance'] == 1 ? 'checked="checked"' : '') . ' />Not important ';
    echo '<input type="radio" id="' . $hidden_param . '" name="' . $hidden_param . '" value="2" ' . ($response['importance'] == 2 ? 'checked="checked"' : '') . ' />It dosen't matter for me <br />';

      

For the first piece of information it works, but for the second I get an error for the hidden parameter as not being defined as a variable.

It is very important for me to use user_id due to the fact that you know which answer to set the value for, as in the following implementation:

foreach ($_POST as $response_id => $response) {
      $query = "UPDATE mismatch_response SET response = '$response' WHERE response_id = '$response_id'";
      mysqli_query($dbc, $query);
    }

     foreach ($_POST as $response_id => $importance) {
      $query = "UPDATE mismatch_response SET importance = '$importance' WHERE response_id = '$response_id'";
      mysqli_query($dbc, $query);
    }

      

+3


source to share


2 answers


I think I have a more complete solution for you This is a one page php script, it will do the following:

  • Show form to submit if no custom request value (with inline user id value)
  • Parse and deduce grades from the form
  • Accepts UserId value as a variable when calling displayForm function

I asked mock questions and asked questions on your rating and subheading? System

I haven't done a lot of formatting or anything else to come to terms with this, and keep in mind that you must wrap these requests with filters or htmlentities or both to prevent attacks.



<?php
    $questions = array(
        1 => "How old is old",
        2 => "Why is green not red",
        3 => "How do you climb while swimming",
        4 => "How long is long",
    );

    $user = isset($_REQUEST["user"]) ? $_REQUEST["user"] : null;
    if($user != null)
    {
        echo "We have data for user ".$user."<br/>";

        foreach ($questions as $key => $value) 
        {
            $rating = $_REQUEST["q_".$key."_rating"];
            $importance = $_REQUEST["i_".$key."_importance"];

            echo "<br/>Question '".$key."' rating '".$rating."' Importance '".$importance."'";
        }
        echo "<br/>";
    }
    else
    {
        displayForm(1);
    }

    function displayForm($userId)
    {
        global $questions;

        $ratings = array("All", "Love", "Hate", "Maybe", "Super");

        $importance = array("Mandatory", "Not important", "It doesn&#39;t matter");

        echo "<form>";
        echo "<input type='hidden' name='user' value='$userId'>";
        foreach ($questions as $key => $value) 
        {
            echo "Question ".$key.") ".$value."?";

            $ratingHtml = "";
            foreach ($ratings as $rat)
            {
                $ratingHtml .= "$rat <input type='radio' name='q_".$key."_rating' id='$key' value='$rat' />";
            }

            $importHtml = "";
            foreach ($importance as $import)
            {
                $importHtml .= "$import <input type='radio' name='i_".$key."_importance' id='$import' value='$import' />";
            }

            echo "[".$ratingHtml."] [".$importHtml."]<br/>";
        }
        echo "<input type='submit' /></form>";
    }
?>

      

This should help, there are many areas for improvement like passing IDs instead of strings for ratings and importance ratings

I am just passing 1 as the user id to the displayForm function. I am assuming the value will be fetched from database or session variable at some point

+2


source


Using the $ _GET variable as shown below you will assign it to $ hidden_param



$hidden_param = $_GET['response_id'];

      

+1


source







All Articles