[英]codeigniter and ajax contact form
I'm trying to use ajax within my contact form in a codeigniter app. 我正在尝试在Codeigniter应用程序的联系表单中使用Ajax。 I have it to where the ajax call is made, but no post data is being sent to the server.
我将其发送到进行了Ajax调用的位置,但是没有将后数据发送到服务器。 I can't figure out why.
我不知道为什么。 Please help.
请帮忙。
I do have some returns in there, but they do nothing. 我确实有一些回报,但是他们什么也没做。 Also, $this->input->post('name') is null.
同样,$ this-> input-> post('name')为null。
Form view 表格检视
<?php
echo form_open('welcome/submit_contact');
echo form_error('name');
echo form_label('Name', 'name'"');
echo form_input('name', set_value('name'), 'id="name);
echo form_error('email');
echo form_label('Email', 'email');
echo form_input('email', set_value('email'), 'id="email"');
echo form_error('phone');
echo form_label('Phone', 'phone');
echo form_input('phone', set_value('phone'), 'id="phone"');
#echo '<h5>Do not start with "1" and no dashes.</h5>';
echo form_error('message');
echo form_label('Message', 'message');
echo form_textarea('message', set_value('message'), 'id="message"');
$submitData = array(
'name' => 'submit',
'value' => 'Submit',
'id' => 'button'
);
echo form_submit($submitData);
echo form_close();
?>
<script type="text/javascript">
$(function() {
$('form').click(function() {
// get the form values
var form_data = {
name: $('#name').val(),
email: $('#email').val(),
message: $('#message').val()
};
// send the form data to the controller
$.ajax({
url: "<?php echo site_url('welcome/submit_contact'); ?>",
type: "post",
data: form_data,
success: function(msg) {
$('form').prepend('<h5 class="good">Message sent!</h5>');
$('h5.good').delay(3000).fadeOut(500);
alert(msg);
}
});
// prevents from refreshing the page
return false;
});
});
</script>
Controller function 控制器功能
function submit_contact()
{
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<h4 class="bad">', '</h4>');
$name = $this->input->post('name');
echo "name = ".$name;
$this->form_validation->set_rules('name', 'Name', 'trim|required|alpha_dash|xss_clean');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|xss_clean');
$this->form_validation->set_rules('phone', 'Phone' , 'trim|integer|exact_length[10]|xss_clean');
$this->form_validation->set_rules('message', 'Message', 'trim|required|max_length[1000]|xss_clean');
// there are validation errors
if($this->form_validation->run() == FALSE)
{
return "error";
}
else // there are no validation errors
{
/*************************
need to actually send the email
*************************/
return null;
}
}
EDIT: I've updated the code in my question. 编辑:我已经更新了我的问题中的代码。 Basically now if there are validation errors, how would I get them to display on the form?
基本上现在,如果存在验证错误,我如何使它们显示在表单上? I'm assuming I would return a value from my controller and then have a statement in my ajax success that if msg == "error" display the errors elseif msg == null, display success message.
我假设我将从控制器返回一个值,然后在ajax成功中有一条语句,如果msg ==“ error”显示错误elseif msg == null,则显示成功消息。 But how would i tell my view to display those errors based on an ajax success variable?
但是我如何告诉我的看法基于ajax成功变量显示那些错误?
i think you should put id on input and textarea not on label ie 我认为你应该把id放在输入和textarea而不是标签上
$data = array
(
"name"=>'message',
"value"=>'message',
"id"=>'message'
)
form_textarea($data);
if you set the id on the label then jquery will pick up nothing from what the user inserted and also codeigniter validation won't work correctly. 如果您在标签上设置id,则jquery不会从用户插入的内容中获取任何内容,并且codeigniter验证将无法正常工作。 This is why your post result to be NULL
这就是为什么您的帖子结果为NULL
the same for other input field 其他输入字段相同
EDIT 编辑
you are asking data via ajax so return a nice json object (remove all your debug prints first): 您正在通过ajax询问数据,因此返回一个不错的json对象(首先删除所有调试输出):
// there are validation errors
if($this->form_validation->run() == FALSE)
{
echo(json_encode("validate"=>FALSE));
}
else // there are no validation errors
{
/*************************
need to actually send the email, then send you mail
*************************/
echo(json_encode("validate"=>TRUE));
}
then test it on your ajax success function to display positive or negative message 然后在ajax成功函数上对其进行测试,以显示肯定或否定消息
<script type="text/javascript">
$(function() {
$('form').click(function() {
// get the form values
var form_data = {
name: $('#name').val(),
email: $('#email').val(),
message: $('#message').val()
};
// send the form data to the controller
$.ajax({
url: "<?php echo site_url('welcome/submit_contact'); ?>",
type: "post",
data: form_data,
dataType: "json"
success: function(msg)
{
if(msg.validate)
{
$('form').prepend('<h5 class="good">Message sent!</h5>');
$('h5.good').delay(3000).fadeOut(500);
}
else
//display error
}
});
// prevents from refreshing the page
return false;
});
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.