Skip to main content
Topic solved
This topic has been marked as solved and requires no further attention.
Topic: Ajax post request(SOLVED BY Willvin) (Read 2862 times) previous topic - next topic

Ajax post request(SOLVED BY Willvin)

Good afternoon PHPRADERS, i am having issues with ajax post request. How do i send data to the specified URL. It doesn't seem to work after updating to this latest release. HELP!!!

Secondly, i cant seem to create custom functions in the sharedcontroller. How do i accomplish this in PhPraD. Thanks for the assistance.


Re: Ajax post request

Reply #2
Thanks @willvin for the response and interest. i want values from list-box 1 and list-box 2 to determine the value list-box3 should contain. I did this:
$('#ctrl-section').on('change', function(){
   var cname= $('#ctrl-class').val();
       var sname =$('#ctrl-section').val();
   $.ajax({
       url:'score/getnames.php',
       type:'post',
       data:{cname:cname,sname:sname},
       dataType:'json',
       success:function(response){
          
          //$('#ctrl-student_name').empty();
         
               $('#ctrl-student_name').html(response);
         
          
       }
   });
   
});
=============================================
//getnames.php file
$lookup_class=$_POST['cname'];
$lookup_section=$_POST[sname];
$db = $this->GetModel();
       $sqltext = "SELECT CONCAT(first_name,' ',middle_name,' ',surname) AS fullname FROM student WHERE class= ? AND section= ?";
       $queryparams = array($lookup_class,$lookup_section);
       $arr = $db->rawQuery($sqltext,$queryparams);
$flnames= array();
           foreach($arr as $fullnames){
               $studname= $fullnames['fullname'];
               $flnames[] = array("names"=> $studname);
             
          
          }
          echo json_encode($flnames);

view the image attached.
the output gives empty options.
Thanks once agian.

Re: Ajax post request

Reply #3
@ubbxst‍ this should populate that field for you, assuming your PHP code returns a JSON data and your ajax post request gets the data from it. Add this 👇 into your success function.
Code: [Select]
var $stdname= $("#ctrl-student_name");
$.each(JSON.parse(response), function() {
    $stdname.append($("<option />").val(this.flnames).text(this.flnames));
});
flnames should be each variable that holds the name in the JSON data.




Re: Ajax post request

Reply #4
Thanks @wilvin for your response but its not working. i tried another approach of sending the data through controller(score/getnames/cname/sname), but still not working. it cant retrieve data from the student table to populate the list.

Note: the controller is ScoreController(getnames is a method in here). the View is score/add.php(this is where the ajax request is written).

Re: Ajax post request

Reply #5
This will help other user that have the same problem with ajax.I know the root cause why it not working in newer version phprad.
Because of when we call ajax url,it not go to root path.
As in ubbxst code is
Code: [Select]
url:'score/getnames.php',
score folder is next from root path.
If this ajax is use in url = {root_path}/customer/add,Ajax not go to right url.It will call to
{root_path}/customer/add/score/getnames.php.  (the correct url is {root_path}/score/getnames.php)
So,to fix it.Your url must add ../ in your code. :-\
Code: [Select]
url:'../score/getnames.php',

Ajax will go to correct url and working again. 8)

Re: Ajax post request

Reply #6
Good morning @tin and thanks for your aid. I'll try that out now.

Re: Ajax post request

Reply #7
Good afternoon @tin that did not work as well.