Reputation: 27
I need to disable a button if there are no records returned in a db query. What i have is this:
$cnt= mysql_num_rows($qry_resultcnt);
if ($ttrec >=1)
{
echo "msgbox";
document.getElementById("btn1").disabled = true;//disable insert button??
}
but this returns a error message
Parse error: syntax error, unexpected '='
Upvotes: 1
Views: 79
Reputation: 10454
You're mixing php with javascript, which is not allowed. Try either of the two options below to escape your javascript from php.
$cnt= mysql_num_rows($qry_resultcnt);
if ($ttrec >=1) {
echo "msgbox";
echo '<script>document.getElementById("btn1").disabled = true;</script>';
}
Or....
$cnt= mysql_num_rows($qry_resultcnt);
if ($ttrec >=1) {
echo "msgbox";
?>
<script>
document.getElementById("btn1").disabled = true;
</script>
<?php
}
Although for both options, I would recommend wrapping your JS in a self invoking function so that you are certainly firing it, like so...
echo '<script> (function() { document.getElementById("btn1").disabled = true; })(); </script>';
OR....
<script>
(function() {
document.getElementById("btn1").disabled = true;
})();
</script>
Upvotes: 1
Reputation: 944320
You need to output suitable client side code. You can't just use client side JS directly in your PHP.
echo '<script>document.getElementById("btn1").disabled = true;</script>';
But you would probably be better off just setting the attribute:
echo '<button etc etc disabled>etc etc</button>';
Upvotes: 1