HTML checkbox values using PHP - Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ziul
Forum Newbie
Posts: 1
Joined: Tue Jul 10, 2007 4:00 pm

HTML checkbox values using PHP - Help

Post by Ziul »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am on a class assignment that is past due, I've been at this problem for several days straight and can't figure it out.

Here is the webpage for my assignment I created, play with it and you'll know what I mean:

http://alvinsanchez.com/Assignment4.php 

Here is my orders.txt file exactly as it is in the text file (the file that has my menu elements delimited by tabs):

1Nachos Grande8.99
2Meano Burrito7.99
3El Nino's Pasta8.49
4Horchata Tea1.99
5Le Creme Pie3.49
6Champ's Meal9.99
7Veggie's Stew4.99

Assignment: Have the script read the menu from your data file. Ready the file into an array, then use the array to generate the menu on the HTML form. (I've got this part done).

below is my script and in [color=red][b]ALL CAPS, RED AND BOLD[/b][/color] are my comments in problem places:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT']; //must be at the start of code to open/read text files

$id = $_POST['id'];
$foodquality = $_POST['foodquality'];
$servicequality = $_POST['servicequality'];
$comments = $_POST['comments'];
$email = $_POST['email'];

$foodquality = trim($foodquality);
$servicequality = trim($servicequality);
$comments = trim($comments);
$email = trim($email);

$offcolor = array('#*$!', '#*$!', 'bitch', '#*$!', '#*$!', 'dick', 'damn');
$comments = str_replace($offcolor, '%@&*#', $comments);

$toaddress = 'sanchez.alvin@gmail.com';

$subject = 'Feedback from web site';
$mailcontent = 'Food Quality: '.$foodquality."\n"
.'Service Quality: '.$servicequality."\n"
.'Customer Comments: '.$comments."\n"
.'Customer Email: '.$email."\n";
$fromaddress = 'From: Customer - alvinsanchez.com';

mail($toaddress, $subject, $mailcontent, $fromaddress);

?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Assignment 4</title>
<link href="Assignment2.css" rel="stylesheet" type="text/css" />
</head>

<body>

<?php 
//Read in the entire file.
 //Each order becomes an element in the array

 $orders= file("$DOCUMENT_ROOT/../public_html/orders.txt"); [color=red][b]//MY orders.txt FILE STORED INTO THE $orders VARIABLE[/b]
[/color]
 // count the number of orders in the array
 $number_of_orders = count($orders);
 if (empty($id)) { 
 
echo "<table width=\"120\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
 echo '<tr><th bgcolor="#CC0000" width=\"50\">Item ID</th>
   <th bgcolor="#CC0000">Menu Item</th>
<th bgcolor="#CC0000">Price</th></tr>

<tr><td><strong>Top of Form </strong></td></tr>
<tr><td>Select the food you ordered from the list below:</td></tr>
<tr><td><form id="mealForm" name="form1" method="post" action="Assignment4.php"></td></tr>';
 
 for ($i=0; $i<$number_of_orders; $i++) { [color=red][b]//IT ALL STARTS HERE WITH THIS FOR LOOP[/b][/color]
 
 [color=red][b]//I SPLIT UP EACH LINE IN MY orders.txt FILE AND STORE IT IN THE ARRAY $line[/b][/color]
 $line = explode( "\t", $orders[$i] );
 // keep only the number of items ordered

$line[0] = intval( $line[0] );
 $line[2] = floatval( $line[2] ); [color=red][b]//FOR MY MENU PRICES WITH DECIMAL POINTS[/b]

 [b]//ON EVERY LOOP PASS, I WANT TO ASSIGN THE CHECKBOX NAME TO id[] WHICH I HAVE DONE, HOWEVER, THE VALUE FIELD WILL NOT ACCEPT $line[0] AS DEMONSTRATED BELOW WHEN I ECHO THE $id[] values AND PRESS THE SUBMIT BUTTON, ALL IT OUTPUTS IS "$line[0]" ACCORDING TO HOW MANY BOXES I CHECKED OFF, BUT NO $line[0] ELEMENT VALUES, (WHICH SHOULD BE 1,2,3,4,5,6,7) ACCORDING TO WHICH BOXES WERE CHECKED...(more below)[/b]
[/color]
 echo "<tr><td>".'<input type="checkbox" name="id[]" value="$line[0]" />'."$line[0]</td>
    <td>$line[1]</td>
<td>$line[2]</td>
</tr>";

}

echo '<tr><td>How would you rate the food quality?</td></tr>
<tr><td><select name="foodquality"><option>High</option>
<option>Average</option>
<option>Low</option>
</select></td></tr>
<tr><td>How would you rate the service quality?</td></tr>
<tr><td><select name="servicequality"><option>High</option>
<option>Average</option>
<option>Low</option>
</select></td></tr>
<tr><td>Please enter any comments (optional).</td></tr>
<tr><td><textarea name="comments"></textarea></td></tr>
<tr><td>Please enter your email address (optional).</td></tr>
<tr><td><input name="email" type="text" /></td></tr>
<tr><td><input name="submit" type="submit" /></td></tr></form>';
 echo '</table>';
 
} else { [color=red][b]//ASSUMING WE HAVE PRESSED THE SUBMIT BUTTON...[/b][/color]

echo "<table style=\"margin: 0 auto; padding: 5px; border: 1px solid #000;\" width=\"300\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
echo '<tr><td><h1>Demo Cafe</h1></td></tr>
 <tr><td><p>Come back again!</p><p>&nbsp;</p></td>
 </tr><tr><td>Here is a copy of your bill:<br /></td></tr>';
 
echo "$id[0]"; [color=red][b]//I WOULD LIKE TO ECHO OUT EVERY $line[0] ELEMENT CREATED, I SHOULD BE ABLE TO SEE ONLY THE ID NUMBERS CHECKED, BUT I DON'T, ALL I SEE ON MY RESULTS PAGE IS "$line[0]" (the value field) 2,3 OR MORE TIMES DEPENDING ON HOW MANY BOXES I CHECKED. ANY HELP WOULD BE APPRECIATED, THANKS IN ADVANCE.[/b][/color]
echo "$id[1]";
echo "$id[2]";
echo "$id[3]";
echo "$id[4]";
echo "$id[5]";
echo "$id[6]";

$order_number = count($id);
echo '<tr><td>'.$order_number.'</td></tr>';

$mealTax = 0.078375; 
$taxedMeal = 0;
$taxedMeal = $mealtotal * $mealTax;
echo '<tr><td>Tax (7.8375%): $'.number_format($taxedMeal,2).'</td></tr>'; 

 

$gratMealTax = 0.00;
$grat1tip = 0;
$grat2tip = 0;
$gratuities = array('grat1' => $foodquality, 'grat2' => $servicequality);

if ($gratuities['grat1'] == 'High') {
$gratMealTax += 0.10;
$grat1tip = 10;
} else if ($gratuities['grat1'] == 'Average') {
$gratMealTax += 0.05;
$grat1tip = 5;
} else if ($gratuities['grat1'] == 'Low') {
$grat1tip = 0;
}

if ($gratuities['grat2'] == 'High') {
$gratMealTax += 0.10;
$grat2tip = 10;
} else if ($gratuities['grat2'] == 'Average') {
$gratMealTax += 0.05;
$grat2tip = 5;
} else if ($gratuities['grat2'] == 'Low') {
$grat2tip = 0;
}

$tip = $grat1tip + $grat2tip;
$gratMeal = 0;
$gratMeal = $mealtotal * $gratMealTax;
echo '<tr><td>Gratuity ('.intval($tip,1).'%):'.' $'.number_format($gratMeal,2).'</td></tr>';

$grandTotal = $mealtotal + $taxedMeal + $gratMeal;
echo '<tr><td>Total: $'.number_format($grandTotal,2).'</td></tr>';

echo '<tr><td>Your Comments:</td></tr>
 <tr><td>'.nl2br($comments).'</td></tr>
 <tr><td>Your Email:</td></tr>';
 
 if (!eregi('^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$', $email)) {
echo '<tr><td>no email provided</td></tr>';
} else {
echo '<tr><td>'.$email.'</td></tr>';
}

 }
 
echo '</table>';

?>

</body>
</html>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

Whenever you echo html you should include "\" within your element values i.e.

Code: Select all

echo "<input name=\"username\" type=\"text\">";
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

Put the code that doesn' t work not the whole code and be more specific in the questions you put.
First you check if the form was submitted and that the data submitted is valid

Code: Select all

$valid_form=false;
  if(!isset($_POST['submit']))
  {
     /* write the code for showing the form
       choose to separate the data from your file using comma or other character(s)
     */
  }
  if(isset($_POST['submit']))
  {
       if($valid_form==false){
      /*
          validate data inputted by user using is_numeric() is_string(), preq_match for email etc 
      */
     $valid_form=true;
    } 
    else{/*display thank you*/}
   }
/*if you have*/
echo " String "whatever" ";
/*use*/
echo 'String "whatever" ';
Post Reply