CakePHP的2.3 AJAX表单提交 [英] cakephp 2.3 ajax form submission
本文介绍了CakePHP的2.3 AJAX表单提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个简单的表单提交数据到数据库中使用Ajax。这里是我的看法。
<?PHP的echo $这个 - >基于HTML>脚本('jQuery的',FALSE); ?>
<?PHP的echo $这个 - >基于HTML>脚本('验证',FALSE); ?>
< DIV ID =成功>< / DIV>
< H2>联系我们< / H>
< PHP
回声$这个 - >形式 - >创建();
回声$这个 - >形式 - >输入(名称,阵列('身份证'=>'名'));
回声$这个 - >形式 - >输入(电子邮件,阵列('身份证'=>电子邮件));
回声$这个 - >形式 - >输入('消息',阵列('身份证'=>'消息'));
回声$这个 - > Js->提交(发送,阵列(
=&GT'之前'; $这个 - > Js->获得('#发送) - >的效果(淡入),
'成功'=> $这个 - > Js->获得('#发送) - >的效果('淡出'),
更新=>#成功
));
回声$这个 - >形式 - >结束();
?>
< DIV ID =发送的风格=显示:无;背景色:浅绿;>发送...< / DIV>
控制器
类MessagesController扩展AppController的{
公共$佣工=阵列('的js');
公共$成分=阵列('RequestHandler的');
公共功能指数(){
如果(空($这个 - >!数据)){
如果($这个 - > MESSAGE->节省($这个 - >数据)){
如果($这 - >请求 - > isAjax()){
$这个 - >渲染('成功','阿贾克斯');
}其他{
$这个 - >会话级> setFlash(信息发送');
$这个 - >重定向(阵列('行动'=>'指数'));
}
}
}
}
}
它不是通过AJAX发送的形式..其实我的新的CakePHP的,这是我第一次我用ajax CakePHP中。同时,我想知道,我可以用codeigniter或表单提交的蛋糕PHP简单的PHP类似的语法。例如像这样
<脚本>
$('#BTN)。点击(函数(事件){
表= $(#表)序列化()。
$阿贾克斯({
键入:POST,
网址:?< PHP的回声SITE_URL('categoryController / addCategory');>中,
数据:表格,
成功:功能(数据){
$('模式')模态(隐藏)。
$(成功)淡入(500).delay(2000).fadeOut(500)。
$(#表)[0] .reset段();
//未终止字符串常量定
}
});
。事件preventDefault();
返回false; //停止实际的表单提交!重要的!
});
< / SCRIPT>
解决方案
抱歉,我得到了我的答案其实我失踪的jquery.js文件
I am trying to create a simple form to submit data to the database with ajax. here is my view
<?php echo $this->Html->script('jquery', FALSE); ?>
<?php echo $this->Html->script('validation', FALSE); ?>
<div id="success"></div>
<h2>Contact Us</h2>
<?php
echo $this->Form->create();
echo $this->Form->input('name', array('id'=>'name'));
echo $this->Form->input('email', array('id'=>'email'));
echo $this->Form->input('message', array('id'=>'message'));
echo $this->Js->submit('Send', array(
'before'=>$this->Js->get('#sending')->effect('fadeIn'),
'success'=>$this->Js->get('#sending')->effect('fadeOut'),
'update'=>'#success'
));
echo $this->Form->end();
?>
<div id="sending" style="display: none; background-color: lightgreen;">Sending...</div>
Controller
class MessagesController extends AppController{
public $helpers = array('Js');
public $components = array('RequestHandler');
public function index(){
if(!empty($this->data)){
if($this->Message->save($this->data)){
if($this->request->isAjax()){
$this->render('success','ajax');
}else{
$this->Session->setFlash('Message sent');
$this->redirect(array('action'=>'index'));
}
}
}
}
}
it is not sending the form through ajax.. actually i am new in cakephp and this is my first time i am using ajax in cakephp. and also i want to know that can i use codeigniter or simple php similar syntax of form submission in cake php. for example like this
<script>
$('#btn').click(function(event) {
form = $("#form").serialize();
$.ajax({
type: "POST",
url: "<?php echo site_url('categoryController/addCategory'); ?>",
data: form,
success: function(data){
$('.modal').modal('hide');
$(".success").fadeIn(500).delay(2000).fadeOut(500);
$("#form")[0].reset();
//Unterminated String constant fixed
}
});
event.preventDefault();
return false; //stop the actual form post !important!
});
</script>
解决方案
sorry i got my answer actually i was missing jquery.js file
这篇关于CakePHP的2.3 AJAX表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文