Reputation: 447
I have a form to insert user. I use POST method and stored procedure. The ReturnStatus
and ReturnMessage
will be automatically called based on the data inserted.
I encountered some error as the ajax return null
value before the data is inserted. But when the form is filled and submitted its work completely fine and managed to display the result of the successful operation.
This is my code:
php
if(isset($_POST['submit']))
{
$UserNm=$_POST["UserNm"];
$UserId=$_POST["UserId"];
$UserPwd=$_POST["UserPwd"];
$stmt = odbc_exec(
$conn,
"CALL UserInsert (
'$UserNm',
'$UserId',
'$UserPwd',)"
);
if (!$stmt) {
"Error : " . odbc_errormsg();
}
if ($stmt) {
if (odbc_fetch_row($stmt)) {
$ReturnStatus=odbc_result($stmt,'ReturnStatus');
$ReturnMessage=odbc_result($stmt,'ReturnMessage');
}
if(isset($ReturnStatus) && $ReturnStatus==1) {
$ReturnMessage=odbc_result($stmt,'ReturnMessage');
}
}
}
echo json_encode($ReturnMessage);
?>
script
<script>
$.ajax({
url: "insert_sp.php",
dataType: "json",
success: function(data){
alert(data.test);
}
});
</script>
Please help me. Thank you :)
Upvotes: 0
Views: 2167
Reputation: 1637
Check, if your $ReturnMessage
Object is UTF-8 encoded, since json_encode only handles UTF-8 encoded data.
You can test the encoding with http://php.net/manual/de/function.mb-detect-encoding.php and change it with http://php.net/manual/de/function.utf8-encode.php
@see: http://php.net/manual/en/function.json-encode.php:
All string data must be UTF-8 encoded.
Add this line right before your json_encode
line:
$ReturnMessage = utf8_encode ( $ReturnMessage );
Upvotes: 1
Reputation: 8308
you are checking for POST request type:
if(isset($_POST['submit']))
while your ajax send a GET
request, read more about how to POST using ajax and jquery.
<script>
$.ajax({
url: "insert_sp.php",
method: "POST",
data: {submit: 'true'},
success: function(response) {
// while you are expecting a json response
// so you will need to decode it .
var data = JSON && JSON.parse(response) || $.parseJSON(response);
alert(data);
}
});
</script>
Upvotes: 1