duan5991518 2014-10-31 00:21
浏览 42
已采纳

javascript调用php里面循环不工作并打破查询

I am attempting to call a javascript function inside a php where loop. I've succeeded in calling the variable, however the function only works on the first line, and then breaks a subsequent query.

The javascript is a simple show/hide of a div or span tag with a specific id. I'm trying to have this appear for every instance of a variable, but only open the span associated with that entry, so I used a php variable from the query.

The javascript code is contained in the header; it works fine without the php, and the php works fine without the javascript but I can't seem to make them work together.

Here's the code:

    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">';

First of all, the showHide is only working on the first record

It is also making this query not respond at all.

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));   



        }
    }
}

I tried removing the return false;, to no avail. Please let me know if I need to show more of the code (e.g. the javascript itself)

Edit:

I've tried working within the php string (this is actually what I had tried first) but it seems to break everything (no javascript, no 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> '; 

Edit: I am open to other solutions if this is not something that is possible. I'm feeling a bit stumped. Realistically I just want to have a list of items called from a mysql database, and have a field appear onclick to edit an associated variable if desired without having to send it to another page or reload the script for usability (hence the javascript piece).

Thanks again, anyone who can assist.

Note: this is the script that I am calling:

<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>
  • 写回答

3条回答 默认 最新

  • dongwen1935 2015-04-12 20:44
    关注

    Okay after a long time on this, I finally figured out what was going on. Part of the issue was that I was trying to call a form inside a form, which I had forgotten is not permitted in HTML, so this required some redesign.

    Other issues involved calling loops within inside loops, which caused problems where the first record would work, but not for the remaining records.

    The javascript above did not need to be modified, only the way that it was called.

    Here is what worked. The main key was using include() instead of 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>';
            }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大