检索通过cakePHP中的JQuery Form Plugin提交的数据

时间:2009-06-23 10:04:13

标签: jquery cakephp

我正在使用JQuery的Form Plugin。

如何在cakephp控制器中获取发布值?

我的代码是这样的:

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

<script>
  $(document).ready(function(){
    var options = { 
      beforeSubmit:  showRequest,  // pre-submit callback 
      success:       showResponse,  // post-submit callback 
      url: "http://localhost/cake_1.2.1.8004/index.php/results/submit1",
      type: 'POST',
      resetForm: true        // reset the form after successful submit 
    }; 
    $('#ResultSubmit1Form').submit(function() { 
      $(this).ajaxSubmit(options); 
      return false; 
    }); 
  });//ready 

  // pre-submit callback 
  function showRequest(formData, jqForm, options) { 
    var queryString = $.param(formData); 
    alert('About to submit: \n\n' + queryString); 
    $.ajax({
      type: "POST",
      url: "http://localhost/cake_1.2.1.8004/index.php/results/submit1",
      data: "str="+queryString,
      success: function(msg){
        alert( "Data Saved: " + msg);
      }
    }); 
    return true; 
  } 

  // post-submit callback 
  function showResponse(responseText, statusText)  { 
    alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
      '\n\nThe output div should have already been updated with the responseText.'); 
  }

</script>

在我的CakePHP控制器中:

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

它仅显示_method=POST而不是_method=POST&name=x&age=22

但如果我使用$_POST['Name'];(其中Name是输入field1的name属性),它会显示我在输入字段1中输入的x。

我如何得到我想要的东西?

1 个答案:

答案 0 :(得分:0)

您可以通过访问$ this-&gt; params ['form']在控制器中取回您的值。

所以对于'姓名'字段,它会进入$ this-&gt; params ['form'] ['Name']。

插件仍然以POST发送数据,唯一的区别是它被ajaxed。 :)