Jump to content

[SOLVED] PHP Email Form with Array


kristenju

Recommended Posts

I will like to get this output in my email:

 

Dynamite

Traffic: Busy

Promotions: Buy 1 get 2nd one half price

Other comments: They got new merchandise

 

Gap

Traffic: Quiet

Promotions: Hoodies for $20

Other comments: Window has changed!

 

Here is the part of my form where it contains those form fields (HTML form page):

 

<div>
<table width="520" cellpadding="5" cellspacing="0">

<tr><td class="left">
<h3 class="subhead">Dynamite</h3><input style="display:none" class="mainForm" name="comp[]" value="Dynamite"></td>
<td></td></tr>

<tbody id="fieldBox_16">
<tr><td class="left">
<label class="formFieldQuestion">Traffic:</label></td>
<td class="right">
<span><input class="mainForm" type="checkbox" name="traffic[]" id="field_16_option_1" value="very busy" ><label class="formFieldOption" for="field_16_option_1">very busy</label><input class="mainForm" type="checkbox" name="traffic[]" id="field_16_option_2" value="busy" ><label class="formFieldOption" for="field_16_option_2">busy</label><input class="mainForm" type="checkbox" name="traffic[]" id="field_16_option_3" value="Quiet" ><label class="formFieldOption" for="field_16_option_3">Quiet</label></span>
</td></tr>
</tbody>

<tbody id="fieldBox_17">
<tr><td class="left">
<label class="formFieldQuestion">Promotions:</label></td>
<td class="right"><textarea id="field_17" class="mainForm" rows="5" cols="30" name="promo[]"></textarea>
</td></tr></tbody>

<tbody id="fieldBox_18">
<tr><td class="left"><label class="formFieldQuestion">Other comments:</label>
</td><td class="right"><textarea id="field_18" class="mainForm" rows="5" cols="30" name="comments[]"></textarea>
</td></tr></tbody>
<tbody>

<tr><td></td></tr>

<tr><td class="left">
<h3 class="subhead">Guess</h3><input style="display:none" class="mainForm" name="comp[]" value="Guess"></td>
<td></td></tr>

<tbody id="fieldBox_16">
<tr><td class="left">
<label class="formFieldQuestion">Traffic:</label></td>
<td class="right">
<span><input class="mainForm" type="checkbox" name="traffic[]" id="field_16_option_1" value="very busy" ><label class="formFieldOption" for="field_16_option_1">very busy</label><input class="mainForm" type="checkbox" name="traffic[]" id="field_16_option_2" value="busy" ><label class="formFieldOption" for="field_16_option_2">busy</label><input class="mainForm" type="checkbox" name="traffic[]" id="field_16_option_3" value="Quiet" ><label class="formFieldOption" for="field_16_option_3">Quiet</label></span>
</td></tr>
</tbody>

<tbody id="fieldBox_17">
<tr><td class="left">
<label class="formFieldQuestion">Promotions:</label></td>
<td class="right"><textarea id="field_17" class="mainForm" rows="5" cols="30" name="promo[]"></textarea>
</td></tr></tbody>

<tbody id="fieldBox_18">
<tr><td class="left"><label class="formFieldQuestion">Other comments:</label>
</td><td class="right"><textarea id="field_18" class="mainForm" rows="5" cols="30" name="comments[]"></textarea>
</td></tr></tbody>
<tbody>

<tr><td></td></tr>

<tr><td class="left">
<h3 class="subhead">Gap</h3><input style="display:none" class="mainForm" name="comp[]" value="Gap"></td>
<td></td></tr>

<tbody id="fieldBox_16">
<tr><td class="left">
<label class="formFieldQuestion">Traffic:</label></td>
<td class="right">
<span><input class="mainForm" type="checkbox" name="traffic[]" id="field_16_option_1" value="very busy" ><label class="formFieldOption" for="field_16_option_1">very busy</label><input class="mainForm" type="checkbox" name="traffic[]" id="field_16_option_2" value="busy" ><label class="formFieldOption" for="field_16_option_2">busy</label><input class="mainForm" type="checkbox" name="traffic[]" id="field_16_option_3" value="Quiet" ><label class="formFieldOption" for="field_16_option_3">Quiet</label></span>
</td></tr>
</tbody>

<tbody id="fieldBox_17">
<tr><td class="left">
<label class="formFieldQuestion">Promotions:</label></td>
<td class="right"><textarea id="field_17" class="mainForm" rows="5" cols="30" name="promo[]"></textarea>
</td></tr></tbody>

<tbody id="fieldBox_18">
<tr><td class="left"><label class="formFieldQuestion">Other comments:</label>
</td><td class="right"><textarea id="field_18" class="mainForm" rows="5" cols="30" name="comments[]"></textarea>
</td></tr></tbody>
<tbody>

<tr><td></td></tr>
</table></div>

 

This is where I am having problems with in my process.php:

 

$tra = $_POST['traffic'][0].",". $_POST['traffic'][1].",". $_POST['traffic'][2];


for($i = 0; $i < count($_POST['comp']); $i++) {
  echo $_POST['comp'][$i] ;
}

$comp .= $_POST['comp'][$i] ;

foreach($_POST["comments"]  as  $value)  {
$comments .= "$value\n";
}

foreach($tra  as  $value)  {
$traffic .= "$value\n";
}

foreach($_POST["promo"]  as  $value)  {
$promo .= "$value\n";
}

$traf .= "Achalandage de la boutique: $traffic \n";
$prom .= "Promotions en cours: $promo \n";
$com .= "Remarques pertinentes: $comments \n";


$analyse .="$comp $traf $prom $com";

// email


$to = "tam.kristen@gmail.com" ;
$subject = "Rapport Journalier: $inputdate";
$message = "
<html><body>
<b>Competitor Analysis Feedbacks</b>
$analyse

</body></html>" ;

$headers = "From: $store <$storemail>" . "\r\n" .
    "Reply-To: $store <$storemail>" . "\r\n" .
    'X-Mailer: PHP/' . phpversion();


mail($to, $subject, $message, $header);

 

Then I get an error in my thankyou page saying:

DynamiteGapGuess

Warning: Invalid argument supplied for foreach() in /public_html/report/process.php on line 41

 

And this is what it is actual displaying in my email when I get the results:

 

Competitor Analysis Feedbacks

 

Traffic:

Promotions: Buy 1 get 2nd one half price

Hoodies for $20

 

Why is the store name not displaying?

Why is the traffic values not displaying?

Why is the 'Comments:' and its values not displaying?

Why are they not grouped with the store?

 

Another problem is: I am not seeing the From: email correctly. I have put it as no-reply@xxx.com

 

Please help!

 

Link to comment
Share on other sites

well firstly this snippet

foreach($_POST["comments"]  as  $value)  {
$comments .= "$value\n";
}

 

is incorrect. you cannot concatenate things onto a string with no value. if you want to concatenate stuff to that string, you need to give it a value first (even an empty value);

$comments = "";
foreach($_POST["comments"]  as  $value)  {
$comments .= "$value\n";
}

 

the same idea goes for your $traffic and $promo variables

Link to comment
Share on other sites

Another problem is: I am not seeing the From: email correctly. I have put it as no-reply@xxx.com

 

Erm.. No you haven't...

 

$headers = "From: $store <$storemail>" . "\r\n" .

 

What do you mean by grouping? Can you give an example of what your getting, and what you want.

 

 

Link to comment
Share on other sites

Another problem is: I am not seeing the From: email correctly. I have put it as no-reply@xxx.com

 

Erm.. No you haven't...

 

$headers = "From: $store <$storemail>" . "\r\n" .

 

What do you mean by grouping? Can you give an example of what your getting, and what you want.

 

I want the output to come out like this: (group or sort by count ([])...?)

 

Dynamite

Traffic: busy

Promotions: promo1

Comments: comment1

 

Gap

Traffic: quiet

Promotions: promo2

Comments: comments2

 

Right now it is grouped by fieldname ...

 

Dynamite Gap

Traffic: busy quiet

Promotions: promo1 promo2

Comments: comments 1 comments 2

 

 

Link to comment
Share on other sites

Another problem is: I am not seeing the From: email correctly. I have put it as no-reply@xxx.com

 

Erm.. No you haven't...

 

$headers = "From: $store <$storemail>" . "\r\n" .

 

 

There is a hidden field in my html file and the value is

$store = Store Name

$storemail = no-reply@xxx.com

 

I had to do it this way because I would like to use one process page for multiple forms..

Link to comment
Share on other sites

There is a hidden field in my html file and the value is

$store = Store Name

$storemail = no-reply@xxx.com

 

I had to do it this way because I would like to use one process page for multiple forms..

Ahh ok fair enough.

Am I right in saying that the stores will be dynamically generated and there won't just be the few you have in your current HTML? The only reason I ask is the way your are storing some information with the HTML structure is going to make it difficult to achieve the aim you have. What is the objective of the Traffic checkboxes, is it actually possible for a single store to have multiple values?

Link to comment
Share on other sites

Hi! Well basically each of my stores have designated competitors to visit.. so basically they need to do a small report on their traffic, promos and other comments..

 

So each store has their own form to fill out and submit.

(They are different in terms of competitors..)

 

That's why I need all these arrays....

sample of my form: http://www.foxyjeans.com/report/form102.html

 

 

 

 

Link to comment
Share on other sites

Ok... Assuming that all the traffic selections are type="radio" and have a name="traffic_X", where the x represents the index of that shop (from 0 upwards) as you walk down the page....

 

$feedback = '';
foreach($_POST["comments"] $as $k=>$v) {
$feedback .= $_POST['comp'][$k] . "\r\n";
$feedback .= "Traffic: " . $_POST['traffic_' . $k] . "\r\n";
$feedback .= "Promo: " . $_POST["promo"][$k] . "\r\n";
$feedback .= "Comments: " . $_POST["com"][$k] . "\r\n\r\n";
}

 

Note: It does somewhat rely on all fields being required, which you would have to check generating this information. If thats a problem let me know.

Link to comment
Share on other sites

well I assuming my translation is correct, only 1 of these can be true of any one location? (Très Occupé, OccupéPeu, Occupé, Tranquille), with that being the case you give them all the same name for each shop, then whichever one is selected will be the value for that location.

Link to comment
Share on other sites

But that's the problem..

They have to do a list..

Maybe I didn't explain it clearly enough..

 

The form you see form102.html is only the form of my store #1. I still have store #2 and #3 and #4... etc..

 

That list (Forever21, Dynamite, Garage, ...) they are all the competitors, and they need to go in each of the stores and give me feedbacks.

 

So for example how's the traffic at Forever21? Busy

How's Traffic at Dynamite? Quiet

How's Traffic at Garage? Soso

 

So basically if I put radio buttons instead of checkboxes, they will be selecting busy at forever21, then when they select quiet at Dynamite, the answer of forever21 will automatically be taken off.. because only one value can be working..

 

So I don't have any problems using checkboxes

 

And even the reply address and from address can be fixed easily.

 

My biggest problem is the answers..

 

I don't get them the way it's suppose to be..

like.. which should be:

 

$comp[0]

Traffic: $traffic[0]

Promos: $promo[0]

Comments: $comments [0]

 

$comp[1]

Traffic: $traffic[1]

Promos: $promo[1]

Comments: $comments [1]

 

$comp[2]

Traffic: $traffic[2]

Promos: $promo[2]

Comments: $comments[2]

 

But now it is displaying

 

$comp[0]

$comp[1] 

$comp[2]

Traffic: $traffic[0] $traffic[1] $traffic[2]

Promos: $promo[0] $promo[1] $promo[2]

Comments: $comments[0] $comments[1] $comments[2]

 

....

 

But I haven't tried what you told me to do yet.. let me try it with the checkboxes..

 

Let me know if you have other suggestions..

Link to comment
Share on other sites

I dont think you understand my code/suggestion.

 

At one shop, so on one HTML page, lets say the local competitors are Forever21 Dynamite and Garage... your HTML would look like so....

 

<!-- start of form -->
<!-- start details for Forever21 -->
<input type="radio" name="traffic_0" value="quite" />
<input type="radio" name="traffic_0" value="not-so-quite" />
<input type="radio" name="traffic_0" value="busy" />
<input type="radio" name="traffic_0" value="omg-I-couldnt-move" />
<!-- rest of info for this shop -->
<!-- start details of Dynamite -->
<input type="radio" name="traffic_1" value="quite" />
<input type="radio" name="traffic_1" value="not-so-quite" />
<input type="radio" name="traffic_1" value="busy" />
<input type="radio" name="traffic_1" value="omg-I-couldnt-move" />
<!-- rest of info for this shop -->
<!-- start details of Garage -->
<input type="radio" name="traffic_2" value="quite" />
<input type="radio" name="traffic_2" value="not-so-quite" />
<input type="radio" name="traffic_2" value="busy" />
<input type="radio" name="traffic_2" value="omg-I-couldnt-move" />
<!-- rest of info for this shop -->

 

This way, the number that follows traffic_ will match the index of each of the other arrays that a shops details are stored in, because garages comments will be in $_POST['comm'][2]. As I said it only works if each field is required, but I imagine that you would want all forms filling out.

 

So checkboxes would both completely screw up the system, and would be semantically wrong, because you only ever want one value per shop.

Link to comment
Share on other sites

The other fields need to be left as is, so that they form an array. It's on that basis that my code works. Your code should looks something like....

 

  <tbody>
    <tr>
      <td class="left"><span class="subhead">Analyse de la compétition</span></td>
    </tr>
  </tbody>
  <tbody>
    <tr>
      <td class="left"><h3 class="subhead">Forever21</h3>
        <input style="display: none;" class="mainForm" name="comp[]" value="Forever21"></td>
      <td></td>
    </tr>
  </tbody>
  <tbody id="fieldBox_16">
    <tr>
      <td class="left"><label class="formFieldQuestion">Achalandage de la boutique:</label></td>
      <td class="right"><span>
        <input class="mainForm" name="traffic_0" id="field_16_option_1" value="Très Occupé" type="radio">
        <label class="formFieldOption" for="field_16_option_1">Très Occupé</label>
        <input class="mainForm" name="traffic_0" id="field_16_option_2" value="Occupé" type="radio">
        <label class="formFieldOption" for="field_16_option_2">Occupé</label>
        <input class="mainForm" name="traffic_0" id="field_16_option_3" value="Peu Occupé" type="radio">
        <label class="formFieldOption" for="field_16_option_3">Peu Occupé</label>
        <input class="mainForm" name="traffic_0" id="field_16_option_4" value="Tranquille" type="radio">
        <label class="formFieldOption" for="field_16_option_4">Tranquille</label>
        </span></td>
    </tr>
  </tbody>
  <tbody id="fieldBox_17">
    <tr>
      <td class="left"><label class="formFieldQuestion">Promotions en cours:</label></td>
      <td class="right"><textarea id="field_17" class="mainForm" rows="5" cols="30" name="promo[]"></textarea></td>
    </tr>
  </tbody>
  <tbody id="fieldBox_18">
    <tr>
      <td class="left"><label class="formFieldQuestion">Remarques pertinentes:</label></td>
      <td class="right"><textarea id="field_18" class="mainForm" rows="5" cols="30" name="comments[]"></textarea></td>
    </tr>
  </tbody>
  <tbody>
    <tr>
      <td><br></td>
    </tr>
  </tbody>
  <tbody>
    <tr>
      <td class="left"><h3 class="subhead">Dynamite</h3>
        <input style="display: none;" class="mainForm" name="comp[]" value="Dynamite"></td>
      <td></td>
    </tr>
  </tbody>
  <tbody id="fieldBox_16">
    <tr>
      <td class="left"><label class="formFieldQuestion">Achalandage de la boutique:</label></td>
      <td class="right"><span>
        <input class="mainForm" name="traffic_1" id="field_16_option_1" value="Très Occupé" type="radio">
        <label class="formFieldOption" for="field_16_option_1">Très Occupé</label>
        <input class="mainForm" name="traffic_1" id="field_16_option_2" value="Occupé" type="radio">
        <label class="formFieldOption" for="field_16_option_2">Occupé</label>
        <input class="mainForm" name="traffic_1" id="field_16_option_3" value="Peu Occupé" type="radio">
        <label class="formFieldOption" for="field_16_option_3">Peu Occupé</label>
        <input class="mainForm" name="traffic_1" id="field_16_option_4" value="Tranquille" type="radio">
        <label class="formFieldOption" for="field_16_option_4">Tranquille</label>
        </span></td>
    </tr>
  </tbody>

 

As taken from your actual site and updated...

Link to comment
Share on other sites

I am getting an error though.. actually...

 

in the php part,

$feedback = '';
foreach($_POST['comments'] as $k=>$v) {
$feedback .= $_POST['comp'][$k] . "\r\n";
$feedback .= "Traffic: " . $_POST['traffic_' . $k] . "\r\n";
$feedback .= "Promo: " . $_POST["promo"][$k] . "\r\n";
$feedback .= "Comments: " . $_POST["com"][$k] . "\r\n\r\n";
}

 

But shouldn't it be:

$feedback .= "Comments: " . $_POST["comments"][$k] . "\r\n\r\n";

 

Cause in my html Comments fieldname is 'comments'...

 

Then where does foreach($_POST['comments'] as $k=>$v) this comments comes from? what should I be replacing it by?

Link to comment
Share on other sites

What does that have to do with anything? We are writting a script to process a HTML form and e-mail the details. There is really no point in worrying about sending an e-mail untill we know the processsing is working correctly. If when we echo out $feedback it contains all the correct information. We slap it into an e-mail, job done.

 

If you would rather keep e-mailing the result, checking your e-mail, just to check if a values working rather than print it straight to screen then you go right ahead, I'm just trying to make life easier for you.

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.