3

I'm really new to PHP, so this is probably a pretty dumb question.

I'm using PHP to submit an email form, and would like the email to contain the values of some of the form's inputs. Here's a stripped down version:

<?php
if(isset($_POST['submit'])) {
  $to = 'address@gmail.com' ;
  $subject = 'Subject';
  $headers  = 'MIME-Version: 1.0' . "\r\n";
  $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  $message =

  //here are the values that the email will send me 
  "<p>".$_POST('some-name')."</p>
   <p>".$_POST('some-name')."</p>" ;

  mail($to, $subject, $message, $headers);
  header('Location: ../estimate.html');
} ?>

The value of input some-name is, say, 10-widgets posted from the form.

Here's the question: instead of listing "10-widgets" twice (as the above code will do), how do I list the first part in the first <p> (so it would be "10") and the second part in the second <p> (so it would be "widgets")?

Something like the following seems promising:

$wholeVal = $_POST('some-name');
$partVal = explode("-",$wholeVal);

and then, somewhere, $_POST($partVal[0]); and $_POST($partVal[1]);

But I don't know where this should take place, and anywhere I put it seems to make the whole thing break.

Thanks for your help.

Tomas Mulder
  • 2,207
  • 2
  • 16
  • 18

4 Answers4

3

First, you should access $_POST with brackets, like $_POST['some-name']. explode returns an array. So in your example, explode('-', '10-widgets')[0] will return '10' and explode('-', '10-widgets')[1] will return 'widgets'

So your code will be something like this:

<?php
  if(isset($_POST['submit'])) {
    $to = 'address@gmail.com' ;
    $subject = 'Subject';
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    $parts = explode('-', $_POST['some-name']);

    //here are the values that the email will send me 
    $message = 
      "<p>".$parts[0]."</p>
      <p>".$parts[1]."</p>";

    mail($to, $subject, $message, $headers);
    header('Location: ../estimate.html');
  } 
?>
Pieter Bos
  • 1,554
  • 1
  • 12
  • 20
  • _Perfect_, just what I needed. Slowly but surely I'm learning PHP. – Tomas Mulder Oct 15 '11 at 07:50
  • +1 for not using `split` and I would recommend to use `exit` after `header` – Book Of Zeus Oct 15 '11 at 12:31
  • @BookOfZeus Why would you use exit? In this example the code practically ends there. – Pieter Bos Oct 15 '11 at 14:48
  • @niomaster simply for the fact that if someday you add code after, nothing will break. I agree with you, you don't need it since there's no code after the `header` but if one day you decide to include this code in some other code (include at the of the file) then nothing will break. – Book Of Zeus Oct 15 '11 at 15:02
2

I know you're looking for functionality at the moment but keep in mind that if you're splitting a string by a "-" it'd be really easy for a script kiddie (or even a well intentioned person for that matter) to use a hyphenated word and your script will break.

Just a heads up :D

Peter
  • 3,144
  • 11
  • 37
  • 56
1

Try that

$wholeVal = $_POST('some-name');
$list($ammount, $type) = explode("-",$wholeVal);

Then into your email:

$message = "<p>" . $ammount . "</p><p>" . $type . "</p>" ;
KodeFor.Me
  • 13,069
  • 27
  • 98
  • 166
0
<?php
if(isset($_POST['submit'])) {
  $to = 'address@gmail.com' ;
  $subject = 'Subject';
  $headers  = 'MIME-Version: 1.0' . "\r\n";
  $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  $message =

    $wholeVal = $_POST('some-name');
    $partVal = explode("-",$wholeVal);
  //here are the values that the email will send me 
  "<p>".$partVal[0]."</p>
   <p>".$partVal[1]."</p>" ;

  mail($to, $subject, $message, $headers);
  header('Location: ../estimate.html');
} ?>
Mohit Bumb
  • 2,466
  • 5
  • 33
  • 52