We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

ajax post to controller

//this is js file $(document).ready(function(){ $('#doctname').on('input', function () { var Value = $(this).val(); if(Value != '') { alert(Value); $.ajax({ url:"trf/working", method:"POST", data:{intoDoc:Value}, success:function(data) { $('#dhospital').value('hi'); } }); } }); });

//This is controller public function workingAction() {

  $Doct = $this->request->getPost("intoDoc");
  $doctors = Doctors::findFirst(["d_name = $Doct",]);
  foreach($doctors as $doctor){
      $this->flash->notice($doctor->d_code);
  }
  }

  It is not working ajax 

I don't think there's an input event for dom objects.

$(document).ready(function(){
    $('#doctname').on('change', function () {
        var Value = $(this).val();
        if(Value != '') {
            alert(Value);
            $.ajax({
                url:"trf/working",
                method:"POST",
                data:{ intoDoc : Value },
                success : function(data) {
                    $('#dhospital').value('hi');
                }
            });
        }
    });
});
edited Dec '16

I don't think there's an input event for dom objects.

$(document).ready(function(){
  $('#doctname').on('change', function () {
      var Value = $(this).val();
      if(Value != '') {
          alert(Value);
          $.ajax({
              url:"trf/working",
              method:"POST",
              data:{ intoDoc : Value },
              success : function(data) {
                  $('#dhospital').value('hi');
              }
          });
      }
  });
});

https://stackoverflow.com/questions/15160395/pre-fill-text-fields-in-form-based-on-value-selected-from-dynamic-drop-down-bo?answertab=active#tab-top

Please visit this link above, I want this same in phalcon php and thanks for reply

And what exactly doesn't work ? Code above should work fine.

And what exactly doesn't work ? Code above should work fine.

when i posting using ajax, in controller isPost, isAjax and getPost are not working, please help to resolve it.

This forum is dedicated for Phalcon, so issues related to other web-development topics won't be answered in detail, sorry.

The code you posted your the first comment should work if you substitute the input event name with change or keyup or whichever suits you. If you have problems with basic development concepts, please educate yoursel ffirst, there are literally tons of tutorials on the web, both text and audio.



43.9k

Hi,

first look at your webdeveloper console to see if form data are well sent and well formatted. Also if you do not have any javascript error ...

Hi,

first look at your webdeveloper console to see if form data are well sent and well formatted. Also if you do not have any javascript error ...

working fine in console



43.9k

so you confirm that intoDoc var is posted by the ajax script and that

$Doct = $this->request->getPost("intoDoc");

is empty ??.

Don't you have any error (see webserver log) regarding $request object ?



43.9k

you didn't answer to my question :


so you confirm that intoDoc var is posted by the ajax script and that

$Doct = $this->request->getPost("intoDoc");

is empty ??.

Don't you have any error (see webserver log) regarding $request object ?


in your view file: I don't see any opening <form> tag. And why don't you use phalcon tag service for displaying your form : https://docs.phalcon.io/en/latest/reference/tags.html#helpers-to-generate-form-elements

be careful in DOm object id naming: id="#dcontact is not good (I thin so ...)

edited Dec '16

you didn't answer to my question :


so you confirm that intoDoc var is posted by the ajax script and that

$Doct = $this->request->getPost("intoDoc");

is empty ??.

Don't you have any error (see webserver log) regarding $request object ?


in your view file: I don't see any opening <form> tag. And why don't you use phalcon tag service for displaying your form : https://docs.phalcon.io/en/latest/reference/tags.html#helpers-to-generate-form-elements

be careful in DOm object id naming: id="#dcontact is not good (I thin so ...)

Sorry for my answer, there are no errors but the variable is empty Sir, i am not expert in phalcon or in jquery so please kindly support me.

You definitely don't want to put a hasmark before an ID in html. id="#foobar" is invalid.

Please take an online course in web development before jumping into frameworks like Phalcon.



43.9k

ok.

For sending all your form data you should use jquery serialize method : https://api.jquery.com/serialize/ (see eaxample)

Are you sure there is XmlHttpRequest header in phalcon app ? If not then phalcon can't know if it was made with ajax or not.

edited Jan '17

Hello i have a working ajax that works fine with my controller, in case you need t check if ours is working try$this-:request->IsAjax();


(function(){

    var $forms  = $("form[role='form']");

    $forms.each(function(){

        var $form = $(this);

        $form.submit(function(){

            var $this   = $(this),
                action  = $this.attr("action"),
                method  = $this.attr("method"),
                inputs  = $this.find("input:not(:file):not(:submit) , textarea"),
                files   = $this.find("input:file"),            
                content = new FormData( $this );

                //  Loop & append inputs
                for( var i = 0;  i < inputs.length ; ++i )
                {
                    content.append( $(inputs[i]).attr("name") , $(inputs[i]).val() ); // Add all fields automatic
                }

                //  Loop & append files with file data
                if( files.length  ) { 
                    for( var i = 0;  i < files.length ; ++i )
                    {
                        if(files[i].files[i] != undefined)
                        {
                            content.append(files.eq(i).attr("name"), files[i].files[i], files[i].files[i].name );// add files if exits
                        }
                    }
                }

                //  Submit data
                $.ajax({
                    url:  action,           //  Action  ( PHP SCRIPT )
                    type: method,           //  Method
                    data: content,          //  Data Created
                    processData: false,     //  Tell jQuery not to process the data
                    contentType: false,     //  Tell jQuery not to set contentType
                    dataType: "json",       //  Accept JSON response
                    cache: false,           //  Disale Cashing
                    success: function( response )
                    {
                        //  use the response
                    }
                });

            return false;
        });

    });

})();