John Davidson

php - JavaScript function doesn't work as per JSON response

0 comments
Message:


I am encoding my returned response through JSON and trying to make it work as per the JSON Response's values.


Assume below values are what I am returning.


if ($upload = 1){
$response['status'] = 1;
$response['message'] = 'SUCCESS';
$response['success'] = 'false';
}else{
$response['status'] = 3;
$response['message'] = 'CATEGORY ASSOCIATED';
$response['success'] = 'false';
}

header('Content-type: application/json');
echo json_encode($response);

I will be retrieving it to my form and handle the action through the encoded responses.


 $('#form').on('submit', function(e){
e.preventDefault();
var id = $('#id').val();
$.ajax({
type: "POST",
url: 'edit.php',
data: {cid:id},
success: function(response){
if(response.status = 1){
console.log('THE STATUS IS 1');
}
else if(response.status = 3){
console.log('THE STATUS IS 3');
}
}
});
});

The issue I am facing is I am getting both responses on browser's network tab. console prints status 1 but not status 3. Where am I making the mistake?



My Answer:
't exist as PHP is a server-side language and JavaScript is a client-side language. However, you can use PHP to generate JavaScript code that can be executed on the client-side. Here is an example of how you can create a JavaScript function using PHP:

php
echo '';
?>


This PHP code will output a JavaScript function that logs "Hello, World!" to the console when called. You can then include this PHP code in your HTML file to create the JavaScript function.

Rate this post

5 of 5 based on 1734 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us