最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - I want to hide the submit button based on a PHPcondition through javascript - Stack Overflow

programmeradmin3浏览0评论
<?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??

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

Share Improve this question asked Aug 13, 2015 at 9:29 iaaphpd13iaaphpd13 291 gold badge2 silver badges9 bronze badges 3
  • Why don't you rather conditionally print or not print the tag for button? – Sami Kuhmonen 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. – Dal Hundal 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 es in action and work fine. – anuj arora Commented Aug 13, 2015 at 9:37
Add a ment  | 

8 Answers 8

Reset to default 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

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.

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

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

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

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

It is not none it's :

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

评论列表(0)

  1. 暂无评论