Reputation: 11
I call a javascript query within php function to test if user wants to allow certain action. The value returned from javascript
is either string value 'true' of 'false'. If I test in php
to see if string, returns true, if I test is as $var=='true' it returns false. Please help with my apparently daft oversight.
I have tested it to ensure it numerous ways to see what value is returned, string, boolean, 1,0 integer, but indicates string
if (file_exists($target_file)) {
function prompt($prompt_msg){
echo("<script type='text/javascript'> var answer = confirm('".$prompt_msg."'); </script>");
$answer = "<script type='text/javascript'> document.write(answer); </script>";
return($answer);
}
//program
$prompt_msg = "Overwrite curent file?";
$name = prompt($prompt_msg);
}
//This returns 'true' or 'false' string value for php sake
if (is_string($name))
{echo "YES";} else
{echo "NO";}
//This always returns YES if I select OK (true) or Cancel (false) in above javascript
echo gettype($name);
//This returns string
echo $name;
/*****/ if($name == 'true') {
$uo = 1;
echo 'success';
} elseif($name == 'false') {
$uo = 0;
}
else {;
$uo = '??';
}
echo $uo;
***//If I replace this statement with if($name == true) whther the javascript returned string is true of false it always gives $uo = 1;
//Whether the javascript value is 'true' or 'false' is will always return ??
Php if query should assign 1 to $uo if 'true' else '0' if false
Upvotes: 1
Views: 559
Reputation: 1004
Your conditions will get executed in PHP not in javascript.
When converting to boolean, the following values are considered FALSE:
the boolean FALSE itself
the integer 0 (zero)
the float 0.0 (zero)
the empty string, and the string "0"
an array with zero elements
an object with zero member variables (PHP 4 only)
the special type NULL (including unset variables)
SimpleXML objects created from empty tags
Refer -
The if statement doesn't work for false in php
Check if boolean is true or false
Also note
var_dump(0==FALSE); //bool(true)
var_dump(0===FALSE); //bool(false)
Upvotes: 0
Reputation: 403
PHP is server side and the JavaScript is client side (not considering NodeJS).
That said when you call the page the PHP is run completely without waiting for the JavaScript you made, actually PHP only builds the webpage that contains the script and only when the browser receives this page that it will execute the JavaScript, but PHP has already finished all its execution.
In your example code what $answer
contains is the string <script type='text/javascript'> document.write(answer); </script>
and not the result of the execution of the JavaScript, because PHP can't execute a script like that.
If this PHP is to be executed on a browser context you need to change the JavaScript to recall the PHP url passing the value of the confirm dialogue in a query, and the PHP to check the passed query with something like parse_str($_SERVER['QUERY_STRING'], $query);
and only build the page when none is passed and to continue execution if it receives a true
.
@v-prince answer gives you the idea of how to do the client side.
Upvotes: 0
Reputation: 130
I hope this will give you some idea.. Good Luck
sample.html
<input type='button' class='test'>
<!-- make sure you have a jquery plugin and check the path of your jquery -->
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
$('.test').click(function(){
var answer = confirm('are you sure you want to click me?');
$.ajax({
url : 'yourPHP.php',
type : 'post',
data : { answerValue : answer },
success : function( result ){
// do something
}
});
});
});
</script>
Upvotes: 0