2

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();  
 ?>
1
  • Look into JSON and get your PHP file to return a JSON object containing the two values, that's probably the best way to do it. Commented Jul 12, 2014 at 16:35

4 Answers 4

8

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();   
?>
Sign up to request clarification or add additional context in comments.

Comments

3

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);
}

Comments

2
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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.