Javascript 从数据库获取值并显示在html表中,包括PHP

Javascript 从数据库获取值并显示在html表中,包括PHP,javascript,php,oracle11g,Javascript,Php,Oracle11g,我有一个由用户输入数据的表。行是动态创建的。用户给定的值被发送到DB(Oracle 11g),并根据该值获取其他值并插入到表中。代码如下所示: <!DOCTYPE HTML> <html> <body> <?php $code = $_POST['code']; $qty = $_POST['qty']; foreach ($_POST['code'] as

我有一个由用户输入数据的表。行是动态创建的。用户给定的值被发送到DB(Oracle 11g),并根据该值获取其他值并插入到表中。代码如下所示:

<!DOCTYPE HTML>
<html>
    <body>
        <?php
            $code = $_POST['code'];
            $qty = $_POST['qty'];
            foreach ($_POST['code'] as $code => $c) {
                //print $c . "&nbsp;&nbsp;&nbsp;" . $qty[$code] . "<br>"; 
            }
            $link = oci_connect('hd','hd', 'localhost/mydb');
            if(!$link) {
                $e = oci_error();
                exit('Connection error  ' . $e['message']);
            }
            foreach ($_POST['code'] as $code => $c)
            {
                $q1 = "select PROD_NAME, PROD_COST from PRODUCT where PROD_ALIAS = :bv_code";
                $q1parse = oci_parse($link, $q1);
                oci_bind_by_name($q1parse, ':bv_code', $c);
                oci_execute($q1parse);
                while($row = oci_fetch_array($q1parse))
                    PRINT $row['PROD_NAME'] . "&nbsp;&nbsp;&nbsp;&nbsp;" . $row['PROD_COST'] . 
                    "&nbsp;&nbsp;&nbsp;&nbsp;" . $qty[$code] . "&nbsp;&nbsp;&nbsp;&nbsp;" . 
                    ($row['PROD_COST']*$qty[$code]) . "<br>";
            }
        ?>
        <script type = "text/javascript" >
            function addRow()
            {
                var table = document.getElementById('order');
                var row = table.insertRow(-1);
                var cell1 = row.insertCell(0);
                var cell2 = row.insertCell(1);
                var cell3 = row.insertCell(2);
                var cell4 = row.insertCell(3);
                var cell5 = row.insertCell(4);
                cell1.innerHTML = "<input type = 'text' name = 'code[]' /> "; 
                cell2.innerHTML = "";
                cell3.innerHTML = "";
                cell4.innerHTML = "<input type = 'text' name = 'qty[]' />";
                cell5.innerHTML = "";

            }

            function delRow(r) 
            {
                document.getElementById('order').deleteRow(-1);
            }   

        </script>
        <p><strong> Order Details </strong></p>
        <form action = 'm3.php' method = 'POST' >
            <table id = "order" border = 1 border_collapse = "collapse" >
                <tr>
                    <th> Item Code </th>
                    <th> Name </th>
                    <th> Price </th>
                    <th> Quantity </th>
                    <th> Total </th>
                </tr>
            </table>
            <input type = 'submit' name = 'submit' value = 'Submit' />
            <input colspan = '2' type = "button" value = "Add Row" onclick = "addRow('order')" />
            <input colspan = '2' type = "button" value = "Delete Row" onclick = "delRow('order')" />
        </form>
    </body>
</html>
// your code
<table id = "order" border = 1 border_collapse = "collapse" >
    <tr>
        <th> Item Code </th>
        <th> Name </th>
        <th> Price </th>
        <th> Quantity </th>
        <th> Total </th>
    </tr>
    <?php
        $sumTotal = 0;
        foreach ($_POST['code'] as $code => $c){
            $q1 = "select PROD_NAME, PROD_COST from PRODUCT where PROD_ALIAS = :bv_code";
            $q1parse = oci_parse($link, $q1);
            oci_bind_by_name($q1parse, ':bv_code', $c);
            oci_execute($q1parse);
            while($row = oci_fetch_array($q1parse)){
                ?>
                <tr>
                    <td><?php echo $c; ?></td>
                    <td><?php echo $row['PROD_NAME']; ?></td>
                    <td><?php echo $row['PROD_COST']; ?></td>
                    <td><?php echo $qty[$code]; ?></td>
                    <td><?php echo $row['PROD_COST']*$qty[$code]; ?></td>
                </tr>
                <?php
                $sumTotal += $row['PROD_COST']*$qty[$code];
            }
        }
    ?>
    <tr>
        <td>Sum Total</td>
        <td colspan="4"><?php echo $sumTotal; ?></td>
    </tr>
</table>
// your code

函数addRow()
{
var table=document.getElementById('order');
var行=table.insertRow(-1);
var cell1=行插入单元格(0);
var cell2=行插入单元格(1);
var cell3=行插入单元格(2);
var cell4=行插入单元格(3);
var cell5=行插入单元格(4);
cell1.innerHTML=“”;
cell2.innerHTML=“”;
cell3.innerHTML=“”;
cell4.innerHTML=“”;
cell5.innerHTML=“”;
}
函数delRow(r)
{
document.getElementById('order').deleteRow(-1);
}   
订单详细信息

项目代码 名称 价格 量 全部的

用户提供“代码”和“数量”的值。将“代码”发送到数据库,并获取产品的其他值。问题是我想在同一个表中显示获取的值。现在我把它展示在桌子外面。另外,我希望在最后有一列显示账单总额。如中所示,总计(价格*数量)。我该怎么做呢?

与其将整个
foreach
循环放在页面中,不如将其放在
如下所示:

<!DOCTYPE HTML>
<html>
    <body>
        <?php
            $code = $_POST['code'];
            $qty = $_POST['qty'];
            foreach ($_POST['code'] as $code => $c) {
                //print $c . "&nbsp;&nbsp;&nbsp;" . $qty[$code] . "<br>"; 
            }
            $link = oci_connect('hd','hd', 'localhost/mydb');
            if(!$link) {
                $e = oci_error();
                exit('Connection error  ' . $e['message']);
            }
            foreach ($_POST['code'] as $code => $c)
            {
                $q1 = "select PROD_NAME, PROD_COST from PRODUCT where PROD_ALIAS = :bv_code";
                $q1parse = oci_parse($link, $q1);
                oci_bind_by_name($q1parse, ':bv_code', $c);
                oci_execute($q1parse);
                while($row = oci_fetch_array($q1parse))
                    PRINT $row['PROD_NAME'] . "&nbsp;&nbsp;&nbsp;&nbsp;" . $row['PROD_COST'] . 
                    "&nbsp;&nbsp;&nbsp;&nbsp;" . $qty[$code] . "&nbsp;&nbsp;&nbsp;&nbsp;" . 
                    ($row['PROD_COST']*$qty[$code]) . "<br>";
            }
        ?>
        <script type = "text/javascript" >
            function addRow()
            {
                var table = document.getElementById('order');
                var row = table.insertRow(-1);
                var cell1 = row.insertCell(0);
                var cell2 = row.insertCell(1);
                var cell3 = row.insertCell(2);
                var cell4 = row.insertCell(3);
                var cell5 = row.insertCell(4);
                cell1.innerHTML = "<input type = 'text' name = 'code[]' /> "; 
                cell2.innerHTML = "";
                cell3.innerHTML = "";
                cell4.innerHTML = "<input type = 'text' name = 'qty[]' />";
                cell5.innerHTML = "";

            }

            function delRow(r) 
            {
                document.getElementById('order').deleteRow(-1);
            }   

        </script>
        <p><strong> Order Details </strong></p>
        <form action = 'm3.php' method = 'POST' >
            <table id = "order" border = 1 border_collapse = "collapse" >
                <tr>
                    <th> Item Code </th>
                    <th> Name </th>
                    <th> Price </th>
                    <th> Quantity </th>
                    <th> Total </th>
                </tr>
            </table>
            <input type = 'submit' name = 'submit' value = 'Submit' />
            <input colspan = '2' type = "button" value = "Add Row" onclick = "addRow('order')" />
            <input colspan = '2' type = "button" value = "Delete Row" onclick = "delRow('order')" />
        </form>
    </body>
</html>
// your code
<table id = "order" border = 1 border_collapse = "collapse" >
    <tr>
        <th> Item Code </th>
        <th> Name </th>
        <th> Price </th>
        <th> Quantity </th>
        <th> Total </th>
    </tr>
    <?php
        $sumTotal = 0;
        foreach ($_POST['code'] as $code => $c){
            $q1 = "select PROD_NAME, PROD_COST from PRODUCT where PROD_ALIAS = :bv_code";
            $q1parse = oci_parse($link, $q1);
            oci_bind_by_name($q1parse, ':bv_code', $c);
            oci_execute($q1parse);
            while($row = oci_fetch_array($q1parse)){
                ?>
                <tr>
                    <td><?php echo $c; ?></td>
                    <td><?php echo $row['PROD_NAME']; ?></td>
                    <td><?php echo $row['PROD_COST']; ?></td>
                    <td><?php echo $qty[$code]; ?></td>
                    <td><?php echo $row['PROD_COST']*$qty[$code]; ?></td>
                </tr>
                <?php
                $sumTotal += $row['PROD_COST']*$qty[$code];
            }
        }
    ?>
    <tr>
        <td>Sum Total</td>
        <td colspan="4"><?php echo $sumTotal; ?></td>
    </tr>
</table>
// your code
//您的代码
项目代码
名称
价格
量
全部的
总数
//你的代码

不应将整个
foreach
循环放在页面中,而应将其放在
如下所示:

<!DOCTYPE HTML>
<html>
    <body>
        <?php
            $code = $_POST['code'];
            $qty = $_POST['qty'];
            foreach ($_POST['code'] as $code => $c) {
                //print $c . "&nbsp;&nbsp;&nbsp;" . $qty[$code] . "<br>"; 
            }
            $link = oci_connect('hd','hd', 'localhost/mydb');
            if(!$link) {
                $e = oci_error();
                exit('Connection error  ' . $e['message']);
            }
            foreach ($_POST['code'] as $code => $c)
            {
                $q1 = "select PROD_NAME, PROD_COST from PRODUCT where PROD_ALIAS = :bv_code";
                $q1parse = oci_parse($link, $q1);
                oci_bind_by_name($q1parse, ':bv_code', $c);
                oci_execute($q1parse);
                while($row = oci_fetch_array($q1parse))
                    PRINT $row['PROD_NAME'] . "&nbsp;&nbsp;&nbsp;&nbsp;" . $row['PROD_COST'] . 
                    "&nbsp;&nbsp;&nbsp;&nbsp;" . $qty[$code] . "&nbsp;&nbsp;&nbsp;&nbsp;" . 
                    ($row['PROD_COST']*$qty[$code]) . "<br>";
            }
        ?>
        <script type = "text/javascript" >
            function addRow()
            {
                var table = document.getElementById('order');
                var row = table.insertRow(-1);
                var cell1 = row.insertCell(0);
                var cell2 = row.insertCell(1);
                var cell3 = row.insertCell(2);
                var cell4 = row.insertCell(3);
                var cell5 = row.insertCell(4);
                cell1.innerHTML = "<input type = 'text' name = 'code[]' /> "; 
                cell2.innerHTML = "";
                cell3.innerHTML = "";
                cell4.innerHTML = "<input type = 'text' name = 'qty[]' />";
                cell5.innerHTML = "";

            }

            function delRow(r) 
            {
                document.getElementById('order').deleteRow(-1);
            }   

        </script>
        <p><strong> Order Details </strong></p>
        <form action = 'm3.php' method = 'POST' >
            <table id = "order" border = 1 border_collapse = "collapse" >
                <tr>
                    <th> Item Code </th>
                    <th> Name </th>
                    <th> Price </th>
                    <th> Quantity </th>
                    <th> Total </th>
                </tr>
            </table>
            <input type = 'submit' name = 'submit' value = 'Submit' />
            <input colspan = '2' type = "button" value = "Add Row" onclick = "addRow('order')" />
            <input colspan = '2' type = "button" value = "Delete Row" onclick = "delRow('order')" />
        </form>
    </body>
</html>
// your code
<table id = "order" border = 1 border_collapse = "collapse" >
    <tr>
        <th> Item Code </th>
        <th> Name </th>
        <th> Price </th>
        <th> Quantity </th>
        <th> Total </th>
    </tr>
    <?php
        $sumTotal = 0;
        foreach ($_POST['code'] as $code => $c){
            $q1 = "select PROD_NAME, PROD_COST from PRODUCT where PROD_ALIAS = :bv_code";
            $q1parse = oci_parse($link, $q1);
            oci_bind_by_name($q1parse, ':bv_code', $c);
            oci_execute($q1parse);
            while($row = oci_fetch_array($q1parse)){
                ?>
                <tr>
                    <td><?php echo $c; ?></td>
                    <td><?php echo $row['PROD_NAME']; ?></td>
                    <td><?php echo $row['PROD_COST']; ?></td>
                    <td><?php echo $qty[$code]; ?></td>
                    <td><?php echo $row['PROD_COST']*$qty[$code]; ?></td>
                </tr>
                <?php
                $sumTotal += $row['PROD_COST']*$qty[$code];
            }
        }
    ?>
    <tr>
        <td>Sum Total</td>
        <td colspan="4"><?php echo $sumTotal; ?></td>
    </tr>
</table>
// your code
//您的代码
项目代码
名称
价格
量
全部的
总数
//你的代码

哦,太好了。它是有效的,但是如何计算总数呢?正如总金额一样?@KaranGupta我更新了我的答案,以回应您的评论,即适应总金额的计算。希望这能解决你的问题,但并没有达到预期的效果。我得到一个错误“第70行中未定义变量sumTotal”,即我们声明“sumTotal”的行。但它也会显示结果,尽管不在表中!)注意:未定义的变量:E:\xampp\htdocs\myfiles\m3.php中的sumTotal在第70行调用堆栈#时间内存函数位置1 0.0005 140960{main}()…\m3.php:0I修复了错误,在声明sumTotal时没有放置“;”。但它仍然会产生上面的错误。哦,太好了。它是有效的,但是如何计算总数呢?正如总金额一样?@KaranGupta我更新了我的答案,以回应您的评论,即适应总金额的计算。希望这能解决你的问题,但并没有达到预期的效果。我得到一个错误“第70行中未定义变量sumTotal”,即我们声明“sumTotal”的行。但它也会显示结果,尽管不在表中!)注意:未定义的变量:E:\xampp\htdocs\myfiles\m3.php中的sumTotal在第70行调用堆栈#时间内存函数位置1 0.0005 140960{main}()…\m3.php:0I修复了错误,在声明sumTotal时没有放置“;”。但它仍然给出了上述错误。