【问题标题】:PHP drop down menu with Javascript alert带有 Javascript 警报的 PHP 下拉菜单
【发布时间】:2012-05-17 00:10:24
【问题描述】:

我在 PHP 中创建了一个简单的下拉菜单:

<td> Privilege: </td>
<td><select name="privilege">
<?php 
    $position_list = array(
                1 => "Chair", 
                2 => "SACA",
                5 => "Faculty",
                0 => "Disable User",
                );
    foreach ($position_list as $priv_id=>$position) {
    echo "<option value= \"{$priv_id}\"";
            if ($priv_id == $sel_user['privilege']) {
                 echo " selected"; 
            }
            echo ">{$position}</option>";   
    }   
?></td></select>        
        </tr>       
        </tr>

我想知道是否可以包含一些 Javascript。因此,当用户选择“禁用用户”时,会显示一条消息“您确定要禁用此用户吗?”弹出。 提前感谢您的任何建议!

【问题讨论】:

  • 这里把PHP抽象出来,专注于html,然后尝试学习一些Javascript,到目前为止你都尝试过什么?
  • 你的“”应该在“”之后
  • 除非网站动态更新(即使用 AJAX),否则您可能只想将用户重定向到另一个页面,以便他们确认他们的选择。

标签: php javascript drop-down-menu alert


【解决方案1】:

这里的问题不是显示警报——这很简单——而是跟踪选择元素曾经拥有的值。毕竟,如果用户单击“否”并且用户仍然被禁用,那将没有多大用处。

为了做到这一点,我建议最简单的做法是使用隐藏的输入元素来存储原始值。然后,您可以参考正确设置选项。

类似这样的(HTML结构在这里更正,请注意;-)):

<td>Privilege:</td>
<td>
  <!-- note the onchange event -->
  <select name="privilege" onchange="checkDisable(this);">
<?php 

    // Define the options
    $position_list = array(
      1 => "Chair", 
      2 => "SACA",
      5 => "Faculty",
      0 => "Disable User",
    );

    // Create the list
    foreach ($position_list as $priv_id=>$position) {
      echo "<option value= \"{$priv_id}\"";
      if ($priv_id == $sel_user['privilege']) {
        echo " selected=\"selected\""; 
      }
      echo ">{$position}</option>";   
    }   

?></select>
  <input type="hidden" id="privilege_old_val" value="<?php echo $sel_user['privilege']; ?>" />
  <script type="text/javascript">
    function checkDisable(element) {

      // Firstly check if the user selected the disable option, then get them to confirm it
      if (element.options[element.selectedIndex].value == 0 && !confirm("Are you sure you want to disable this user?")) {
        // If they didn't confirm it, loop the options...
        for (var i = 0; i < element.options.length; i++) {
          // Find the option with the old value...
          if (element.options[i].value == document.getElementById('privilege_old_val').value) {
            // ...and set the select back to that option
            element.selectedIndex = i;
          } // if
        } // for
      } // if

      // Update the input that tracks the old value with the current value
      document.getElementById('privilege_old_val').value = element.options[element.selectedIndex].value;

    }
  </script>
</td>

【讨论】:

  • 嗨——哇!这是一个很好的答案。我尝试将其粘贴到我的代码中,但它没有更新到数据库。那是因为我需要改变一些事情吗?非常感谢!
  • 很难说没有看到您的数据库代码 - 它以前是否有效?确保服务器仍然在$_GET['privilege']$_POST['privilege'] 中接收到合理的值,无论您使用的是哪个
  • 编辑:没关系,那是我的错误。它工作得很好!太棒了:)
【解决方案2】:

是的,你可以使用onchange事件:

<select name="privilege" onchange="if(this.options[this.selectedIndex].value === 0)confirm('Are you sure you want to disable this user?')">

【讨论】:

  • 嗨,很好的建议。我之前尝试了一些非常相似的东西,但它不起作用。这可能是一个非常愚蠢的问题,但是:我必须更改此代码还是可以将其粘贴进去?抱歉 - 我从来没有用过 Javascript 编程,但我认为这是一个很好的插件功能
【解决方案3】:

这个 JavaScript 应该为你做:

<select name="Select1" onchange="if(this.value==0) confirm('are you sure you want to disable this user?')">
<option value="1">Chair</option>
<option value="2">SACA</option>
<option value="5">Faculty</option>
<option value="0">Disable user</option>
</select>​​​

【讨论】:

  • 您好,感谢您的建议。我试图坚持在 pHp 中创建下拉菜单,这样我就可以跟踪数据库中的选定值(并且先前选定的值将突出显示,等等)
  • 您仍然可以在 PHP 中创建下拉菜单,只需添加 onlick JavaScript 部分。
  • 不会用 alert() 代替 confirm()
  • @gunnx - alert() 只是给你一个确定按钮来点击。 confirm() 为您提供是/否选项,它将根据用户的选择返回真/假。
  • @NVidovic - 很高兴为您提供帮助。如果您认为这回答了您的问题,请考虑对其进行投票和/或将其选为接受的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多