NYCPHP Meetup

NYPHP.org

[nycphp-talk] PHP and empty if form value is 0

Tony anthonybol2 at netscape.net
Mon Jan 6 15:29:27 EST 2003


I am using GBSurvey (http://www.geniusbug.com/) to create a survey for 
teachers at a school.  The application only allows for radio buttons in 
the surveys. The school wants some questions to have checkboxes so 
responders can select multiple answers for the question.  I rewrote the 
code to allow the admin to select if the answers to a survey question 
will be radio buttons or checkboxes.  That displays properly.  When I 
test it with multiple check boxes selected in one of the questions only 
the value of the last checkbox selected is entered into the database.  I 
confirmed this by printing the $_POST variables and only the last 
checkbox selected is being sent to the script.  If anyone can offer some 
help, I would be very appreciative.

Here is the code:

Explaination of VARS for this example
------------------
$survey_id = The id of the survey

DB Field Definitions
------------------
surveya_type = Radio or Checkbox
surveyq_id = The question id in the database
surveya_id = The answer id in the database
surveya_name = The answer itself
------------------

This is the code to create the questions:
------------------
function list_surveyq($survey_id) {
      echo "<b><i><a 
href=\\"".$GLOBALS["PHP_SELF"]."?survey_id=".$survey_id."&view_results=true\\">View 
results without taking the survey</a></i></b><P>\
";

      $strSQL = "SELECT * From bug_public_survey WHERE 
survey_id=".$survey_id;
      $query1 = mysql_query($strSQL,$GLOBALS["dbconn"]);
      $survey = mysql_fetch_array($query1);
      echo "<form method=\\"post\\" action=\\"".$GLOBALS["PHP_SELF"]."\\">\
";
      echo "<input type=\\"hidden\\" name=\\"votes\\" value=\\"1\\">\
";
      echo "<input type=\\"hidden\\" name=\\"survey_id\\" 
value=\\"".$survey_id."\\">\
";
//Get all the questions
      $strSQL = "SELECT * From bug_public_surveyq WHERE 
surveyq_surveyid=".$survey["survey_id"]." ORDER By surveyq_id";
      $query2 = mysql_query($strSQL,$GLOBALS["dbconn"]);
      while($surveyq = mysql_fetch_array($query2)) {
//Get all the answers
              echo "<b>".$surveyq["surveyq_name"]."</b><br>\
";
              $strSQL = "SELECT * From bug_public_surveya WHERE 
surveya_surveyqid=".$surveyq["surveyq_id"];
              $query3 = mysql_query($strSQL,$GLOBALS["dbconn"]);
              echo "<ul>\
";
              while($surveya = mysql_fetch_array($query3)) {

                      //
                      //THIS IS WHERE THE ANSWERS ARE PRINTED
                      //print radio button or checkbox and answer title 
for each question
                      //
echo "<input type=\\"".$surveya["surveya_type"]."\\" 
name=\\"answer[".$surveyq["surveyq_id"]."]\\" 
value=\\"".$surveya["surveya_id"]."\\"> ".$surveya["surveya_name"]."<br>\
";
//original code -> echo "<input type=\\"radio\\" 
name=\\"answer[".$surveyq["surveyq_id"]."]\\" 
value=\\"".$surveya["surveya_id"]."\\"> ".$surveya["surveya_name"]."<br>\
";
              }
              echo "<hr></ul>\
";
      }
      echo "<input type=\\"submit\\" value=\\"Submit\\">\
";
      echo "</form>\
";

------------------

When the form is submitted, this is the SQL that is executed.   The 
value of $votes is set to 1.  The page will reload and check to see if 
$votes is set.

-------------------

if (isset($votes)) {
      $strSQL = "SELECT * From bug_public_surveyq WHERE 
surveyq_surveyid=".$survey_id;
      $query = mysql_query($strSQL,$GLOBALS["dbconn"]);
      while($surveyq = mysql_fetch_array($query)) {
              $strSQL = "INSERT INTO bug_public_surveyr 
(surveyr_surveyid, surveyr_surveyaid, surveyr_surveyqid) VALUES 
(".$survey_id.", ".$answer[$surveyq["surveyq_id"]].", 
".$surveyq["surveyq_id"].")";
                            echo $strSQL;
              mysql_query($strSQL,$dbconn);
      }
      mysql_free_result($query);
}
-------------------

The name of each checkbox is the same answer[id#] but the values are 
different.  Do both the name and values have to be different?  When it 
is inserted into the DB it shouldn't matter since the record would not 
be a duplicate.

Thanks again!

Tony Bollino


Freedman, Tom S. wrote:

>I ran into this myself.  With checkboxes (and radio buttons, I think), the
>form key and value are only passed to the following page if they are checked
>off.  That is, if Checkbox is checked (and has 'value=1', you will see
>$Checkbox=1.  If Checkbox is not checked, you will see nothing at all, as if
>Checkbox doesn't exist on the submitted form.  So if you are passing
>checkboxes or radio buttons to a processing page, you really have to check
>for existence, as well as value.  isset(), which Steve pointed out, may
>work... I go about it differently (I set all of my variables explicitly,
>instead of in a loop, so I use 'if ($_GET['checkbox']){$checkData =
>$_GET['checkbox'];}' and such.)
>
>-----Original Message-----
>From: Phil Powell [mailto:soazine at erols.com]
>Sent: Saturday, January 04, 2003 7:28 PM
>To: NYPHP Talk
>Subject: [nycphp-talk] PHP and empty if form value is 0
>
>
>foreach ($HTTP_GET_VARS as $key => $val) {
>   if (!empty($HTTP_GET_VARS["$key"])) ${"$key"} = $HTTP_GET_VARS["$key"];
>  }
>
>  foreach ($HTTP_POST_VARS as $key => $val) {
>   if (!empty($HTTP_POST_VARS["$key"])) ${"$key"} = $HTTP_POST_VARS["$key"];
>  }
>
>Whenever the form variable is equal to 0, the value is not passed into the
>corresponding variable.
>
>For example, if isLogin = 1, then $isLogin = 1
>But if isRegistered = 0 then $isRegistered = {null}
>
>I do not understand why this happens, someone enlighten me!
>
>Phil
>
>
>
>
>
>
>
>--- Unsubscribe at http://nyphp.org/list/ ---
>
>
>
>  
>

-- 
Tony Bollino
AdytumSolutions
Sales and Field Operations
301-788-6886
http://www.adytumsolutions.com
tony at adytumsolutions.com

************************************************
* "Not just a solution ... an AdytumSolution." *
************************************************






More information about the talk mailing list