1
<?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??

3
  • Why don't you rather conditionally print or not print the tag for button? Commented Aug 13, 2015 at 9:34
  • 2
    Firstly, you're probably including the script tag before the submit button. Secondly - if you want to hide the button based on a condition in PHP - you should hide the button with PHP. No reason to use JS to do it. Commented Aug 13, 2015 at 9:34
  • @DalHundal is right. You can do it with PHP or you have to right the JS at the end of page, so that after page is ready then JS comes in action and work fine. Commented Aug 13, 2015 at 9:37

8 Answers 8

7

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

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, doing it through the PHP itself rather than using JS is the sensible solution. If he wants it to not show (rather than be disabled) you could use style="display: none;" in the condition.
yes, javascript was'nt doing it because I was executing script before the submit button. Doing it directly through php on the input tag is a better option.Thanks
3

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.

Comments

1

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"';?>>

Comments

1

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 } ?>
     />

Comments

0

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
}
?>

Comments

0

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
}
?>

Comments

0
<?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>';
}
?>

1 Comment

Adding a small explanation of your solution may be a help for someone running into this in the future :D
-2

It is not none it's :

<script type='text/javascript'>
    document.getElementById("submitbtn").style.visibility = "hidden";
</script>

4 Comments

document.getElementById("submitbtn").style.display = "none"; Works just as well as that, not really a solution.
I just tried and it doesn't work with "none" but works with "hidden"
There's a difference between display and visibility. This isn't a solution to this problem.
yes @GeoffAtkins its working fine now. The problem was that I was loading script before the submit button had loaded. Both, executing the script after the submit button and doing it directly through PHP on the input tag work good.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.