How do I call a jQuery function from PHP?
<?php
if ($error == 1) {
?>
<script type="text/javascript">
error('1');
</script>
<?
}
?>
It doesn´t work.
How do I call a jQuery function from PHP?
<?php
if ($error == 1) {
?>
<script type="text/javascript">
error('1');
</script>
<?
}
?>
It doesn´t work.
The script tags cannot be injected directly into php tags.... But if you echo it, the script will work.... The following code works :
<?php
if( some condition ){
echo "<script>alert('Hello World');</script>";
}
?>
In case of using jQuery, you have to wrap the code in:
jQuery(function(){
//Your Code
});
and then echo it....
It depends on where you place this code fragment within the HTML output.
One of these solutions, I think, should work:
<?php
if ($error == 1) {
?>
<script type="text/javascript">
$(function() {
error('1');
})
</script>
<?
}
?>
<?php
if ($error == 1) {
?>
<script type="text/javascript">
$(document).ready(
function() {
error('1');
}
)
</script>
<?
}
?>
<?php
if(this == that) {
//do something in php
} else {
//do something with jquery (i.e. fade something in/out
}
?>