简体   繁体   中英

ajax call must get response from method in ajax.php?

This is my ajax call.

   $(document).on('click','#Quote_create_value',function(){
        $.ajax({
            type : 'GET',
            url : '../../../protected/config/ajax.php',
            success : function(response){
                $("#Quote_template_value").html(response);
            }
        });
   });

I have many methods in ajax.php. Each and every method throws some response.

    <?php
      function respose()
      {
        $query = "select * from quote where template IS NOT NULL";
        $result = mysql_query($query, $con);
        while ($row = mysql_fetch_assoc($result)) {
            echo '<option value="'.$row['template'].'">' . $row['template'] . '</option>';
        }

        $query1 = "select * from template";
        $data = mysql_query($query1,$con);
        while ($row = mysql_fetch_assoc($data)) {
            echo json_encode($row);
        }
      }

      function result()
      {
      }

    ?>

But i want to get response from one method [ie. from response()].

How can this be done?

You could include a selector in the ajax request data. Like this for example:

   $(document).on('click','#Quote_create_value',function(){
        $.ajax({
            type : 'GET',
            url : '../../../protected/config/ajax.php',
            data: "function=result",
            success : function(response){
                $("#Quote_template_value").html(response);
            }
        });
   });

Then in your PHP code, a simple if-statement will check which one to output.

if(isset($_GET['function'])) {
    if($_GET['result'] == 'result') {
        // do result stuff
    } elseif($_GET['function'] == 'response') {
        // do response stuff
    }
}

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