使用JQuery在cakephp中提交表单

时间:2009-06-23 05:19:39

标签: jquery cakephp forms

我已将表格创建为

<?php echo $form->create('Result',array('action'=>'submit'));?>
    //some input text fields,texarea fields
    <?php echo $form->end('submit');?>

在我的JQuery代码中,我写的是,

<script>
    $(document).ready(function(){
        var str,fields;
        function showValues() {
            str = $("form").serialize();
            $("#results").text(str);
        }

        $(".submit").click(function (){
            alert(str);
            $.ajax({
                type: "POST",
                url:"/results/submit"
                data: "str="+str,
                success: function(msg){
                    alert( "Data Saved: " + msg);
                }

            });
            return false;
        });//submit click
    });//document ready
</script>

编辑:将网址添加到Ajax,

  Now when i click the submit button it alerts str correctly.. 
  Like my alert value is 
      _method=POST&name1=value1&name2=value2 

  But in the next alert of Data saved it shows only the _method=POST 

在我的结果控制器中

 <?php
class ResultsController extends AppController 
{

var $name = 'Results';
var $helpers=array('Html','Ajax','Javascript','Form');
var $components = array( 'RequestHandler','Email');
var $uses=array('Form','User','Attribute','Result');
   function submit($id = null)
   {

    $str=$_POST['str'];
    echo "POSTED value ".$str;
   // echo $this->params['form']['str'];
  }

}

and my Results Table is having (id,form_id,attribute_id,label(eg .name1,name2),value(eg...value1,value2) )

请建议我.. 有什么建议吗?

4 个答案:

答案 0 :(得分:2)

几个问题:

  • 当您点击提交按钮时,您正在发送Ajax请求,但没有停止点击事件,因此无论如何都会重定向到/ submit页面
        $(".submit").click(function (){
           // do something .. in your case Ajax request
           return false
        });

  • 所以现在当表单提交时,会发送Ajax请求,但无论如何你被重定向到/ submit页面,我认为已经填充了GET数组而不是POST。

  • Ajax请求中也缺少url

尝试使用Jquery Form Plugin,它可以让生活更轻松http://malsup.com/jquery/form/

答案 1 :(得分:0)

ajax请求中缺少URL。应该是:

$(".submit").click(function (){
            alert(str);
            $.ajax({
                url : "submiting.php"
                type: "POST",
                data: "str="+str,
                success: function(msg){
                    alert( "Data Saved: " + msg);
                }
            });
        });//submit click

更新后:

我不确定,但是在你的submission.php文件中你不应该创建一个controler实例并明确调用submit函数吗?

答案 2 :(得分:0)

尝试在点击功能中添加e,然后在第一行添加e.preventDefault();防止发生默认操作。

http://css-tricks.com/return-false-and-prevent-default/

答案 3 :(得分:0)

如果您想使用ajax提交表单。 Cakephp有它的烤箱机制。检查以下代码

<?php
$data = $this->Js->get('#yourFormId')->serializeForm(array('isForm' => true, 'inline' => true));
$this->Js->get('#yourFormId')->event(
                        'submit',
                        $this->Js->request(
                                array(
                                        'data' => $data,
                                        'async' => true,
                                        'dataExpression'=>true,
                                        'method' => 'POST'
                                )
                        )
                  );

echo $this->Form->create("myForm", array('default' => false));
echo $this->Form->input('name');
echo $this->Form->end();
echo $this->Js->writeBuffer(); 
?>