简体   繁体   English

为什么我无法使用php从我的表单中获取访问者的数据?

[英]Why can't I get the visitor's data, from my form, in my email using php?

I use Dreamweaver CS6 and the php code that I'm using sends the form to my email, but not the data from who fills it out. 我使用Dreamweaver CS6,而我正在使用的php代码会将表单发送到我的电子邮件中,但不会将填写者的数据发送到我的电子邮件中。 My code on my php is (actual email is replaced with "email@email.com": 我在php上的代码是(实际电子邮件已替换为“ email@email.com”:

    <?php
$to= "email@email.com";
$subject= "Endorsement Form";
$message= "Endorser name: " . $_POST['name'] . "\r\n" . 
"Email:             " . $_POST['email'] . "\r\n" . 
"Address:           " . $_POST['address'] . "\r\n" . 
"City:              " . $_POST['city'] . "\r\n" . 
"State:             " . $_POST['state'] . "\r\n" . 
"Zip Code:          " . $_POST['zipcode'] . "\r\n" . 
"Phone Number:      " . $_POST['phonenumber'] . "\r\n" . 
"Endorsement Type   " . $_POST['endorsementtype'] . "\r\n" . "\r\n" . 
"Endorsement Quote  " . $_POST['endorsementquote'] . "\r\n" ."\r\n" .
$from= $_POST['email'];
$headers= "From: $from" . "\r\n";
$headers .= "Bcc: email@email.com" . "\r\n";
mail($to,$subject,$message,$headers);  
?>

And my HTML form: 而我的HTML表单:

    <form id="endorsementForm" name="endorsementForm" method="post"     action="email_form.php" enctype="text/plain">
        <span id="sprytextfield1">
          <label for="name">Name:</label>
          <br />
          <input type="text" name="name" id="name" />
          <span class="textfieldRequiredMsg">*</span></span>
            <p><span id="sprytextfield2">
          <label for="email">Email:<br />
          </label>
          <input type="text" name="email" id="email" />
          <span class="textfieldRequiredMsg">*</span><span     class="textfieldInvalidFormatMsg">*</span></span></p>
        <p><span id="sprytextfield3">
          <label for="address">Address:</label>
          <br />
          <input type="text" name="address" id="address" />
          <span class="textfieldRequiredMsg">*</span></span></p>
        <p><span id="sprytextfield4">
          <label for="city">City:</label>
          <br />
          <input type="text" name="city" id="city" />
          <span class="textfieldRequiredMsg">*</span></span></p>
        <p><span id="sprytextfield5">
          <label for="state">State:<br />
          </label>
          <input type="text" name="state" id="state" />
          <span class="textfieldRequiredMsg">*</span></span></p>
        <p><span id="sprytextfield6">
          <label for="zipcode">Zip Code:<br />
          </label>
          <input type="text" name="zipcode" id="zipcode" />
          <span class="textfieldRequiredMsg">*</span><span     class="textfieldInvalidFormatMsg">*</span></span></p>
        <p><span id="sprytextfield7">
          <label for="phonenumber">Phone Number:</label>
          <br />
          <input type="text" name="phonenumber" id="phonenumber" />
          <span class="textfieldRequiredMsg">*</span></span></p>
        <p>Endorsment Type:</p>
        <p><span id="spryradio1">
          <label>
            <input type="radio" name="endorsementtype" value="type1"     id="endorsementtype_0" />
            Personal</label>
          <br />
          <label>
            <input type="radio" name="endorsementtype" value="type2"     id="endorsementtype_1" />
            Professional</label>
          <br />
          <label>
            <input type="radio" name="endorsementtype" value="type3"     id="endorsementtype_2" />
            Organization</label>
          <br />
          <span class="radioRequiredMsg">Please make a selection.</span>    </span></p>
        <p>Endorsment Quote:</p>
        <p><span id="sprytextarea1">
          <textarea name="endorsementquote" id="endorsementquote" cols="45"  rows="5"></textarea>
          <span class="textareaRequiredMsg">*</span></span></p>
        <p>
          <input type="submit" name="submit" id="submit" value="Submit" />
          <input type="reset" name="reset" id="reset" value="Clear Form" />

Consult my footnote. 请查阅我的脚注。

Given the probable missing </form> tag in your question (something I commented on under your question), you're not checking if any of your inputs are empty, in turn leading to no data sent in mail. 考虑到您的问题中可能缺少</form>标记(我在您的问题下作了评论),您不会检查任何输入是否为空,从而导致没有邮件发送数据。

You need to use a conditional !empty() against your inputs. 您需要对输入使用条件!empty()

Sidenote: The && logical operator means AND . 旁注: &&逻辑运算符表示AND You can also use the || 您也可以使用|| being the logical OR operator, depending on what you wish to check for. 是逻辑OR运算符,具体取决于您要检查的内容。

Ie: 即:

 if(!empty($_POST['var1']) && !empty($_POST['var2']) ) 
 {

 // run your code

 }

which you can add to that with your other POST arrays. 您可以将其与其他POST数组一起添加。

Reference: 参考:

Plus, if your HTML form and PHP are inside the same file (which is unknown but probable), that will also send empty data. 另外,如果您的HTML表单和PHP位于同一文件内(未知但可能),那么这也将发送空数据。

Therefore, use a conditional isset() for your submit button, followed by the conditional empty() inside that. 因此,请为您的提交按钮使用条件isset() ,然后在其中使用条件empty()

You're also open to an XSS injection with the POST arrays inside the $message variable. 您还可以使用$message变量中的POST数组进行XSS注入。

Assign variables to POST arrays, and use validation filters. 将变量分配给POST数组,并使用验证过滤器。

You also have a syntax error at the end here: 您在此末尾还会遇到语法错误:

"Endorsement Quote  " . $_POST['endorsementquote'] . "\r\n" ."\r\n" .

You did not close it properly, if that's your actual code. 如果这是您的实际代码,则未正确关闭它。

If should contain a closing semi-colon and losing the last dot: 如果应包含一个结束的分号并丢失最后一个点:

"Endorsement Quote  " . $_POST['endorsementquote'] . "\r\n" ."\r\n";

Error reporting would have caught that. 错误报告会发现这一点。


Footnote: 脚注:

Edit: As noted by Riggs. 编辑:如Riggs所述。

You need to remove enctype="text/plain" 您需要删除enctype="text/plain"

I didn't see that passed what is shown. 我没有看到显示的内容。 But my answer above still applies and you should be doing that, because you will still end up with probable empty data. 但是我上面的答案仍然适用,您应该这样做,因为您仍然会得到可能为空的数据。

Here is an article on that here on Stack: 这是一篇关于堆栈的文章:

Remove the enctype= property from your <form> tag to let it default to application/x-www-form-urlencoded or change the encytpe to <form>标记中删除enctype=属性,使其默认为application/x-www-form-urlencoded或将encytpe更改为

enctype="application/x-www-form-urlencoded"

Also @Fred-ii- makes a lot of good points @ Fred-ii-也有很多好处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM