Forum Moderators: coopster

Message Too Old, No Replies

PHP to process a Javascript populated calculation

ID not picked up by PHP

         

nicob2

10:03 am on Jul 25, 2012 (gmt 0)

10+ Year Member



Hi,
I need to mail the results of an orderform to the client.
The quantity is in a number field the description in text only and the total is text.

My php prints the number and description but ignores the total:
<snip>

[edited by: eelixduppy at 4:24 pm (utc) on Jul 26, 2012]
[edit reason] snipped url [/edit]

omoutop

11:01 am on Jul 25, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



welcome to webmasterworld.
its hard to assist you without some code.
Can you please provide us with some php code?

nicob2

11:42 am on Jul 25, 2012 (gmt 0)

10+ Year Member



Hi,
and thanks - at the moment its still minimal - I am adapting it from a normal contact form I used b4 but here is the code:


*please pardon messy coding I am a designer - not clever -lol

<?php
/* Set e-mail recipient */
$myemail = "email@example.com";

/* Check all form inputs using check_input function */
$PROD_SP_485 = check_input($_POST['PROD_SP_485']);
$platter1R = check_input($_POST['platter1R']);


$order_total = check_input($_POST['order_total']);




/* If e-mail is not valid show error message */
/*if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}*/

/* If URL is not valid set $website to empty */
/*if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
{
$website = '';
}*/

/* Let's prepare the message for the e-mail */
$message = "


<strong>Order for outside catering as follows:</strong><br>


<strong></strong>$PROD_SP_485 Cheese and Biscuit Platter $platter1R<br>
$order_total



------------------------------------------
";







/*Prepare the autoresponder subject*/
$respond_subject = "Outside Catering from Casa Toscana";
/*Prepare autoresponder message*/
$respond_message = "Dear Valued client<br>
<br>
We would like to thank you for your patronage and confirm that your order wasreceived and will receive immediate attention.<br>
<br>
<br>
<br>
Our aim is to exceed your expectations and to make your experience with us memorable. If we do not succeed in doing exactly that, please let us know. If we did succeed, please forward us your story as we would love to post your positive experience on our website.<br>
<br>
Have a wonderful day<br>
<br>
From your Casa Toscana Team";

//$headers = 'MIME-Version: 1.0' . "\r\n";
//$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
//$headers .= 'To: \'Mary\' <mary@example.com>' . "\r\n";
//$headers .= 'From: "Support" <support@example.com>' . "\r\n";


$headers = "From: Outside Catering<Catering@example.com>\r\n";
$headers .= "Reply-To: support@example.com\r\n";
$headers .= "Return-Path: support@example.com\r\n";
$headers .= "X-Mailer: Custom\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";



/* Send the message using mail() function */
mail($email, $respond_subject, $respond_message, $headers );
mail($myemail, $subject, $message, $headers);

/* Redirect visitor to the thank you page */
header('Location: thanks.htm');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>
<html>
<body>

<b>Please correct the following error:</b><br />
<?php echo $myError; ?>

</body>
</html>
<?php
exit();
}
?>

[edited by: eelixduppy at 4:26 pm (utc) on Jul 26, 2012]
[edit reason] exemplified [/edit]

omoutop

11:49 am on Jul 25, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



a quick glance gives no indication for any obvious errors
$order_total = check_input($_POST['order_total']);

Have you checked the spelling of your input field?

at the top of you page do something like this:
echo '<pre>';
print_r($_POST);
exit();

that will show you what is posted to your php page. Maybe thats where the error is

nicob2

11:58 am on Jul 25, 2012 (gmt 0)

10+ Year Member



Thanks,
I'll do that now (Its like a walk in the dark for me..gotta start somewhere

nicob2

3:56 pm on Jul 25, 2012 (gmt 0)

10+ Year Member



What is wrong with this file:
The error message: A <code>sendAdminEmail</code> function is included in the head of this file. You will need to place your email address there (<code>$admin_email</code>) and uncomment the <code>mail</code> function to send the emails

keeps coming up - I feel daft..........

<?php
require('includes/ex2.inc.php');

function sendAdminEmail($total, $order) {
$admin_email = 'email@example.com';
$subject = 'Order Information';
$name = stripslashes( strip_tags( $_POST['first_name'] ) ) . ' ' .
stripslashes( strip_tags( $_POST['last_name'] ) );

$email = stripslashes( strip_tags( $_POST['email'] ) );
// check for valid email address
$regex = '/^[\w\+\'\.-]+@[\w\'\.-]+\.[a-zA-Z]{2,}$/';
if ( !preg_match($regex, $_POST['email']) ) {
// don't send email
echo '<p>Your email appears to be invalid. Please hit your browser back button to return to the previous page to enter a vaild email address.</p>';
return;
}
$phone = stripslashes( strip_tags( $_POST['phone'] ) );
$msg_body = "Order placed for:
$order

Total: $$total

Name: $name
Email: $email
Phone: $phone";

echo nl2br($msg_body); // for testing

@mail($admin_email, $subject, $msg_body );
}


function handleOrderInfo() {
global $PRODUCTS;;
$str = ''; $total = 0; $order = '';
while ( list($key, $val) = each($_POST) ) {
// Check for valid quantity entries > 0
if ( ( strpos($key, '_qty') !== false ) && is_int( (int)$val) && $val > 0 ) {
$pt = strrpos($key, '_qty'); // get product abbr
$name_pt = substr( $key, 0, $pt);

foreach($PRODUCTS as $product) {
list($prod_abbr, $prod_name, $prod_price) = $product;
if ($prod_abbr == $name_pt) {
$sub_tot = $val * $prod_price;
// build string to display order info
$str .= "<p>$val $prod_name at $" . number_format($prod_price, 2) .
' each for $' . number_format($sub_tot, 2) . '</p>';
$total += $sub_tot;
$order .= "$val $prod_abbr, ";
}
}
}
}
$total = number_format($total, 2);
$order = rtrim($order, ', ');
if ( $str === '' ) {
$str = '<p>You didn\'t order anything.</p>';
} else {
$str = "<h2>Your Order:</h2>$str<p>Total: $$total</p>";
sendAdminEmail($total, $order);
}

return $str;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Your Order</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="includes/ex.css" type="text/css" />
</head>
<body>

<?php
echo handleOrderInfo();
?>


<p>&nbsp;</p>
<p>A <code>sendAdminEmail</code> function is included in the head of this file. You will need to place your email address there (<code>$admin_email</code>) and uncomment the <code>mail</code> function to send the emails.</p>

<p>Back to <a href=".">index</a></p>

</body>
</html>

[edited by: eelixduppy at 4:27 pm (utc) on Jul 26, 2012]
[edit reason] exemplified [/edit]

nicob2

4:02 pm on Jul 25, 2012 (gmt 0)

10+ Year Member



The error message keeps coming up in this code, I am missing something:?
<?php
require('includes/ex2.inc.php');

function sendAdminEmail($total, $order) {
$admin_email = 'email@example.com';
$subject = 'Order Information';
$name = stripslashes( strip_tags( $_POST['first_name'] ) ) . ' ' .
stripslashes( strip_tags( $_POST['last_name'] ) );

$email = stripslashes( strip_tags( $_POST['email'] ) );
// check for valid email address
$regex = '/^[\w\+\'\.-]+@[\w\'\.-]+\.[a-zA-Z]{2,}$/';
if ( !preg_match($regex, $_POST['email']) ) {
// don't send email
echo '<p>Your email appears to be invalid. Please hit your browser back button to return to the previous page to enter a vaild email address.</p>';
return;
}
$phone = stripslashes( strip_tags( $_POST['phone'] ) );
$msg_body = "Order placed for:
$order

Total: $$total

Name: $name
Email: $email
Phone: $phone";

echo nl2br($msg_body); // for testing

@mail($admin_email, $subject, $msg_body );
}


function handleOrderInfo() {
global $PRODUCTS;;
$str = ''; $total = 0; $order = '';
while ( list($key, $val) = each($_POST) ) {
// Check for valid quantity entries > 0
if ( ( strpos($key, '_qty') !== false ) && is_int( (int)$val) && $val > 0 ) {
$pt = strrpos($key, '_qty'); // get product abbr
$name_pt = substr( $key, 0, $pt);

foreach($PRODUCTS as $product) {
list($prod_abbr, $prod_name, $prod_price) = $product;
if ($prod_abbr == $name_pt) {
$sub_tot = $val * $prod_price;
// build string to display order info
$str .= "<p>$val $prod_name at $" . number_format($prod_price, 2) .
' each for $' . number_format($sub_tot, 2) . '</p>';
$total += $sub_tot;
$order .= "$val $prod_abbr, ";
}
}
}
}
$total = number_format($total, 2);
$order = rtrim($order, ', ');
if ( $str === '' ) {
$str = '<p>You didn\'t order anything.</p>';
} else {
$str = "<h2>Your Order:</h2>$str<p>Total: $$total</p>";
sendAdminEmail($total, $order);
}

return $str;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Your Order</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="includes/ex.css" type="text/css" />
</head>
<body>

<?php
echo handleOrderInfo();
?>


<p>&nbsp;</p>
<p>A <code>sendAdminEmail</code> function is included in the head of this file. You will need to place your email address there (<code>$admin_email</code>) and uncomment the <code>mail</code> function to send the emails.</p>
<p>Back to <a href=".">index</a></p>

</body>
</html>

[edited by: eelixduppy at 4:28 pm (utc) on Jul 26, 2012]
[edit reason] exemplified [/edit]

omoutop

11:33 am on Jul 26, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



if i understand the error corectly, you must define the admin email somewhere in your included file.

nicob2

3:35 pm on Jul 26, 2012 (gmt 0)

10+ Year Member



I have been able to work out the email error - I find it difficult to understand the relationship between the file and the includes.
I am trying to edit the four items in the example by increasing the number of array items and then displaying the new added data in the form but it keeps to the original 4 items - I really am too new at this