Reputation: 29
<?php
if(mysql_num_rows($result5)>0)
{
echo 'condition matched';
// the above echo is just for testing
?>
<script type='text/javascript'>
document.getElementById("submitbtn").style.display = "none";
</script>
<?php
}
?>
here I am evaluating an if clause, if the condition is true, I want to hide the submit button of the form.
<form action='xyz.php'>
<input type='text'>
<input type='text'>
<input type='text'>
<input type='submit' id='submitbtn' name='save' value='SAVE' class='form_btn' >
</form>
The condition is evaluating to true, but the submit button is still visible. How do I achieve this??
Upvotes: 1
Views: 26867
Reputation: 16
<?php
if(mysql_num_rows($result5)>0)
{
echo'<div style='visibility:hidden' id="del">';
echo'<script>
$(document).ready(function(e) {
$("#del").css("visibility", "visible");
});
</script>';
}
?>
Upvotes: 0
Reputation: 1189
Posting the answer because all others are just disabling the button. It will still be visible. So to hide the button, use:
window.onload = function(){
document.getElementById("<id of your submit button>").style.display = "none";
}
inside the if(condition=true)
OR
<input type='submit' name="sbmt" id="id_1"<?php if(condition=true) { ?> style="display: none" <?php } ?> />
but since your question has asked exceptionally for "javascript" i will go one step further and write
<input type='submit' name="sbmt" id="id_1"
<?php if(condition=true) { ?>
<script type='text.javascript'>
document.getElementById("id_1").style.display = "none";
</script>
<?php } ?>
/>
Upvotes: 1
Reputation: 774
I Think you added your php codes before that form
loads!
So you need window.onload
OR Replace your php codes to after form
<?php
if(mysql_num_rows($result5)>0)
{
echo 'condition matched';
?>
<script type='text/javascript'>
window.onload = function(){
document.getElementById("submitbtn").style.display = "none";
}
</script>
<?php
}
?>
Upvotes: 0
Reputation: 468
try using this way....
<input type='submit' <?php if($result5->num_rows>0) {?> disabled="disabled" <?php } ?> id='submitbtn' name='save' value='SAVE' class='form_btn' >
may be it will help
Upvotes: 7
Reputation: 105
I had tied your code,and these worked ok, so I think must have other problems to affected your js code, such as a same name Id of submitbtn
, or a js error.
Upvotes: 3
Reputation: 2525
If you want to use js try this :
<?php
if(mysql_num_rows($result5)>0)
{
echo 'condition matched';
// the above echo is just for testing
?>
<script type='text/javascript'>
$(document).ready(function(){
document.getElementById("submitbtn").style.display = "none";
});
</script>
<?php
}
?>
You can achieve this by using php too.
<input type='submit' id='submitbtn' name='save' value='SAVE' class='form_btn' <?php if(mysql_num_rows($result5)>0) echo 'style="display:none"';?>>
Upvotes: 1
Reputation: 3488
Try as below : jQuery must be there on your page.
On calling the statement on page ready will solve the problem.
<?php
if(mysql_num_rows($result5)>0)
{
echo 'condition matched';
// the above echo is just for testing
?>
<script type='text/javascript'>
$( document ).ready(function() {
document.getElementById("submitbtn").style.display = "none";
});
</script>
<?php
}
?>
Upvotes: 0
Reputation: 2729
It is not none
it's :
<script type='text/javascript'>
document.getElementById("submitbtn").style.visibility = "hidden";
</script>
Upvotes: -2