简体   繁体   English

单击时禁用表格或提交按钮,然后提交表单

[英]Disable a table or a submit button when clicked and submit the form

I am creating a dynamic table with a query on my BD, the problem is that I want to disable a button on a table every time I make a submit, I would like to get the form submitted without page load so that the disabled button is visible. 我正在用BD上的查询创建一个动态表,问题是我想在每次提交时禁用表上的按钮,我想在没有页面加载的情况下提交表单,因此禁用的按钮是可见。

<?php
            $theCounter=0;
            while($row = mysql_fetch_object($result))
            {

        ?>

            <form id="id" action="table.php" method="post">
            <input type="hidden" value="<?php  echo $row->id_table;  ?>" name="idUser">
                <tr>
                    <td><?php  echo $row->id_userS;  ?></td>
                    <td><?php  echo $row->username_s;  ?></td>
                    <td><?php  echo $row->num_people;     ?></td>
                    <td><?php  echo $row->start_time;  ?></td>
                    <td> 
                        <input type="submit" name="delete_check" value="Eliminar"> 
                    </td>
                    <td> 
                        <input type="submit" name="push_check" value="Enviar Push">
                    </td>
              </form>
        <?php
            $theCounter++;
            }

        ?>

The button I want to disable or hide its the one named "push_check". 我要禁用或隐藏其名为“ push_check”的按钮。 It doesn't matter if I disable the button or hide the button 禁用按钮或隐藏按钮都没关系

Thanks in advance! 提前致谢!

PHP:- PHP:-

<?php
        $theCounter=0;
        while($row = mysql_fetch_object($result))
        {

Echo "

        <form id='id' action='table.php' method='post'>
        <input type='hidden' value='$row->id_table;' name='idUser'>
            <tr>
                <td>$row->id_userS</td>
                <td>$row->username_s</td>
                <td>$row->num_people</td>
                <td>$row->start_time</td>
                <td> 
                    <input type='submit' id='del$thecounter' name='delete_check' value='Eliminar' onclick='hide(del$thecounter)'> 
                </td>
                <td> 
                    <input type='submit' id='push$thecounter' name='push_check' value='Enviar Push' onclick='hide(push$thecounter)'>
                </td>
          </form>
";
        $theCounter++;
        }
?>

Now use Javascript:- 现在使用Javascript:

function hide(a)
{
     document.getElementById(a).style.visibility="hidden";
}

If you want the button to be disabled on all the page loads after the first submit then you need to store some flag in the session which will indicate that the button has already been clicked. 如果要在首次提交后禁用所有页面加载上的按钮,则需要在会话中存储一些标志,以表明该按钮已被单击。 And you need to check for that flag on every page load. 并且您需要在每次页面加载时检查该标志。

I'm not sure if I understood everything you want to achive, but here is what I made out of your question: 我不确定我是否了解您想要达到的所有目标,但这是我根据您的问题做出的:

<script>

    document.forms[0].onsubmit = function(e){

        e.preventDefault();  // at the beginning stop page from being reloaded by the script
        var form = e.currentTarget; // get form
        form.push_check.disabled = true; // set submit-button disabled

        if(form.idUser.value){ // send data if there is data to send
           document.forms[0].onsubmit = null; // set event listener null otherwise onsubmit is a forever-loop
           document.forms[0].submit();  // send finally data to the php-script
        }
        else{
           form.mysubmit.disabled = false; // there was no data to send
        }
    }
</script>

You can put this little script after or within the body-tag. 您可以将此小脚本放在正文标签的后面或内部。 It should work in each browser. 它应该在每个浏览器中都可以工作。

One more method to prevent loading the page is using target 防止加载页面的另一种方法是使用target

<form action='' method='POST' target='upload_target' onsubmit='hide(dynamicid)'>

fields between form.....
<input type='submit' id='dynamicid'/>
<iframe id='upload_target' name='upload_target' src='#' style='width:0;height:0;border:0px solid #fff;'></iframe>
</form>

Hi i manage to do it by this: 嗨,我设法做到这一点:

<?php
                        $push_button = $row->time_push;
                        if($push_button==0)
                        {
                      ?>
                    <td> 
                        <input type="submit" id='push$thecounter' name="push_check" value="Enviar Push">
                    </td>
                    <?php
                        }
                        ?>

Since i've already making a query to my DB, and the button is making a change, im checking if that change its already made, so if not, im displaying the button! 由于我已经对我的数据库进行了查询,并且按钮正在进行更改,因此我会检查是否已进行了更改,因此,如果没有,我会显示按钮! thanks a lot for all your comments! 非常感谢您的所有评论!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM