2

I have a PHP questionnaire on which at every question I receive two pieces of information.

 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's say from 1 to 3.

How can I connect the answer I have received with a variable user_id that corresponds in the table from database with 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 part of the information it works, but for the second I receive an error for hidden param as not being defined as a variable.

For me it is very important to use user_id because of also knowing to which answer to set the importance 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);
    }
Jonny C
  • 1,943
  • 3
  • 20
  • 36
SocketM
  • 564
  • 1
  • 19
  • 34

2 Answers2

1

By using the $_GET variable as used below you will assign it to $hidden_param

$hidden_param = $_GET['response_id'];
Vasko
  • 257
  • 3
  • 13
1

I think I've got a more complete solution for you This is a single page php script it will do the following;

  • Show the form for submission if the user request value isn't present (With the embedded user id value)
  • Parse and output the ratings from the form
  • Accepts the UserId value as a variable when calling the displayForm function

I've made a mock up question set and given the questions your rating and sub rating? system

I've not done much formatting or anything to pretty it up and bear in mind you should wrap these requests in 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, theres lots of areas for improvement like passing ids instead of strings for the ratings and importance grading

I'm just passing in 1 as the user Id to the displayForm function I am guessing that value will come from a database or session variable at some point

duindain
  • 525
  • 3
  • 12
  • Thank u very much duindain :-) Can you help with another question also http://stackoverflow.com/questions/29875069/the-object-stored-into-a-php-session-gets-the-wrong-value-stored – SocketM May 23 '15 at 17:27