简体   繁体   中英

Receive multiple value from php file via ajax call

Below is my ajax call code. I want to send one data in .php file via ajax call and want to get two values from .php file. This two values I want to set in different 'input' tags whose id are 'course_name' and 'course_credit'.

Here my ajax call return correct value(real value from DB table) of 'course_name' input tag.
But 'MY PROBLEM IS' the value of input tag whose id is 'course_credit' shows 'success'. How can I get the correct value(real value from DB table) of id 'course_credit' ?

I have a 'select' tag which id is 'c_select'

HTML:

<input type="text" name="course_name" id="course_name" value=""/>
<input type="text" name="course_credit" id="course_credit" value=""/>

AJAX :

$('#c_select').change(function(){
    $.ajax({
        type:'post',
        url:'get_course_info_db.php',
        data: 'c_id='+ $(this).val(),                 
        success: function(reply_data1,reply_data2){
            $('#course_name').val(reply_data1);
            $('#course_credit').val(reply_data2);
        }
    }); 
});

get_course_info_db.php

<?php 
       include('db_connection.php'); 
       $c_id = $_POST['c_id'];
       $result = mysql_query("SELECT * FROM course WHERE c_id = '$c_id'"); 
       $all_course_data = mysql_fetch_array($result);
       $c_name = $all_course_data['c_name'];
       $c_credit = $all_course_data['c_credit']; 
       echo $c_name,$c_credit;
       exit();  
 ?>

AJAX code:-

$('#c_select').change(function(){
    $.ajax({
        type:'post',
        url:'get_course_info_db.php',
        data: 'c_id='+ $(this).val(),                 
        success: function(value){
            var data = value.split(",");
            $('#course_name').val(data[0]);
            $('#course_credit').val(data[1]);
        }
    }); 
  });

PHP code:-

<?php 
     include('db_connection.php'); 
     $c_id = $_POST['c_id'];
     $result = mysql_query("SELECT * FROM course WHERE c_id = '$c_id'"); 
     $all_course_data = mysql_fetch_array($result);
     $c_name = $all_course_data['c_name'];
     $c_credit = $all_course_data['c_credit']; 
     echo $c_name.",".$c_credit;
     exit();   
?>

The success callback is Function( PlainObject data, String textStatus, jqXHR jqXHR ); http://api.jquery.com/jQuery.ajax/

php:

$data = array(
    'name' => $c_name,
    'credit' => $c_credit,
);
echo json_encode($data);

javascript:

success: function(data) {
    var result = $.parseJSON(data);
    $('#course_name').val(result.name);
    $('#course_credit').val(result.credit);
}
success: function(reply_data1,reply_data2){
    $('#course_name').val(reply_data1);
    $('#course_credit').val(reply_data2);
}

second arguement is the status of http request, you have to encode the answer, i suggest you JSON in your php

$c_credit = $all_course_data['c_credit']; 
echo json_encode(array('name' => $c_name,'credit' => $c_credit));
exit(); 

and in your javascript

 success: function(response,status){
     var datas = JSON.parse(response);
     $('#course_name').val(datas.name);
     $('#course_credit').val(data.credit);
 }

this is not tested, but this is the way to do it

I'd suggest using JSON to encode the data you fetch from the database.

Try changing your ajax call as follows:

$('#c_select').change(function(){
    $.ajax({
        type:'post',
        url:'get_course_info_db.php',
        data: 'c_id='+ $(this).val(),
        dataType: 'json', // jQuery will expect JSON and decode it for you
        success: function(reply_data){
            $('#course_name').val(reply_data['c_name']);
            $('#course_credit').val(reply_data['c_credit']);
        }
    }); 
});

And your PHP as follows:

include('db_connection.php'); 
// Escape your input to prevent SQL injection!
$c_id = mysql_real_escape_string($_POST['c_id']); 
$result = mysql_query("SELECT * FROM course WHERE c_id = '$c_id'"); 
$all_course_data = mysql_fetch_array($result);
echo json_encode($all_course_data);
exit();

I haven't tested this but I imagine it'd work for you.

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