0

i need one help that is returning data just after submit inside the database.I am explaining my code below.

addCourse.php:

<?php
$course_name=stripslashes($_POST['course_name']);
$course_short_name=stripslashes($_POST['course_short_name']);
$semester=stripslashes($_POST['semester']);
$con = mysql_connect('localhost', 'root', '******');
mysql_select_db('go_fasto', $con);
$qry ='INSERT INTO db_course (course_name,short_name,semester) values ("' . $course_name . '","' . $course_short_name . '","' . $semester . '")';
$qry_res = mysql_query($qry);
if ($qry_res) {
        echo "Course has added successfully";
    } else {
        echo "course could not added ";
    }
?>

This file is called using ajax and all returning value will appear in ajax's success function.Here i need to return both the submitted data and success message to ajax's success function.Please help me to resolve this issue.

3
  • Return a json array with a response and data array contained within Commented Sep 29, 2015 at 5:00
  • Can you please edit your answer ? Commented Sep 29, 2015 at 5:02
  • See @raveenanigam's answer, similar to that Commented Sep 29, 2015 at 5:06

3 Answers 3

1

You can use json_encode to achieve the expected output.

    $result['course_name'] = $course_name;
    $result['course_short_name'] = $course_short_name;
    $result['semester'] = $semester;
    if ($qry_res) {
    $result['message'] = 'Course has added successfully';
    } else {
        $result['message'] = 'course could not added';
    }

echo json_encode($result);

You can retrieve all the submitted data along with the message.

Sign up to request clarification or add additional context in comments.

6 Comments

@ raveenanigam : Yes, your answer gave the solution but here one small problem,i need to return data from database directly because i will append those data on view page for display purpose so the database column name and here the variable are diffrent.
@satya you just want to return the single record which is currently inserted
@satya...$result['course_name'], $result['short_name'],$result['semester'] no issues...just change the indexes
because i also need the id of each column that is the reason why i want return value from database.
append those id's in $result array
|
0

use below code in you ajax script:

$.ajax({
    type: 'POST',
    url: ajaxURL,
    data: datastring,
    dataType: 'json',
    success: function(data){
        console.log(data);
        alert(data.msg)
    }
});

and PHP file like:

<?php
$course_name=stripslashes($_POST['course_name']);
$course_short_name=stripslashes($_POST['course_short_name']);
$semester=stripslashes($_POST['semester']);
$con = mysql_connect('localhost', 'root', 'Oditek123@');
mysql_select_db('go_fasto', $con);
$qry ='INSERT INTO db_course (course_name,short_name,semester) values ("' . $course_name . '","' . $course_short_name . '","' . $semester . '")';
$qry_res = mysql_query($qry);
$result = $_POST;
if ($qry_res) {
        $result['msg'] = "Course has added successfully";
    } else {
         $result['msg'] =  "course could not added ";
    }
return $result;
?>

2 Comments

I tried your answer but in console i did not get any value.It is not returning any value.
use the echo json_encode($result); instead of return $result;
0

try using this:

 $result['postValue'] = $_POST;
    if ($qry_res) {
            $result['msg'] = "Course has added successfully";
        } else {
             $result['msg'] =  "course could not added ";
        }
    echo  json_encode($result);

////////////////////in script//////////////

$.ajax({
    type: 'POST',
    url: ajaxURL,
    data: datastring,
    success: function(data){
        var data =JSON.parse(data);
        console.log(data.postValue);
        alert(data.msg)
    }
});

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.