i want check if variable inside php is have active value i test like this but not work
i have this var inside check.php
$status = 'active';
and i have this inside javascript file
$.ajax({
type: "POST",
url: "check.php",
success: function(check) {
var statusJS = '<?php echo $status; ?>';
if (check[statusJS] == "active" {
alert("status : active");
}}})
this full check.php file
<?php
$status = 'active';
$gpa = 'html data here';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
echo $gpa;
echo $status;
} else {
include("gp-e.php");
}
?>
it is possible to get variable value from php in javascript without echo it in php file ?
Try this one.
<?php
$status = 'active';
$gpa = 'html data here';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ($status == 'active') {
return print_r($status);
}
} else {
include("gp-e.php");
}
?>
$.ajax({
type:"POST",
url:"dt.php",
success:function(data){
//alert(data);
if (data.trim() == 'active')
{
alert("status:" + data);
}
else
{
alert("Status:Inactive");
}
}
});
No as per me,It is not possible to get variable value from php without echo in javascript.
But u can assign php value in one of input field and get its value in javascript.
Like below code.
<input type="hidden" name="status" id="status" value="<?php echo $status; ?>" />
Now to get that value in javascript ::
var status = $("#status").val();
you should echo
it in check.php
and create an array
$status = 'active';
$gpa = 'some string'
$data = $status . ',' . $gpa
echo $data
and in your javascript
$.ajax({
type: "POST",
url: "check.php",
success: function(data) {
var check = (new Function("return [" + data + "];")());
if (check[0] == "active") { // check[0] is $status in php
alert("status : active");
}}})
*if you want to echo
multiple values. you can create an object/array.
PHP
<?php
$status = 'active';
$gpa = 'html data here';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$response = [
'status' => $status,
'message' => $gpa
];
echo json_encode($response);
} else {
include("gp-e.php");
}
?>
JavaScript
$.ajax({
type: "POST",
url: "check.php",
dataType: "json",
success: function(data) {
if (data['status'] == "active" {
alert("status : active");
}
}
});
I think, you want to check if the $status = 'active'
and return the html data to your javascript?