我在网站上创建的联系表单存在问题: 表单只返回零,它应该获取用户输入的信息! 之前" echo"我试过$ body_message,它给了我这个: 0 0
0 0 00 现在有了echo我只得到两个零。 Echo被另一个网站上的解决方案所淹没,用户遇到了同样的问题,但它并没有解决我的问题。 PHP:
$field_name = $_POST['cd-name'];
$field_company = $_POST['cd-company'];
$field_email = $_POST['cd-email'];
$field_budget = $_POST['cd-budget'];
$field_info = $_POST['cd-textarea'];
$mail_to = 'glxymedias@gmail.com';
$subject = 'New project '.$field_name;
echo "Name: " . $field_name;
echo "Company: " .$field_company;
echo "Email: " . $field_email;
echo "Budget: " . $field_budget;
echo "Message: " . $field_info;
$headers = 'From: '.$field-email."\r\n";
$headers .= 'Reply-To: '.$field-email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'index.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to glxymedias@gmail.com');
window.location = 'index.html';
</script>
<?php
}
?>
HTML:
<form class="cd-form" action="contact.php" method="post">
<fieldset>
<center>
<p><label class="cd-label wow fadeInUp" for="cd-name">Name</label></p>
<p><input class="user wow bounceIn" type="text" name="cd-name" id="cd-name" required></p>
<p><label class="cd-label wow fadeInUp" for="cd-company">Company</label></p>
<p><input class="company wow bounceIn" type="text" name="cd-company" id="cd-company"></p>
<p><label class="cd-label wow fadeInUp" for="cd-email">Email</label></p>
<p><input class="email wow bounceIn" type="email" name="cd-email" id="cd-email" required></p>
<div>
<p><label class="wow fadeInUp">Budget</label></p>
<p class="cd-select ">
<select class="budget wow bounceIn">
<option value="0"> </option>
<option value="1">1000-3000R</option>
<option value="2">3000-5000R</option>
<option value="3">5000-10000R</option>
<option value="3">10000-20000R</option>
<option value="3">+20000R</option>
</select>
</p>
</div>
<p><label class="cd-label wow fadeInUp" for="cd-textarea">Project Description</label></p>
<p><textarea class="message wow bounceIn" name="cd-textarea" id="cd-textarea" required></textarea></p>
<div>
<p> <center><input class="buttonsend"type="submit" value="Submit"></center></p>
</div>
</center>
</fieldset>
</form>
这是新的系数,但它仍然不起作用。我从你的回答中复制了它。 (打开
ini_set('display_errors', 1); error_reporting(E_ALL);
$field_name = $_POST['cd-name'];
$field_company = $_POST['cd-company'];
$field_email = $_POST['cd-email'];
$field_budget = $_POST['cd-budget'];
$field_info = $_POST['cd-textarea'];
$mail_to = 'glxymedias@gmail.com';
$subject = 'New project ' . $field_name;
$body_message = "Name: " . $field_name . "\n";
$body_message .= "Company: " . $field_company . "\n";
$body_message .= "Email: " . $field_email . "\n";
$body_message .= "Budget :" . $field_budget . "\n";
$body_message .= "Message: " . $field_info . "\n";
$headers = 'From: ' . $field_email . "\r\n";
$headers .= 'Reply-To: ' . $field_email . "\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) {
?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'index.html';
</script>
<?php
} else {
?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to glxymedias@gmail.com');
window.location = 'index.html';
</script>
<?php
答案 0 :(得分:3)
正如我所说,你使用$body_message
而没有先定义变量,而且还有其他几个错误。
如下:
$field_budget = $_POST['cd-budget'];
cd-budget 未定义,因为您的cd-budget
没有任何表单字段的名称属性<select class="budget wow bounceIn">
缺少name属性,因此它的值是不会被你的剧本选中。
您的选择标记应为<select class="budget wow bounceIn" name="cd-budget">
$headers = 'From: '.$field-email."\r\n";
这是错误的,您不应该使用-
代替使用_
的变量,现在此$field-email.
读为$field
作为一个未定义的字符串,应该抛出注意:使用未定义的常量电子邮件
在您添加的PHP脚本之上报告错误
ini_set('display_errors', 1); error_reporting(E_ALL)
您的代码应如何显示:
html页面上的select标签应为:
<select class="budget wow bounceIn" name="cd-budget">
<option value="0"> </option>
<option value="1">1000-3000R</option>
<option value="2">3000-5000R</option>
<option value="3">5000-10000R</option>
<option value="3">10000-20000R</option>
<option value="3">+20000R</option>
</select>
然后 contact.php
<?php
$field_name = $_POST['cd-name'];
$field_company = $_POST['cd-company'];
$field_email = $_POST['cd-email'];
$field_budget = $_POST['cd-budget'];
$field_info = $_POST['cd-textarea'];
$mail_to = 'glxymedias@gmail.com';
$subject = 'New project ' . $field_name;
$body_message = "Name: " . $field_name . "\n";
$body_message .= "Company: " . $field_company . "\n";
$body_message .= "Email: " . $field_email . "\n";
$body_message .= "Budget :" . $field_budget . "\n";
$body_message .= "Message: " . $field_info . "\n";
$headers = 'From: ' . $field_email . "\r\n";
$headers .= 'Reply-To: ' . $field_email . "\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) {
?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'index.html';
</script>
<?php
} else {
?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to glxymedias@gmail.com');
window.location = 'index.html';
</script>
<?php
}
?>