I can do that? Im trying to call a javascript function after conditional php "if"
<script type="text\javascript">
functioncall = (function(id){
alert(id);
});
</script>
<?php
if(isset($_get['id']){
echo "<script>functioncall({$_get['id']});</script>";
}
?>
Use $_GET instead of $_get in php condition, I have corrected some of the errors like script tag script type, and PHP $_GET
<script type="text/javascript">
functioncall = (function(id){
alert(id);
});
</script>
<?php
if(isset($_GET['id'])){
echo "<script>functioncall('{$_GET['id']}');</script>";
}
?>
Or just using JS ?
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if(pair[0] == variable){return pair[1];}
}
return(false);
}
And do like...
var id = getQueryVariable('id');
if(id) {
alert(id);
}
Credit : http://css-tricks.com/snippets/javascript/get-url-variables/
As Quentin said you are exposed to XSS attacks. Don't ever put anything into your page from GET variables (url variables) - 'id' in your case.
Anyways, if you want to call a function based on a php condition, you can do something like this:
<script type="text\javascript">
functioncall = (function(id){
alert(id);
});
</script>
<?php
if(condition-goes-here){
?>
<script type="text/javascript">
functioncall(<?php echo 'Put in your php data' ?>);
</script>
<?php } ?>
I haven't checked it, hope it should work. If your condition returns false, your function call will not be put into your page.
It's not a good practice though.