简体   繁体   中英

Form submission in cakephp using JQuery

I have created the Form as like

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

In my JQuery code, i have written like,

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

Edit: Added Url to the 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 

In my Results 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;
   // echo $this->params['form']['str'];
  }

}

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

Please suggest me.. Any suggestions?

Few issues:

  • You are sending a Ajax Request,when the Submit button is clicked, but not stopping the click event with something like, so it will in any case redirect to the /submit page
$(".submit").click(function (){
           // do something .. in your case Ajax request
           return false
        });
  • So now when the form submits, the Ajax request gets sent but you are redirected to /submit page anyway, which I think has populated the GET array instead of POST.

  • Also the url is missing in the Ajax request

Try using the Jquery Form Plugin, its makes life a lot easier http://malsup.com/jquery/form/

There are URL missing in ajax request. Should be:

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

After update:

I'm not sure, but in your submission.php file shouldn't your create an instance of controler and call the submit function explicitly?

try using adding e inside your click function and then on the first line add e.preventDefault(); to prevent the default action from occurring.

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

If you want to submit your form with ajax. Cakephp have its oven Mechanism for that. Check below code

<?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(); 
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM