Jump to content

HTML checkbox values using PHP - Help


Ziul

Recommended Posts

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('#*$!', '#*$!', '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"); //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> </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>

Link to comment
Share on other sites

Number One

 

If you looked at your source code then you would see the problem

 

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

                <td>Meano Burrito</td>

<td>7.99</td>

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

 

                <td>El Nino's Pasta</td>

<td>8.49</td>

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

                <td>Horchata Tea</td>

<td>1.99</td>

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

 

                <td>Le Creme Pie</td>

<td>3.49</td>

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

                <td>Champ's Meal</td>

<td>9.99</td>

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

 

Doesn't that look ify to you?

 

Number Two, Don't Echo variables like that

 

Do it like this:

 

echo $variable;

 

Number Three, We are not here to do your homework for you, we will however help you.

 

Number Four, if you are trying to echo a variable in the middle of a string, then you need to do the following

 

$variable = "Injuncted"
echo "This is an ".$variable." string";

Link to comment
Share on other sites

I was also pulling your leg ^_^

 

 

<!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']; 
$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
$orders = file("$DOCUMENT_ROOT/../public_html/orders.txt"); //MY orders.txt FILE STORED INTO THE $orders VARIABLE
$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>';
echo '<th bgcolor="#CC0000">Price</th></tr><tr><td><strong>Top of Form </strong></td></tr>';
echo '<tr><td>Select the food you ordered from the list below:</td></tr>';
echo '<tr><td><form id="mealForm" name="form1" method="post" action="Assignment4.php"></td></tr>';

for ($i=0; $i<$number_of_orders; $i++) 
	{ 
		$line = explode( "\t", $orders[$i] );
		$line[0] = intval( $line[0] );
		$line[2] = floatval( $line[2] ); 
		echo '<tr><td><input type="checkbox" name="id[]" value="'.$line[0]." />".$line[0]."</td>";
		echo "<td>".$line[1]."</td>";
		echo "<td>".$line[2]."</td></tr>";
	}

echo '<tr><td>How would you rate the food quality?</td></tr>';
echo '<tr><td><select name="foodquality"><option>High</option>';
echo '<option>Average</option>';
echo '<option>Low</option>';
echo '</select></td></tr>';
echo '<tr><td>How would you rate the service quality?</td></tr>';
echo '<tr><td><select name="servicequality"><option>High</option>';
echo '<option>Average</option>';
echo '<option>Low</option>';
echo '</select></td></tr>'
echo '<tr><td>Please enter any comments (optional).</td></tr>';
echo '<tr><td><textarea name="comments"></textarea></td></tr>';
echo '<tr><td>Please enter your email address (optional).</td></tr>';
echo '<tr><td><input name="email" type="text" /></td></tr>';
echo '<tr><td><input name="submit" type="submit" /></td></tr></form>';
echo '</table>';

}
else 
{ 

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> </p></td></tr><tr><td>Here is a copy of your bill:</td></tr>'; 
echo $id[0]."<br>"; 
echo $id[1]."<br>"; 
echo $id[2]."<br>"; 
echo $id[3]."<br>"; 
echo $id[4]."<br>"; 
echo $id[5]."<br>"; 
echo $id[6]."<br>"; 

$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>';
/*
This Eregi might be better 
[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-\.]+(\.com|\.net|\.co\.uk|\.org)


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

 

Give that a whirl, it should work.

Link to comment
Share on other sites

Yes, thanks for coming through, it turns out I am newb and did not understand the simple difference between single and double quotation marks relating to variables and PHP.

 

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...

Link to comment
Share on other sites

Fuxing time elapsing on edit, how lame is that

 

Additionally, you could use the following preg_match on the array of the file

 

if (preg_match('/\\d\\s*[a-zA-ZZ-aA-z]*\\s*[a-zA-ZZ-aA-z]*\\s*\\d*\\u002E\\d*/', $subject, $matches)) {

} else {

}

Loop through $matches and compare the ID from the checkbox with the array key-values

 

The array should contain all the elements needed - you just need to discover what they are.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.