Solved

Error trapping...code help

Posted on 2008-06-09
8
186 Views
Last Modified: 2013-12-13
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

0
Comment
Question by:syedasimmeesaq
  • 5
  • 2
8 Comments
 
LVL 48

Expert Comment

by:hernst42
ID: 21747140
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
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 21747172
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
 
LVL 48

Expert Comment

by:hernst42
ID: 21747205
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 21747247
I replace this part
if ( !isset( $_REQUEST['r_Q1'] ) ) {

with your suggestion but it still did not work
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 21747311
any idea?
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 21747433
not sure why would it not work. Tried almost everything. Anybody has any idea wh wouldn't it work?
Thanks guys
0
 
LVL 9

Expert Comment

by:Rurne
ID: 21762159
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
 
LVL 9

Accepted Solution

by:
syedasimmeesaq earned 0 total points
ID: 21762482
Rurne, thanks for your reply. would you be able to give me a sample code. Thanks
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question