spires Posted April 22, 2009 Share Posted April 22, 2009 Hi guys. I'm completely stuck. I want to be able to add a new value to an array every time the form is submitted. Example: Click submit once - $f1Array[0] = $quantity; Click submit twice - $f1Array[1] = $quantity; However, I just can't get it to work. Here's my code $f1Array = array(); if (isset($_POST['submit'])) { $quantity = $_POST['quantity']; $product = $_POST['product']; $itemComp = $_POST['itemComp']; $unitPrice = $_POST['unitPrice']; $qtyPrice = $_POST['qtyPrice']; $gp = $_POST['gp']; if ($unitPrice=='' || $unitPrice=='0' || $unitPrice=='0.00'){ $finalGP = $itemComp - $product; $gpTotal = $finalGP * $quantity; }else{ $finalGP = $itemComp - $product + $unitPrice; $gpTotal = $finalGP * $quantity; } $finalUnit = $quantity * $unitPrice; if ($f1Array[0] == '') { $f1Array[0] = $quantity; }elseif ($f1Array[1] == '') { $f1Array[1] = $quantity; } } Then, I'm echoing out each array in the HTML body <?PHP echo $f1Array[0].'<br>'.$f1Array[1].'<br>'.$f1Array[2].'<br>'.$f1Array[3]; ?> Can some one please advise Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 22, 2009 Share Posted April 22, 2009 populating an array can be as easy as : $f1Array[] = $_POST['quantity']; //the [] allow values to be added to an array (in this case, $f1Array) print_r($f1Array); //will display the keys=>values in the specified array Quote Link to comment Share on other sites More sharing options...
spires Posted April 22, 2009 Author Share Posted April 22, 2009 Thats great thanks. But what I want it to do is add a new value into the array. Example: When I output the results, I want to have each value of the array on a new line. e.g f1Array[0]<br> f1Array[1]<br> f1Array[2]<br> f1Array[3]<br> and so on. Please see http://www.businessmobiles.com/comcalc/main_interface2.php This is using your code. Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 22, 2009 Share Posted April 22, 2009 do you have to create an array to house your posted values? what are you using that array for? your posted values can be easily displayed like so : echo $quantity.'<br />'.$product.'<br />'.$itemComp; //and so on... Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 22, 2009 Share Posted April 22, 2009 try that out .. play around with it a bit... <?PHP foreach ($_POST as $k => $v) { $asdf[] = $v; } print_r($asdf); ?> <form name="form1" action="" method="post"> <input name="quantity" type="text" size="6" value="45"/> <select name="product" onchange="this.style.backgroundColor = '#FFCCCC'"> <option value=""> </option> <option value="1">add 1</option> <option value="2">add 2</option> <option value="3">add 3</option> <option value="4">add 4</option> <option value=""> </option><option value="450">Nokia</option> </select> <select name="itemComp" onchange="this.style.backgroundColor = '#FFCCCC'"> <option value=""> </option><option value="340" selected="selected">Orange</option> </select> <input name="unitPrice" type="text" size="14" value="0.00"/> <input name="qtyPrice" type="text" size="14" value="0"/> <input name="gp" type="text" size="14" value="15300"/> <input type="submit" name="submit" value="add" /> </form> <?php foreach ($asdf as $value) { echo $value.'<br />'; } ?> just a very, very basic way to do this .. better that you just learn what is going on before getting into any more advanced techniques. Quote Link to comment Share on other sites More sharing options...
spires Posted April 22, 2009 Author Share Posted April 22, 2009 Hi Thanks for all your help, I really appreciate it. The image above shows the form the i'm trying to create. The user selects the information from the top section, once 'add' is clicked, all the information will appear at the bottom. If the user then fill out another 'add to current proposal' section, then this will appear below the last lot of results in the 'current proposal handset details' section. I was thinking that this should be done using an array. Each time the form is submitted, it will add the field values into an array, then display them like in the picture. Thanks once again, Any more help would be great. Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 22, 2009 Share Posted April 22, 2009 i'm always drawn to databases for situations like this .. you could have a temporary table in your db that stores the information upon submission, and then the records (information) is easily retrieved and displayed as per the user's request. Quote Link to comment Share on other sites More sharing options...
nankoweap Posted April 22, 2009 Share Posted April 22, 2009 you need to first decide how/where you're going to store the form's state in between submits. you've at least two choices - the client-side form or the user's server-side session. with something this simple and not knowing a lot about your application, i've done it both ways, but typically lean toward the client-side form. with each form submit build your array from the saved state, then evaluate and perform the action the user selected - add, remove, edit, udpate. jason Quote Link to comment Share on other sites More sharing options...
spires Posted April 22, 2009 Author Share Posted April 22, 2009 Thanks for your help guys. I think i'm gonna store all the info in a database, the pull the values back out to display them row by row. Thanks Quote Link to comment Share on other sites More sharing options...
nankoweap Posted April 22, 2009 Share Posted April 22, 2009 Thanks for your help guys. I think i'm gonna store all the info in a database, the pull the values back out to display them row by row. Thanks ahhh... yes... at least three ways to save state. not a thing wrong with that approach. jason Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.