Solved

Error trapping...code help

Posted on 2008-06-09
8
193 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
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…

623 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