Mani
Mani

Reputation: 43

How to call the php function using jquery ajax

I am using the following script

<script>
    $(document).ready(function(){
        $("#view").click(function(){
            var requestId = $("#hdnRequestId").val();

            $.ajax({
                type: "POST",
                url: "enquiryProcess.php",
                data: requestId,
                cache: false,
                success: function(data){
                    console.log(data);
                }
            });

            return false;
        });
    });

My controller function is

<?php
  include('enquiry_function.php');
  $functionObj=new Enquiry();
  if(isset($_POST['requestId']))
  {
    $qt_request_id=$_POST['requestId'];
    $responce=$functionObj->view_enquiry_request($qt_request_id);
    echo json_encode($responce);
  }
?>

And my model function is

class Enquiry
{
      public function view_enquiry_request($qt_request_id)
    {
        $query=mysql_query("SELECT * FROM quote_request WHERE qt_request_id='$qt_request_id'");
        $result=mysql_fetch_assoc($query);
        return $result;
    }
  } 

I did not get any error.But result in console message is empty.How to get the result from php in jquery ajax.please help me.

Upvotes: 0

Views: 45

Answers (1)

Saji
Saji

Reputation: 1394

Please change

var requestId = $("#hdnRequestId").val();

                    $.ajax({
                        type: "POST"
                        , url: "enquiryProcess.php"
                        , data: {"requestId":requestId}
                        , cache: false
                        , success: function (data) {
                            console.log(data);
                        }
                    });

Pass data as PlainObject or String or Array. See jQuery documentation here http://api.jquery.com/jquery.ajax/

Upvotes: 1

Related Questions