tags:

views:

389

answers:

1

Hi!

I am using JQuery's Form Plugin.

How can I get the posted value in the cakephp controller?

My code is like this:

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

And in my CakePHP controller:

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

it's displaying only _method=POST instead of _method=POST&name=x&age=22.

But if i used $_POST['Name']; (where Name is the name attribute of the input field1), it's displaying the x that I typed in the input field1.

How do I get what I want?

A: 

you can get back your values in your controller by accessing the $this->params['form'].

so for the 'Name' field, it would go in $this->params['form']['Name'].

the plugin still sends the data as POST, the only difference is it's been ajaxed. :)

zam3858
Ya But i need to post this value directly.Like instead of retriving the value $_POST['fom']['Name'] since i the controller wont know what are all fields are there in the page...The form may contain different fields in each form ...
Aruna
I dont understand what you're saying. all posted values will go to $this->params and properly named input fields will go to $this->data. that plugin does indeed sends a POST request from the browser to the controller and you would process the form.
zam3858
Aruna