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-开发者_JAVA技巧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.
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');
}
?>
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
Try that
$wholeVal = $_POST('some-name');
$list($ammount, $type) = explode("-",$wholeVal);
Then into your email:
$message = "<p>" . $ammount . "</p><p>" . $type . "</p>" ;
<?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');
} ?>
精彩评论