HTML textbox.value未在JavaScript函数中更新

HTML textbox.value未在JavaScript函数中更新,javascript,php,associative-array,Javascript,Php,Associative Array,我有一个包含文本框的PHP项目,用于可视化堆栈数据结构概念;如下面的屏幕截图所示: 我将PHP值传递给JavaScript函数,并仅向没有值的索引号(即堆栈顶部)添加值 代码如下: <html> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Data Structure Vis

我有一个包含文本框的PHP项目,用于可视化堆栈数据结构概念;如下面的屏幕截图所示:

我将PHP值传递给JavaScript函数,并仅向没有值的索引号(即堆栈顶部)添加值

代码如下:

 <html>
 <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Data Structure Visualization</title>
    <link rel=stylesheet href=StackStyleSheet.css>
    <script type="text/javascript">
        function addElement($id, $value)
        {
            var id = $id;
            var value = $value;
            //correct id and value is passed here.

            var textbox = document.getElementById(id);

            //this LOC does not execute and returns to the php code
            textbox.value = value;
        }
    </script>

    <body>
    <?php include 'header.php'; ?>

    <form method="POST" action="">

    <div id ="container">
        <div id="top">
            <h3>STACK (ARRAY IMPLEMENTATION)</h3>
        </div> 

        <div id="bottom">
            <input id="inputBox" type="text" name="inputNumber" value="">
            <button id="pushButton"  type="submit"  class="button button1" name = "PUSH">Push</button>
            <button id="popButton"   type="submit"  class="button button2" name = "POP">Pop</button>
            <button id="clearButton" type="submit"  class="button button3" name = "CLEAR">Clear Stack</button>
        </div>

        <div id="operation">
            <input type="text" name="5E" value="" class="operation1 obutton6">
            <input type="text" name="4E" value="" class="operation1 obutton5">
            <input type="text" name="3E" value="" class="operation1 obutton4">
            <input type="text" name="2E" value="" class="operation1 obutton3">
            <input type="text" name="1E" value="" class="operation1 obutton2">
            <input type="text" name="0E" value="" class="operation1 obutton1">
        </div>

    </div>  

    </form>

    <?php
    include 'footer.php';

    if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['PUSH']))
    {   
        $val0E = (int)$_POST['0E'];
        $val1E = (int)$_POST['1E'];
        $val2E = (int)$_POST['2E'];
        $val3E = (int)$_POST['3E'];
        $val4E = (int)$_POST['4E'];
        $val5E = (int)$_POST['5E'];

        //creating an associative array to store/add/delete values in the index locations

        $elementsindex = array("0E"=>$val0E ,"1E"=>$val1E,"2E"=>$val2E, "3E"=>$val3E, "4E"=>$val4E, "5E"=>$val5E);

            foreach ($elementsindex as $key => $value) 
            {
                if(($elementsindex[$key])==null) 
                {
                    //value copied from textbox
                    $id = $key;
                    $value = $_POST['inputNumber'];

                    //value pushed in
                    $elementsindex[$key] = $value; //saves value against element index

                    //value reflected on screen box
                    echo '<script type="text/javascript">';
                    echo 'addElement("'.$id.'", "'.$value.'");';
                    echo '</script>';
                    break;
                }
                else if(($elementsindex[$key])!=null)
                {
                    continue;
                }
            }
        }
    }
  </body>
</html>

代码哪里错了?

使用
$id
在addElement函数的顶部添加
console.log()
,并检查以确保元素实际存在。这似乎是您应该在javascript中完成的事情。您可以添加click/submit回调,只需通过javascript动态添加元素,而无需涉及PHP。“这似乎太过分了。”弗兰克兹说。我知道,但是我们的项目负责人要求它是在phpy中,我们的代码仍然可以是在PHP中。(最终提交/需要进行的任何服务器端更改)。当您可以在客户端进行这些更改时,以这种方式涉及PHP是愚蠢的。谢谢
  var textbox = document.getElementById(id);

  //this LOC does not execute and returns to the php code
  textbox.value = value;