Click to See Complete Forum and Search --> : HTML checkbox values using PHP - Help


ziul
07-10-2007, 04:26 PM
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 ALL CAPS, BOLD AND RED are my comments in problem places:

<!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('#*$!', '#*$!', '*****', '#*$!', '#*$!', '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"); //MY orders.txt FILE STORED INTO THE $orders VARIABLE

// 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++) { //IT ALL STARTS HERE WITH THIS FOR LOOP

//I SPLIT UP EACH LINE IN MY orders.txt FILE AND STORE IT IN THE ARRAY $line
$line = explode( "\t", $orders[$i] );
// keep only the number of items ordered

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

//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)

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 { //ASSUMING WE HAVE PRESSED THE SUBMIT BUTTON...

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]"; //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.
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>

temp.user123
07-10-2007, 07:36 PM
This line:

echo "<tr><td>".'<input type="checkbox" name="id[]" value="$line[0]" />'."$line[0]</td>
should be like this:

echo "<tr><td>".'<input type="checkbox" name="id[]" value="'.$line[0].'" />'."$line[0]</td>

ziul
07-10-2007, 10:14 PM
Yes, I overlooked something as simple as single and double quotation marks thanks.

I've got one more question:

I am able to access and output the ID numbers for my order items, however, I cannot associate the ID numbers with the appropriate items and prices.

I'm close to getting what I want with fulfilling this assignment. I have a question:

<?php
//Read in the entire file.
//Each order becomes an element in the array
$orders= file("$DOCUMENT_ROOT/../public_html/orders.txt");
// 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++) { //starts loop

//split up each line on every loop pass
$line = explode( "\t", $orders[$i] );
// keep only the number of items ordered

$line[0] = intval( $line[0] );
$line[2] = floatval( $line[2] );
// output each order

echo "<tr><td>".'<input type="checkbox" name="id[]" value="'.$line[0], $line[1], $line[2].'" />'.$line[0]."</td>"; //in the value line, I've passed three values, and it's been giving me the output I want, however, it's not as flexible as simply using the id number then referencing the menu items (strings and prices) to so that I can manipulate those elements separately in output. I think passing multiple values to this checkbox is inefficient, is there a more effective and efficient solution?
echo "<td>".$line[1]."</td>"; //on every pass we output the second element of every line in our text file which are strings
echo "<td>".$line[2]."</td></tr>"; //on every pass we output the third element of every line in our text file which is a float type as specified above with our code "$line[2] = floatval($line[2]);"


}

//THE SUBMIT BUTTON IS PRESSED

} else {



$count_items = count($id); //seems fine
for ($i = 0; $i < $count_items; $i++) { //seems fine
$items = explode("\t", $id[$i]); //DOES NOT seem fine
//with this loop, I want to output the id numbers of the corresponding checkboxes selected, that part works, where I'm having trouble is accessing the corresponding menu item and price elements that belong to the appropriate ID numbers, let me know if I've explained this right and you understand my goal.
echo '<tr><td>'.$id[$i].'</td></tr>';
$mealtotal += $id[$i]; //the $mealtotal variable would contain the price amount for the appropriate item (ID number) selected, which above is contained in $line[2] before I press the submit button, however, I can't access it, the rest of my calculations below are waiting for the appropriate price amounts
}




//Other calculations for $mealtotal etc...

temp.user123
07-10-2007, 11:04 PM
When the id number is returned in your POST array (id[]), use that to match against your CSV file items. Don't bother trying to return any other information from the form. This is also how things would work if you were using a database (isntead of a CSV file).

ziul
07-10-2007, 11:23 PM
I'm a newb. What you say I don't fully understand what you mean, I think that I am using the id[] to access my elements using the ID numbers specified in the text. Will you elaborate a little more? an example would be helpful as I am visual. thanks