简体   繁体   English

提交返回错误,但仍提交表单-PHP,Javascript,AJAX,CodeIgniter

[英]Submit Return False, But Still Submit Form - PHP, Javascript, AJAX, CodeIgniter

This is probably going to sound backwards. 这听起来可能会倒退。 I need for a form to submit return false, but still submit the form. 我需要一个表单来提交return false,但是仍然提交表单。

I have a form being pulled into a page via ajax (jquery load function), and on submit I'd like to display a graphic and some text in the same div, rather than redirect the page. 我有一个表单通过ajax(jquery加载功能)被拉入页面,并且在提交时我想在同一div中显示图形和一些文本,而不是重定向页面。

This is my submit button (codeigniter): 这是我的提交按钮(codeigniter):

<?php $attributes = array('class' => 'button', 'onclick' => 'form_success_client(); return false;', 'name' => 'submit'); echo form_submit( $attributes, 'Save'); ?>

and in html: 并在html中:

<input type="submit" onclick="form_success(); return false;" class="button" value="Save" name="submit">

and the javascript function which loads the success message: 以及加载成功消息的javascript函数:

 function form_success_client() { // form success
    $('.contentarea').load("/mm/index.php/site/form_success_client/");
 }

That all works fine, but unsurprisngly it doesn't submit the form. 一切正常,但毫不奇怪的是,它没有提交表格。 I know that the proper way to do this, is to pass the form submission over to jquery, but I'm not sure how to do that. 我知道执行此操作的正确方法是将表单提交传递给jquery,但是我不确定如何执行此操作。 Could do with a quick fix if possible (until I have time to sort a better solution out), however all suggestions appreciated. 可能的话可以进行快速修复(直到我有时间整理出更好的解决方案),但是所有建议都值得赞赏。

Thanks! 谢谢!

ANSWER: 回答:

This is what worked for me, just a slight edit of Maggie's answer: 这就是我的工作,对Maggie的答案进行了少许编辑:

function form_success_client(obj) {
    $.ajax({
       type: 'POST',
       url: $(obj).attr('action'),
       data: $(obj).serialize(),
       success: function(msg){
         $('.contentarea').load("/mm/index.php/site/form_success_client/");
       }
     });
     return false;
}

Note the $() wrapping obj on url and data. 注意$()将obj包装在url和data上。

bind your JS onclick-event to the form (not the button) like this 'onclick' => 'form_success_client(this); return false;' 像这样'onclick' => 'form_success_client(this); return false;'将您的JS onclick-event绑定到表单(而不是按钮'onclick' => 'form_success_client(this); return false;' 'onclick' => 'form_success_client(this); return false;'

and change your function to 并将您的功能更改为

function form_success_client(obj) {
    $.ajax({
       type: "POST",
       url: obj.attr("action"),
       data: obj.serialize(),
       success: function(msg){
         $('.contentarea').load("/mm/index.php/site/form_success_client/");
       }
     });
}

untested 未经测试

Possible solution. 可能的解决方案。 I'm sticking to your code 我坚持你的代码

<input type="submit" onclick="form_success_client(this); return false;" class="button" value="Save" name="submit">

function form_success_client( i ) {
   formData = $(i).parents('form').serialize();
   $.post( "/mm/index.php/site/form_success_client/", formData, function(loaded) { $('.contentarea').append(loaded) }, 'html' );
}

EDIT: And yes, as maggie said, you should bind form_success_client(this); return false; 编辑:是的,正如玛吉所说,您应该绑定form_success_client(this); return false; form_success_client(this); return false; like that 像那样

<form onsubmit="form_success_client(this); return false;">

not to the button 不按按钮

You can use jquery .post , .get or .ajax 您可以使用jquery .post,.get或.ajax

based on SUCCESS or returned data you can submit the form 根据成功或返回的数据,您可以提交表格

http://api.jquery.com/jQuery.get/ http://api.jquery.com/jQuery.get/

http://api.jquery.com/jQuery.post/ http://api.jquery.com/jQuery.post/

http://api.jquery.com/jQuery.ajax/ http://api.jquery.com/jQuery.ajax/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM