php内部的javascript调用where循环不起作用并中断查询


javascript call inside php where loop not working and breaks query

我正试图在php where循环中调用一个javascript函数。我已经成功地调用了变量,但是函数只在第一行起作用,然后中断后续查询。

javascript是一个带有特定id的div或span标记的简单显示/隐藏。我试图让它出现在变量的每个实例中,但只打开与该条目相关的span,所以我使用了查询中的php变量。

javascript代码包含在标头中;它在没有php的情况下运行良好,在没有javascript的情况下也运行良好,但我似乎无法使它们协同工作。

这是代码:

    while($row = mysqli_fetch_array($qir)) {
        $ingredient_id = $row['ingredient_id'];
        echo  '<input type="checkbox" value="' . $ingredient_id . '" name="markdelete[]">';

            echo $row['amt'] . ' ' .$row['ingredient_name']; ?> <button onclick="showHide('<?php echo $row['ingredient_id']; ?>'); return false">Edit amount</button> <br />
             <span id="<?php echo $row['ingredient_id']; ?>" class="hide">
            <?php include_once('amt.php'); 
            echo '</span> ';  
        //  }
        echo '<br />';

    }
    echo '<input type ="submit" name="remove" value="Remove">';

首先,showHide只在第一条记录上工作

它也使得这个查询根本没有响应。

if (isset($_POST['remove']))  {
    iF (!empty($_POST['markdelete'])) {
        foreach ($_POST['markdelete'] as $delete_id)   {

        //   remove specific source from source_subject

           $rem_ing = "DELETE from dish_ingredient 
                       where ingredient_id = $delete_id
                       and dish_id = $dish_id ";
           mysqli_query($dbc, $rem_ing)
             or die ('Error removing ingredient: '.mysqli_error($dbc));   

        }
    }
}

我尝试删除return false;,但没有成功。如果我需要显示更多的代码(例如javascript本身),请告诉我

编辑:

我尝试过在php字符串中工作(这实际上是我第一次尝试的),但它似乎破坏了一切(没有javascript,没有php)

            echo $row['amt'] . ' ' .$row['ingredient_name'] . '<button onclick="showHide('''. $row['ingredient_id'] .''') return false">Edit amount</button> <br />';
             echo '<span id=" '. $row['ingredient_id'] .' " class="hide">';
            include_once('amt.php'); 
            echo '</span> '; 

编辑:如果这不是可能的,我愿意接受其他解决方案。我觉得有点不知所措。实际上,我只想从mysql数据库中调用一个项目列表,并在单击时显示一个字段来编辑相关变量,而无需将其发送到另一个页面或重新加载脚本以获得可用性(因此是javascript部分)。

再次感谢所有能提供帮助的人。

注意:这是我正在调用的脚本:

<script language="JavaScript" type="text/JavaScript">
    menu_status = new Array();
    function showHide(theid){
        if (document.getElementById) {
        var switch_id = document.getElementById(theid);
            if(menu_status[theid] != 'show') {
               switch_id.className = 'show';
               menu_status[theid] = 'show';
            }else{
               switch_id.className = 'hide';
               menu_status[theid] = 'hide';
            }
        }
    }
</script>

您不需要标记,因为您已经在php块中了。尝试不使用

showHide('''.$row['ingredient_id'].''')

并更改

   <?php include_once(....);

   include_once(........);

希望这将工作

===========试试这个javascript

<script language="JavaScript" type="text/JavaScript">
    function showHide(theid){
        if (document.getElementById) {
          var switch_id = document.getElementById(theid);
          if(!switch_id) {
            switch_id.className = (switch_id.className.indexOf("show") > -1) ? "hide" : "show"
          }
        }
}

好吧,经过很长时间的研究,我终于弄清楚发生了什么。部分问题是我试图在表单中调用表单,我忘记了这在HTML中是不允许的,所以这需要重新设计。

其他问题涉及内部循环中的调用循环,这会导致第一条记录工作的地方出现问题,但其余记录则不工作。

上面的javascript不需要修改,只需要修改它的调用方式。

以下是行之有效的方法。主密钥是使用include()而不是include_once()

while($r = $qir->fetch_assoc()) {
    $ingredient_id = $r['ingredient_id'];
    $amt = $r['amt'];
    $ingredient_name = $r['ingredient_name'];
        echo $r['amt'] . ' ' .$r['ingredient_name'];
        if ($row['user_id'] == $user_id) {
            echo ' <span class="openlink"><button onclick="showHide('''.$ingredient_id. ''')">edit amount</button></span><br/>';
            echo '<div id="'.$ingredient_id.'" class="hide">';
            include('amt1.php');
            echo '</div>';
        }
}