Page 1 of 1

Combine fields in PHP form

Posted: Mon Mar 31, 2008 6:16 am
by awa
Hi

I have a very long HTML form, processed using PHP. I have been asked to combine some fields in the email output of the form.

I'm using $Field to pick up everything, inc name, email, etc. The ordering section of the form uses two fields for each item, a text input quantity (e.g. q_itemone) and an item spec taken from a dropdown (e.g. s_itemone).

The email currently gets sent reading (for example)...

q_itemone: 88
s_itemone: large


How might I (Can I?) get my email to combine these two fields, so that the output reads (for example)

itemone: 88 x large

Code snippet below. Any advice much appreciated - thanks.

Code: Select all

  
<?php
  $to='Name <name@email.uk>';
  $messageSubject="Quote";
  $confirmationSubject="Quote";
  $confirmationBody="Thank you ". $Name . " for visiting Name. \n\nYou have asked us to quote for the following items:\n\n". $MsgBody . "\n";
  
 $displayForm=true;
  if ($_POST){
    $SubmissionName=stripslashes($_POST['Name']);
    $Email=stripslashes($_POST['Email']);
  
foreach 
    ($_POST as $Field=>$value) 
      if (is_array($value)){
      $MsgBody .= "$Field:".implode(", " , $value)."\n\n";
      }
else {
    if(strlen($value[$z])>0)
     $MsgBody .= "$Field: $value\n\n";
     }
    
    $valid=eregi('^([0-9a-z]+[-._+&])*[0-9a-z]+@([-0-9a-z]+[.])+[a-z]{2,6}$',$Email);
    if ($Email && $valid){
      if (mail($to,$messageSubject,$MsgBody,'From: '.$Email."\r\n")
          && mail($Email,$confirmationSubject,$confirmationBody.$MsgBody,'From: '.$to."\r\n")){
        $displayForm=false;
?>

Re: Combine fields in PHP form

Posted: Mon Mar 31, 2008 7:46 am
by aceconcepts
To combine two values you would write it like this:

Code: Select all

 
$combined=$field1 . " x " . $field2;
 

Re: Combine fields in PHP form

Posted: Mon Mar 31, 2008 7:51 am
by kryles
or

$combined= "$field1 x $field2";

I think this should work the same. Could be wrong heh.

Re: Combine fields in PHP form

Posted: Mon Mar 31, 2008 8:02 am
by awa
Oh, it's as easy as using 'combined'. Great, thank you both.

Though, I forgot a crucial point in the original post...

Where I have specified, e.g., q_itemone and s_itemone, there are many items similar throughout the form, like, up to about itemthirty.

Would I need to make a 'combined' statement for each of the sets, or would a substitution work, like saying 'if it matches q_ [+whatever item it is]'

I did try using * and something with stristr earlier which didn't work, though I probably got the syntax for that wrong too.

Re: Combine fields in PHP form

Posted: Mon Mar 31, 2008 8:13 am
by kryles
something along the lines of

Code: Select all

 
 
foreach($_POST as $item => $value)
{
    /* grab them and concatenate to string */
}
 
 
This would have to have code in it to test if the item is one you want added to the string. Not sure in your situation if this is feasible, but it was an idea

by the way if you wanted all $_POST information concatenated just do

Code: Select all

 
$Msg = "";
foreach($_POST as $item=>$value)
{
$Msg .= $value . "x";
}
//i forget how but use a string function to drop the last X
 
sorry for the half code and untested idea


Edit: Oh I just re-read it and this way wont work for you. Although I'll keep it just for future reference or maybe it'll help someone later

change the values in your form to have numerical values instead of "one" "two" etc

Code: Select all

 
 
<form>
<input type="text" name="q_item1" value="dsfdfds">
<input type="text" name="s_item1" value="fsdfds">
<input type="text" name="q_item2" value="dsfdfds">
<input type="text" name="s_item2" value="fsdfds">
......
 
Then use $_POST['q_item'.$i] and $_POST['s_item'.$i] in a loop to grab them all.

Re: Combine fields in PHP form

Posted: Mon Mar 31, 2008 9:13 am
by awa
Thanks kryles - that looks promising.
Will let you know how I get on.

Re: Combine fields in PHP form

Posted: Tue Apr 01, 2008 9:54 am
by awa
Having read the above properly and sitting down to do this, I see it still isn't actually what I need.

The items are not actually numbered - that was just for example. Samples might be

q_angled_brackets
s_angled_brackets

q_corner_piece
s_corner_piece

... and so on

Re: Combine fields in PHP form

Posted: Tue Apr 01, 2008 12:21 pm
by thaynejo
awa wrote:Having read the above properly and sitting down to do this, I see it still isn't actually what I need.

The items are not actually numbered - that was just for example. Samples might be

q_angled_brackets
s_angled_brackets

q_corner_piece
s_corner_piece

... and so on
Since that is the case, yes you will need to write a combination for each one. You can do it one of two ways.

One:

Code: Select all

if ($Field == "q_angled_brackets") $msg .= "Angeled Brackets: $_POST['q_angled_brackets'] x $_POST['s_angled_brackets'];
if ($Field == "q_corner_piece") $msg .= "Corner Piece: $_POST['q_corner_piece'] x $_POST['s_corner_piece'];
Two:

Code: Select all

switch ($Field) {
case "q_corner_piece":
  $msg .= "Corner Piece: $_POST['q_corner_piece'] x $_POST['s_corner_piece'];
  break;
case "q_angeled_brackets":
  $msg .= "Angeled Brackets: $_POST['q_angled_brackets'] x $_POST['s_angled_brackets'];
  break;
}
I have not verified the code is working as is, but those are the two options you have that I know of. I personally prefer the switch statement, but I know others that would prefer the if statements as well. Feel free to correct me if anyone finds something wrong.

Re: Combine fields in PHP form

Posted: Tue Apr 01, 2008 12:34 pm
by awa
Thanks very much - I was beginning to think I may need to do a thing for each set, but it's a big help to see the two ways of doing it.