abbos Posted November 22, 2008 Share Posted November 22, 2008 Hi, I'm having a problem holding a value in a variable and then reloading the page. It is for a shopping cart but I wish to add a field that allows me to manually enter a value for "Labour". I've aded the field but everytime I click on my Update button, it forgets the value I've entered. I can change the quantity values for the items in the cart and this works fine. Here is my code: function showCart() { global $db; $cart = $_SESSION['cart']; if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } $output[] = '<form action="estimate.php?action=update" method="post" id="cart">'; $output[] = '<table border="0" width="100%" align="center">'; $output[] = '<td><strong>Our Ref</strong>:<input type="text" name="ref" /><br/><br/></td>'; $output[] = '</table>'; $output[] = '<table border="0" width="100%" align="center">'; $output[] = '<tr class="estimatetitle">'; $output[] = '<td colspan="2" nowrap>Item</td>'; $output[] = '<td colspan="1" width="100px">Quantity</td>'; $output[] = '<td colspan="1" width="100px">Cost</td>'; $output[] = '</tr>'; $output[] = '<tr>'; $output[] = '<td colspan="4"><hr size="2"></td>'; $output[] = '</tr>'; foreach ($contents as $id=>$qty) { $sql = 'SELECT * FROM product WHERE id = '.$id; $result = $db->query($sql); $row = $result->fetch(); extract($row); $output[] = '<tr>'; $output[] = '<td class="estimatedesc">'.$title.'</td>'; $output[] = '<td><a href="estimate.php?action=delete&id='.$id.'" class="r">Remove</a></td>'; $output[] = '<td class="estimateqty"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>'; $output[] = '<td class="estimatecost">£'.$salesprice.'</td>'; $output[] = '</tr>'; $output[] = '<input name="id[]" type="hidden" value='.$id. ' />'; $subtotal += $salesprice * $qty; $output[] = '</table>'; $output[] = '<tr>'; $output[] = '<td colspan="4"><hr size="1"></td>'; $output[] = '</tr>'; } $output[] = '<p align="right"><input type="text" name="labour'.$id.'" value="'.$labour.'" size="3" maxlength="3" /></p>'; $total += $subtotal + $labour; $output[] = '<p align="right">Subtotal: <strong>£'.$subtotal.'</strong></p>'; $output[] = '<p align="right">Total: <strong>£'.$total.'</strong></p>'; $output[] = '<div><button type="submit">Update</button></div>'; $output[] = '</form>'; } else { $output[] = '<p>Your shopping cart is empty.</p>'; } return join('',$output); } I'm calling the function ShowCart on my cart page. Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/133812-problem-holding-value-in-variable/ Share on other sites More sharing options...
genericnumber1 Posted November 22, 2008 Share Posted November 22, 2008 You can echo the submitted value into the value setting of the input tag (after sanitizing the input of course). (This seems to be a common theme to answers today eh?) Link to comment https://forums.phpfreaks.com/topic/133812-problem-holding-value-in-variable/#findComment-696524 Share on other sites More sharing options...
abbos Posted November 23, 2008 Author Share Posted November 23, 2008 Thanks for the reply but I am really struggling to put together the code and just can't seem to get the syntax right. Could you possibly help please? Link to comment https://forums.phpfreaks.com/topic/133812-problem-holding-value-in-variable/#findComment-696961 Share on other sites More sharing options...
blufish Posted November 23, 2008 Share Posted November 23, 2008 Try using sessions or cookies? http://www.w3schools.com/php/php_cookies.asp http://www.w3schools.com/php/php_sessions.asp hope that helps. Link to comment https://forums.phpfreaks.com/topic/133812-problem-holding-value-in-variable/#findComment-696967 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.