Мне нужно сохранить вход пользователя в переменную 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
}
?>
Вот функция Javascript для addtocart():
<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
...
<input id="qty" type="number" name="qty[<?php echo $Row['id'] ?>]" >
Затем вы можете использовать действие PHP POST или JavaScript AJAX на этом, чтобы отправить это и сохранить его, это отдельный аспект, который вам нужно решить, прежде чем люди смогут помочь...