dsio68964998 2015-02-24 17:59
浏览 13
已采纳

联系表格没有返回适当的信息

I'm currently working through setting up a fully functional contact form using the Bootstrap framework and PHP. I'm receiving test emails from the form but it's not displaying the content the way I understood it would and I'm having some other issues.

First, the user-submitted email is showing up as being received from the host of my site. It is also not including the name of the person, which I thought I included in my PHP

Screenshot of email field returned as number

Here is my code for contact.php and the form in my index.html file

<?php 

$to = 'example@gmail.com';
$name = $_POST['name'];
$email = $_POST['email'];
$subject=$_POST['subject'];
$message = $_POST['msg'];

if ($_POST){
mail($to, $subject, $message);
$confirm= "Your email was sent successfully!";
echo "<script type='text/javascript'>confirm('$confirm');</script>";
}
else {
    $error_msg= "All fields are required!";
    echo "<script type='text/javascript'>alert('$error_msg');</script>";
}
?>

index.html

<form class="form-horizontal" role="form" action="contact.php" method="post">
                <div class="modal-header">
                  <h2>Contact</h2>
                </div>

                <div class="modal-body">
                <!--name-->
                  <div class="form-group">
                      <label for="name" class="col-sm-2 control-label">Name</label>
                      <div class="col-sm-10">
                        <input type="text" class="form-control" name="name" id="name" placeholder="First and Last Name">
                      </div>
                  </div>
                <!--email-->
                  <div class="form-group">
                      <label for="email" class="col-sm-2 control-label">Email</label>
                      <div class="col-sm-10">
                        <input type="email" class="form-control" name="email" id="email" placeholder="example@somewhere.com">
                      </div>
                  </div>
                <!--subject-->
                <div class="form-group">
                      <label for="subject" class="col-sm-2 control-label">Subject</label>
                      <div class="col-sm-10">
                        <input type="text" class="form-control" name="subject" id="subject" placeholder="Enter subject here...">
                      </div>
                  </div>
                <!--message-->
                  <div class="form-group">
                      <label for="msg" class="col-sm-2 control-label">Message</label>
                      <div class="col-sm-10">
                        <textarea class="form-control" rows="4" name="msg" id="msg" placeholder="Enter message here..."></textarea>
                      </div>
                  </div>

                </div>

                <div class="modal-footer">
                  <a class="btn btn-default" data-dismiss="modal">Close</a>
                  <button type="submit" class="btn btn-primary">Send</button>
                </div>
          </form>

Another unintended issue is that once the page is submitted, a blank page (contact.php) is loaded. I thought, looking at my code, a javascript alert or confirm box would pop up instead though? I'm new at this so, I apologize if these are dumb questions.

Thanks -K

  • 写回答

1条回答 默认 最新

  • duane9322 2015-02-24 18:09
    关注

    First of, you don't have from defined in your header. You need

    $headers = "From: sender_email@your_domain.com
    ";
    mail($to,$subject,$message,$headers);
    

    You can also modify header to send html message which is desired in most cases in addition to setting from and cc: carbon copy or bcc: blind carbon copy email addresses.

    // Always set content-type when sending HTML email
    $headers = "MIME-Version: 1.0" . "
    ";
    $headers .= "Content-type:text/html;charset=UTF-8" . "
    ";
    
    // More headers
    $headers .= 'From: <webmaster@example.com>' . "
    ";
    $headers .= 'Cc: myboss@example.com' . "
    ";
    

    Get this code incorporated in your if statement. I also noticed you have

    else {
        $error_msg= "All fields are required!";
    

    Even if there was only one field passed in as $_POST method, the else statement isn't checking if all fields were passed in. You need to break that down or check the fields individually or do it on with client side JavaScript field validation.

    What is the confirm javascript function? can you post it on here? if you're only looking for an alert, you can straight insert alert function in between script tags.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目