【问题标题】:html button (onlick) not working when sending data to mysql table将数据发送到mysql表时html按钮(onclick)不起作用
【发布时间】:2016-04-23 20:20:33
【问题描述】:

所以我在 html 页面上有一个表单,可以从 mysql 表中检索数据。用户可以修改这些数据,或者删除整行。我有一个 html 按钮,它具有删除整行的功能,并且使用该按钮我有一个 javascript 函数,它显示一个确认弹出框,确认用户是否要删除它。如果他们单击是,则将其发送到执行删除的另一个 php 页面。问题是当我单击确认框时没有任何反应,它只是停留在同一页面上。这是我的 html 和 javascript 代码:

<form class="form-inline" method=GET action="envoieModifierAlbumAdmin.php">
    <table class="table table-striped">
        <tr>
            <td><button type="button" class="btn btn-danger" onclick="verif()">Supprimer Cet Album</button>
            </td><td></td><td></td>
        </tr>

        <tr>
            <td><label for="exampleInputName2">id: </label>
                <input readonly type="text" class="form-control" value="<?php echo "$donnees[id]"?>" id="exampleInputName2" name="id">
            </td><td></td><td></td>
        </tr>
        <tr>
            <td><label for="exampleInputName2">Nom d'album: </label>
            <input type="text" class="form-control" id="exampleInputName2" name="titre" value="<?php echo "$donnees[titre]"?>"></td>
            <td></td><td></td>
        </tr>
    </table>
</form>
<script>
    function verif(){
        var x;
        if(confirm("Vous êtes sûr que vous voulez supprimer ce compte?") == true){
            document.location="supprimerAlbumAdmin.php?id=$_GET['id']";
        }
        else
    }
</script>

唯一的问题是按钮,上表的其他部分只是展示用户正在修改的示例。提前致谢!

【问题讨论】:

  • 尝试使用 onClick 而不是 onclick 并删除函数中的 else 或添加 else 大括号,如 else{}
  • @ManinderpreetSingh 也不起作用。这可能与它是一个包含 javascript 的 .php 文件这一事实有关吗?
  • 打开你的控制台并检查究竟是什么错误...你可以在 php 中编写 js 这不是问题

标签: javascript php html mysql


【解决方案1】:

你的价值应该是这样的

value="<?php echo  $_GET['id'] ?>"

不是这样的

value="<?php echo "$donnees[id]"?>"

并将按钮类型更改为提交

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>


function verif(){
	alert("hi");
        var x;
        if(confirm("Vous êtes sûr que vous voulez supprimer ce compte?") == true){
            
			//window.location.replace("https://google.com");
			//exit();
          return true;
          
        }
        else
          {
            
            return false;
            
            }
        
    }
</script>
<form class="form-inline" method="GET" action="envoieModifierAlbumAdmin.php">
    <table class="table table-striped">


        <tr>
            <td><label for="exampleInputName2">id: </label>
                <input readonly type="text" class="form-control" value="" id="exampleInputName2" name="id">
            </td><td></td><td></td>
        </tr>
        <tr>
            <td><label for="exampleInputName2">Nom d'album: </label>
            <input type="text" class="form-control" id="exampleInputName2" name="titre" value=""></td>
            <td></td><td></td>
        </tr>
              <tr>
            <td><button type="submit" class="btn btn-danger" onclick="verif()">Supprimer Cet Album</button>
            </td><td></td><td></td>
        </tr>
    </table>
</form>

【讨论】:

  • $GET 需要一个下划线 $_GET
  • $_GET 是一个超全局的php.net/manual/en/language.variables.superglobals.php ,这个答案需要为未来的读者编辑,他们可能认为你的答案是正确的,但事实并非如此。您仍然没有修复语法错误。编辑:你确实修好了,太棒了
  • @jothi 不客气,同样“谢谢” ;-) 干杯
【解决方案2】:

试试

function verif(){
        var x;
        if(confirm("Vous êtes sûr que vous voulez supprimer ce compte?") == true){
            window.location.href="supprimerAlbumAdmin.php?id=<?php echo $_GET['id']; ?>";
        }
        else
    }

【讨论】:

    【解决方案3】:

    尝试以下操作:

    function verif(){
        var x;
        if(confirm("Vous êtes sûr que vous voulez supprimer ce compte?")){
            window.location.href="supprimerAlbumAdmin.php?id=<?php echo $_GET['id']; ?>";
        }
    }
    

    【讨论】:

    • 也不行!这可能与它是一个包含 javascript 的 .php 文件这一事实有关吗?
    • 我试过通过 jsfiddle 运行。它工作得很好。在“确定”点击时,它将用户重定向到其他页面
    猜你喜欢
    • 2014-07-09
    • 2022-01-23
    • 2018-05-10
    • 1970-01-01
    • 2016-01-02
    • 2021-02-08
    • 2020-03-18
    • 1970-01-01
    相关资源
    最近更新 更多