iaaphpd13
iaaphpd13

Reputation: 29

I want to hide the submit button based on a PHPcondition through javascript

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

Answers (8)

Loukoulele
Loukoulele

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

Ishan Srivastava
Ishan Srivastava

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

Nabi
Nabi

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

Sahil Manchal
Sahil Manchal

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

gangzi
gangzi

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

Happy Coding
Happy Coding

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

AnkiiG
AnkiiG

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

Hearner
Hearner

Reputation: 2729

It is not none it's :

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

Upvotes: -2

Related Questions