【问题标题】:Form submission in cakephp using JQuery使用 JQuery 在 cakephp 中提交表单
【发布时间】:2009-06-23 05:19:38
【问题描述】:

我已经像这样创建了表单

<?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>

编辑:将 Url 添加到 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) )

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

【问题讨论】:

    标签: jquery cakephp forms


    【解决方案1】:

    几个问题:

    • 当单击提交按钮时,您正在发送 Ajax 请求,但不会停止单击事件,因此无论如何它都会重定向到 /submit 页面
    $(".submit").click(函数 (){ // 做点什么 .. 在你的情况下 Ajax 请求 返回假 });
      1234563
    • Ajax 请求中也缺少 url

    尝试使用 Jquery 表单插件,它让生活更轻松http://malsup.com/jquery/form/

    【讨论】:

    • 我添加了 return false;在单击功能中..现在当我单击提交按钮时,它会正确提醒 str.. 就像我的警报值是 _method=POST&name1=value1&name2=value2 但是在下一个保存的数据警报中,它只显示 _method=POST 请建议我..
    • 你能不能也为 /submit 发布 cakephp 动作
    • +1 建议使用 jQuery 表单插件 - 它确实让事情变得更容易
    • 绝对 +1 推荐插件。
    【解决方案2】:

    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
    

    更新后:

    我不确定,但是在您的 submit.php 文件中,您不应该创建一个控制器实例并显式调用提交函数吗?

    【讨论】:

      【解决方案3】:

      尝试在 click 函数中添加 e ,然后在第一行添加 e.preventDefault();以防止发生默认操作。

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

      【讨论】:

        【解决方案4】:

        如果您想使用 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(); 
        ?>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-12-23
          • 1970-01-01
          相关资源
          最近更新 更多