Php $\将输入值分为两部分(分解?)

Php $\将输入值分为两部分(分解?),php,email,post,explode,Php,Email,Post,Explode,我对PHP非常陌生,所以这可能是一个相当愚蠢的问题 我正在使用PHP提交一个电子邮件表单,希望电子邮件包含表单输入的值。这是一个精简版: <?php if(isset($_POST['submit'])) { $to = 'address@gmail.com' ; $subject = 'Subject'; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; char

我对PHP非常陌生,所以这可能是一个相当愚蠢的问题

我正在使用PHP提交一个电子邮件表单,希望电子邮件包含表单输入的值。这是一个精简版:

<?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');
} ?>
然后,在某个地方,
$\u POST($partVal[0])
$\u POST($partVal[1])

但我不知道这应该发生在哪里,我把它放在任何地方似乎都会让整个事情破裂

谢谢你的帮助。

试试看

$wholeVal = $_POST('some-name');
$list($ammount, $type) = explode("-",$wholeVal);
然后输入您的电子邮件:

$message = "<p>" . $ammount . "</p><p>" . $type . "</p>" ;
$message=“”$阿蒙。“

”$类型。“

”;

首先,您应该使用括号访问$\u POST,如
$\u POST['some-name']
<代码>分解
返回一个数组。因此,在您的示例中,
explode('-','10个小部件')[0]
将返回
'10'
explode('-','10个小部件')[1]
将返回
'widgets'

因此,您的代码如下所示:

<?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');
  } 
?>

我知道您目前正在寻找功能,但请记住,如果您用“-”分隔字符串,脚本小孩(甚至是出于好意的人)很容易使用连字符,您的脚本就会崩溃


只是一个提示:D

完美,正是我所需要的。我正在慢慢地但肯定地学习PHP.+1,因为我没有使用
split
,我建议在
header
@BookOfZeus之后使用
exit
?在本例中,代码实际上到此为止。@niomaster的原因很简单,如果有一天您在之后添加代码,则不会有任何中断。我同意你的看法,你不需要它,因为
标题后面没有代码,但是如果有一天你决定将此代码包含在其他代码中(包含在文件的末尾),那么就不会有任何代码中断。
<?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');
} ?>
<?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');
  } 
?>