繁体   English   中英

如何从HTML获取值到PHP变量?

[英]How to get a value from HTML into PHP variable?

我需要将用户输入存储到PHP变量中,但无法这样做。 这是从数据库获取值的代码:

<?php
    while($Row = mysql_fetch_array($Query_Result)) {
    { 
?>                               
        <tr>
            <td><?php echo $Row['Product_Name'] ?></td>
            <td><?php echo $Row['Product_Type'] ?></td>
            <td><?php echo $Row['Product_Price'] ?></td>

            //the "qty" box does not exist in the database. I want user to enter a number in "qty" box and that number should be stored in a php variable 

            <td><input id="qty" type="number" name="qty" ></td>
            <td><input type="button" value="Add to Cart" onclick="addtocart(<?php echo $Row['Product_ID'] ?>)"</td>
        </tr>

<?php
    }
?>

这是addtocart()函数的Javascript:

<script language="javascript">
    function addtocart(pid) {
        document.form1.Product_ID.value=pid;
        document.form1.command.value='add';
        document.form1.submit();
    }
</script>

<?php
    require 'ConnectDB.php';
    require 'functions.php';

    if(isset($_REQUEST['command']) && $_REQUEST['command']=='add' &&   $_REQUEST['Product_ID']>0 ) {
        $pid=$_REQUEST['Prodcut_ID'];
        // This addtocart() function here sets the qauntity to 1 by default.. I want it to be dynamic. User should input the qauntity and I should be able to pass it to the addtocart()        function

        addtocart($pid,1);
        header("location:shopping.php");
        exit();
    }
?>

请帮忙!

上面的代码格式不正确,因此我为您进行了编辑:

<?php while( $Row = mysql_fetch_array( $Query_Result ) ): ?>
    <tr>
        <td><?php echo $Row['Product_Name'] ?></td>
        <td><?php echo $Row['Product_Type'] ?></td>
        <td><?php echo $Row['Product_Price'] ?></td>

        <!-- the "qty" box does not exist in the database. I want user to enter a  number      in     "qty" box and that number should be stored in a php variable -->

        <td><input id="qty" type="number" name="qty" ></td>
        <td><input type="button" value="Add to Cart" onclick="addtocart(<?php echo $Row['Product_ID'] ?>)"</td>
    </tr>
<?php endwhile ?>

您需要做的是添加一个输入字段,然后将行ID放入其中(我假设您的行ID是id ...

<input id="qty" type="number" name="qty[<?php echo $Row['id'] ?>]" >

然后,您可以对此执行PHP POST或JavaScript AJAX操作,以提交并存储它,这是一个独立的方面,您需要在人们可以帮助之前决定...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM