我创建了一个网站,其中包含两个联系表,一个是预订表,另一个是正常的“联系我们”表。两种表单数据都将出现在HTML表中。问题是我收到我声明的邮件主题为空白的电子邮件(从网站预订,从网站查询)。但是,当我尝试发送表单数据而不填写它时,它将不允许我发送,html“ required”属性告诉“请填写此字段”。但我收到附件中显示的空白电子邮件 我的查看页面是
<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;
}
答案 0 :(得分:0)
此$data['name']=$name=$this->input->post('name');
是奇怪的语法。
尝试$name = $this->input->post('name');
以下内容(电子邮件,电话,消息)相同。
答案 1 :(得分:0)
用类似的方法做
<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
。例如,我们将在<form data-url="<?php echo base_url('home/contact_details'); ?>">
函数中添加url: $(this).closest('form').data('url')
来调用它,并检查发布数据中是否有值。在控制器中执行此操作。
代替使用$data['name']=$name=$this->input->post('name');
。您可以执行$data = $this->input->post();
。 print_r()
的数据一旦出现,您将以数组形式看到它,并且可以通过$data['name']
调用它,具体取决于name
的{{1}}。