【问题标题】:codeigniter form receives blank emails from bootstrap formcodeigniter 表单从引导表单接收空白电子邮件
【发布时间】:2018-12-31 15:04:31
【问题描述】:

我创建了一个网站,其中包含两个联系表格,一个是预订表格,另一个是普通的“联系我们”表格。两个表单数据都将出现在 HTML 表中。问题是我收到了邮件主题为我声明的空白电子邮件(来自网站的预订来自网站的查询)。但是当我尝试发送表单数据而不填写它时,它不允许我发送,html“必需”属性告诉“请填写此字段”。但是我收到了附件中显示的空白电子邮件 我的查看页面是

<form id="contact_form" method="post" enctype="multipart">
          <div class="form-group">
            <input type="text" class="form-control" name="name" value="" placeholder="Name" required>
          </div>
          <div class="form-group">
            <input type="email" class="form-control" name="email" value="" placeholder="E-mail" required>
          </div>
          <div class="form-group">
            <input type="tel" class="form-control" name="phone" value="" placeholder="Phone" required>
          </div>
          <div class="form-group">
            <textarea class="form-control" name="message" rows="3" placeholder="Message"></textarea>
          </div>
          <button class="btn btn-default" type="submit" name="submit">
              <i class="fa fa-paper-plane-o" aria-hidden="true"></i>Submit
          </button>
        </form>

jquery 是

<script>
            $("#contact_form").on('submit', function(e)
            {
                var temp=0;
                e.preventDefault();             
                var data = new FormData($(this))
                $.ajax({

                    url: "<?php echo base_url();?>index.php/home/contact_details",
                    type: "POST",
                    dataType:'json',
                    data:  new FormData(this),
                    contentType: false,
                    cache: true,
                    processData:false,
                    success: function(data)
                    {  
                        alert("Successfully submitted");
                    }
                });  
            });
        </script>

我的控制器功能是

public function contact_details()
    {
        $data['name']=$name=$this->input->post('name');
        $data['email']=$email=$this->input->post('email');
        $data['phone']=$phone=$this->input->post('phone');
        $data['message']=$message=$this->input->post('message');
        $config = Array(        
            'protocol' => 'sendmail',
            'smtp_host' => 'localhost',
            'smtp_port' => 25,
            'smtp_user' => 'SMTP Username',
            'smtp_pass' => 'SMTP Password',
            'smtp_timeout' => '4',
            'mailtype'  => 'html', 
            'charset'   => 'iso-8859-1'
        );
        $message='<table border="1" width="100%">
         <tr>
           <td>Name</td>
           <td>'.$name.'</td>
           </tr>
           <tr>
           <td>Phone</td>
           <td>'.$phone.'</td>
            </tr>
           <tr>
           <td>Email</td>
           <td>'.$email.'</td>
            </tr>
           <tr>
           <td>Message</td>
           <td>'.$message.'</td>
         </tr>            
        </table>';
         $this->email->set_mailtype("html");
        $this->load->library('email', $config);
        $this->email->from($email); 
        $this->email->to('example@example.ae');
        $this->email->cc('example@example.com');
        $this->email->cc('example@example.ae');  
        $this->email->subject('Enquiry from website');
        $this->email->message($message);    
        $sent=$this->email->send();
        $var=1;
        echo $var;

    }

【问题讨论】:

    标签: php jquery codeigniter email


    【解决方案1】:

    这个$data['name']=$name=$this-&gt;input-&gt;post('name'); 语法很奇怪。

    试试$name = $this-&gt;input-&gt;post('name');

    同样适用于以下(电子邮件、电话、消息)。

    【讨论】:

      【解决方案2】:

      用这样的方法来做

      <script>
          $("button[type='submit']").click(function(e) {
              e.preventDefault();
              $.ajax({
      
                  url: "<?php echo base_url();?>index.php/home/contact_details",
                  type: "POST",
                  dataType:'json',
                  data:  $(this).closest('form').serialize(),
                  contentType: false,
                  cache: true,
                  processData:false,
                  success: function(data)
                  {  
                      alert("Successfully submitted");
                  }
              });  
          });
      </script>
      

      这样做的好处是您可以使此代码可重用。你可以改变url。例如,我们将在带有url: $(this).closest('form').data('url') 的函数中添加&lt;form data-url="&lt;?php echo base_url('home/contact_details'); ?&gt;"&gt; 调用它,并检查发布数据中是否有值。在控制器中执行此操作。

      而不是使用$data['name']=$name=$this-&gt;input-&gt;post('name');。你可以做$data = $this-&gt;input-&gt;post();。一旦您print_r() 该数据,您将以数组形式看到它,您可以通过$data['name'] 调用它,这取决于selectorname

      【讨论】:

        猜你喜欢
        • 2013-10-30
        • 2016-06-07
        • 1970-01-01
        • 1970-01-01
        • 2017-09-13
        • 2016-09-06
        • 1970-01-01
        • 2014-02-12
        • 1970-01-01
        相关资源
        最近更新 更多