0

I have an order form (table) that I need to be sent to an email address. I understand the basics of how to send an email in PHP, but not the specifics of this situation (taking the information of the cookie array + form elements and put them in body of the PHP mailer)

I am using this link's answer to store the order form as an array in a cookie

Then I use this javascript to create the table in HTML, inside the #catalog div. The important part is the for loop that makes the table. Using list.items(); one can list the "cookie array" as an array

function loopArrayMail() { 
if ($.cookie('productWishlist') == null) {
    html = ""; $('#catalog').html(html);
} else {
    var cookie = $.cookie("productWishlist"); var items = cookie ? cookie.split(/,/) : new Array();
    var html = "<table><tr><th>Product</th><th># to order</th></tr>";
    for(var i=0;i<items.length;i++){ html += "<tr><td width='450'>"+items[i]+"</td><td><input type='text' name='numberOfItems' /></td></tr>"; }
    html += "</table>"; $('#catalog').html(html);
}}

There is a text input next to each item in the order form for the user to input the # of items they want. How would I send the contents of the table and each form input as an email in PHP?

My guess is that I'd have to take the cookie array, and with each iteration have an array of all the text inputs and use foreach so that they are together in the email (unsure of how to do this exactly). It is also important that there can be any number of text inputs as the number of items on the order form will increase/decrease.

Here is an example of using foreach to get multiple input boxes, but how might I combine the array from my cookie with the text inputs?

Community
  • 1
  • 1
Jweb
  • 47
  • 5

2 Answers2

0

Better you send only plain HTML in the email without any javascript because you won't have any javascript in the email program nor you have any cookies.

No idea who told you you could use that, but Email programs are not Browsers.

However if you send a link via email, any user can then open it in a full-featured internet browser so this might be what you're looking for.


enter image description here

Netscape Communicator 4 had support for javascript in emails, no idea about the cookies.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • I'm only using javascript to store the cookie as an array, and to read the cookie back. My question is asking, how do I put the "cookie array" along with the input boxes containing the # of items in the body of a PHP email. The question asks how to "Mail Dynamic Table with Forms via PHP", not via javascript – Jweb Aug 14 '12 at 07:41
  • Well as the email program does not support javascript nor cookies, you technically can not. You would need to have cookies supported (which is very problematic) and then you would need to have javascript support (which is very problematic as well) to actually set the cookie. Two important conditions are very problematic. This is just not possible I'd say. I'm sorry that I need to write you that, I have no idea how you could circumvent that if you do not want to limit emailaing to certain browsers you have tested it works (e.g. the outdated Netscape Communicator 4 Series, oh we had fun). – hakre Aug 14 '12 at 07:52
  • I'm not asking to put cookies or javascript in an email. I want to send my cookie that is an array, along with the text inputs as an email in PHP – Jweb Aug 14 '12 at 07:57
  • I do not understand you. You then want to send the cookie that is an array with the html in that email? Convert the cookie data into text and send it as an email. You do not know how to access cookies in PHP? Is that your issue? If so enjoy the manual: http://php.net/manual/en/reserved.variables.cookies.php – hakre Aug 14 '12 at 08:08
  • I guess my question should be "How do I gather all text inputs, and combine them with another array to make a coherent table in a PHP email" – Jweb Aug 14 '12 at 08:14
0

First you must give the form elements a name followed by [] indicating that it is an array, when you create the form elements in the loopArrayMail() function.

for(var i=0;i<items.length;i++){
  html += "<tr><td width='450'>"+items[i]+"</td><td><input type='text' name='ordernum[]' size='8' /></td></tr>";
}

Since the cookie is a string you must first explode the string based on the delimiter which is a comma. Place the two arrays into variables. Then use a for loop to go through all of the array.

function explode_trim($str, $delimiter = ',') {
  if ( is_string($delimiter) ) {
    $str = trim(preg_replace('|\\s*(?:' . preg_quote($delimiter) . ')\\s*|', $delimiter, $str));
    return explode($delimiter, $str);
  } 
return $str; }

$orderCookie = explode_trim($_COOKIE['productWishlist']);
$orderNum = $_POST['ordernum'];

for ( $i = 0; $i < count($orderCookie); $i++) {
  echo $orderCookie[$i] . ' = ' . $orderNum[$i] . '<br />';
}
Jweb
  • 47
  • 5
  • If someone else can't help you with your question, bash your face against the keyboard until it works... I guess! – Jweb Aug 16 '12 at 19:13