Error trapping...code help

I have this form that gets the table and wid from last form. It works fine as long as the users click on all the radio buttons. But if the radio buttons are not clicked, it suppose to highlight that question is red, but instead it just gets stuck at this point

You are working on: wid   and $table

here is the functions file that generate the radio button and in the snippet I included the actual form code

XXXXXXXXXXXXX
file that generates radio buttons
XXXXXXXXXXXXX

<?php

function checked( $number, $value ) {
     $result = "";
     
     if ( $number == $value )
          $result = " checked='checked' ";
 
     return $result;          
}
 
 
 
function somename($someparametername)
{
 
// Capture current value of button
//
$currValue = -1;
if ( isset( $_REQUEST["r_$someparametername"] ) )
     $currValue = intval( $_REQUEST["r_$someparametername"]);
          else
               $txtValue = " ";
 echo "<table width='200' height='87' border='1'>
  <tr>
    <td width='200'>";
      echo "<input name='r_$someparametername' type='radio' value='1' ".checked(1, $currValue)."/>Yes</td>";
    echo"
  </tr>
  <tr>
    <td>";
      echo"<input name='r_$someparametername' type='radio' value='0' ".checked(0, $currValue)."/>No</td>";
  echo "</tr>
  <tr>
    <td>";
      echo "<input name='r_$someparametername' type='radio' value='2' ".checked(2, $currValue)."/>N/A</td>";
  echo "</tr>
</table>";

}
function display_title($field, $title){
    if(isset($_POST['r_' . $field]) && trim($_POST['r_' . $field]) == ''){
         echo "<span style='color: red' >$title<br />Please Choose an Option</span><br />";
    } else {
        echo $title . '<br />';
    }
}
 
?>
<?php 
	 $wid = $_POST['wsid'];
	 $table = $_POST['table'];
	 echo "You are working on: ".$wid."and Table is".$table; ?><br /><br /><br />
      <?php
echo"<center> </ceter>";
     $err1Start = "";
     $err1End   = "";
     $err2Start = "";
     $err2End   = "";
	 $err3Start = "";
	 $err3End   = "";
	 $err4Start = "";
     $err4End   = "";
	 $err5Start = "";
	 $err5End   = "";
	 $err6Start = "";
     $err6End   = "";
	 
 
     
     // Deal with form submission
     //
     if ( isset( $_POST['localsubmit'] ) ) {//3main if
 
          // Question 1
          //
          if ( !isset( $_REQUEST['r_Q1'] ) ) {
		  		$wid = $_POST['wsid'];
				$table = $_POST['table'];
				
               $err1Start = "<span style='color: red;'>";
               $err1End   = "</span>";
          }
          
 if ( !isset( $_REQUEST['r_Q2'] ) ) {
		  		$wid = $_POST['wsid'];
				$table = $_POST['table'];
				
               $err2Start = "<span style='color: red;'>";
               $err2End   = "</span>";
          }
         if ( !isset( $_REQUEST['r_Q3'] ) ) {
		  		$wid = $_POST['wsid'];
				$table = $_POST['table'];
				
               $err3Start = "<span style='color: red;'>";
               $err3End   = "</span>";
          }
		  if ( !isset( $_REQUEST['r_Q4'] ) ) {
		  		$wid = $_POST['wsid'];
				$table = $_POST['table'];
				
               $err4Start = "<span style='color: red;'>";
               $err4End   = "</span>";
          }
		  if ( !isset( $_REQUEST['r_Q5'] ) ) {
		  		$wid = $_POST['wsid'];
				$table = $_POST['table'];
				
               $err5Start = "<span style='color: red;'>";
               $err5End   = "</span>";
          }
		  if ( !isset( $_REQUEST['r_Q6'] ) ) {
		  		$wid = $_POST['wsid'];
				$table = $_POST['table'];
				
               $err6Start = "<span style='color: red;'>";
               $err6End   = "</span>";
          }
		  
		  
          if ( $err1Start == "" &&
               $err2Start == "" &&
               $err3Start == "" &&
               $err4Start == "" &&
               $err5Start == "" &&
			   $err6Start == "" ){//4insert
 
               // Do the insert to the database here
               //
               //@mysql_query("insert into ....... ");
			   
			   require_once('connection.php');
 
$wid = $_POST['wsid'];
$table = $_POST['table'];
//$survey = $_POST['survey'];
		   
$Q1 = $_REQUEST['r_Q1'];
 
$Q2 = $_REQUEST['r_Q2'];
 
$Q3 = $_REQUEST['r_Q3'];
 
$Q4 = $_REQUEST['r_Q4'];
 
$Q5 = $_REQUEST['r_Q5'];
 
$Q6 = $_REQUEST['r_Q6'];
$completed = $_REQUEST['completed'];
 
$comments = $_REQUEST['comments'];
 
$user = $_SESSION['user'];
 
 
 
 
$insertsurvey = "insert into $table (Q1,Q2,Q3,Q4,Q5,Q6,comments, completed, wid, user, submitted) VALUES('".$Q1."','".$Q2."','".$Q3."','".$Q4."','".$Q5."','".$Q6."','".$comments."','".$completed."','".$wid."', '".$user."', CURRENT_DATE)";
$queryin = mysql_query($insertsurvey) or die(mysql_error());
 
//header("Location: lastindic.php?qq1=$Q1&qq2=$Q2&qq3=$Q3&qq4=$Q4&qq5=$Q5&qq6=$Q6&ccomments=$comments&wwiserid=$wiserid");
//exit;
 
 
 
}//3
 }//4main if
	 
 
     
?>
 
<center>
 
<? 
if (isset( $_POST['fromlastpage'] ) ) {//3
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<? $one = 1;
   
 
 ?>
 <? include('functions.indic.php'); ?>
<input name="completed" type="hidden" value="<? echo $one; ?>" />
<? //echo $wiserid; ?>
<? echo "<input name='wsid' type='hidden' value='".$wid."' />"; ?>
<? echo "<input name='table' type='hidden' value='".$table."' />"; ?>
 
<p align="left"><?php display_title('Q1',$err1Start ."Q1. ?$err1End"); ?></p><br />
<? somename("Q1"); ?><br /><br /><br />
<P align="left"><?php  display_title('Q2',$err2Start ."Q2. ?$err2End"); ?></p><br />
<? somename("Q2"); ?><br /><br /><br />
<P align="left"> <?php display_title('Q3',$err3Start ."Q3. ?$err3End"); ?></P><br />
<? somename("Q3"); ?><br /><br /><br />
<p align="left"><?php display_title('Q4',$err4Start ."Q4. ? $err4End"); ?></p>
<? somename("Q4"); ?><br /><br />
<p align="left"><?php display_title('Q5',$err5Start ."Q5. ?$err5End"); ?></p><br />
<? somename("Q5"); ?><br /><br /><br />
<p align="left"><?php display_title('Q6',$err6Start ."Q6. ?$err6End"); ?></p><br />
<? somename("Q6"); ?><br /><br /><br />
<textarea name="comments" cols="50" rows="10">No Comments</textarea><br />
 
<? 
 
echo "<input name='localsubmit' type='submit' Value='Submit  >>' />";
 
 
?>
<br />
<br />
</form></center>
<?
}//3
}//2
}//1
?>

Open in new window

LVL 9
syedasimmeesaqAsked:
Who is Participating?
 
syedasimmeesaqConnect With a Mentor Author Commented:
Rurne, thanks for your reply. would you be able to give me a sample code. Thanks
0
 
hernst42Commented:
if none radio-button is check $_REQUEST["r_$someparametername"]  is not set. So that form elementname is not in that array, as it is not send by the browser.

So better try for this case:
if(empty($_POST['r_' . $field]) || trim($_POST['r_' . $field]) == ''){
0
 
syedasimmeesaqAuthor Commented:
hmm..could you expalain a bit more. I am not sure where to insert your suggested code. Also where are you getting $field from.
Thanks
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
hernst42Commented:
It's the line outside
 if(isset($_POST['r_' . $field]) && trim($_POST['r_' . $field]) == ''){

So general to check radiobuttons and checkboxes is better done with empty instead of isset, because isset is always false if no such elements is selected.
0
 
syedasimmeesaqAuthor Commented:
I replace this part
if ( !isset( $_REQUEST['r_Q1'] ) ) {

with your suggestion but it still did not work
0
 
syedasimmeesaqAuthor Commented:
any idea?
0
 
syedasimmeesaqAuthor Commented:
not sure why would it not work. Tried almost everything. Anybody has any idea wh wouldn't it work?
Thanks guys
0
 
RurneCommented:
Several things.
1. The code block you've provided doesn't parse.  166 and 167 are unnecessary.

2. Line 6 is unnecessary.

3.  Lines 24, 28, 36, 43, 50, 57, 64: !isset() does not play well with web forms.  Use empty() instead.  Vice versa; isset() should be replaced with !empty() for most of what you're doing.

4. Lines 73-78: same thing.  replace '== ""' with empty() calls.
0
All Courses

From novice to tech pro — start learning today.