bionic25 Posted September 4, 2009 Share Posted September 4, 2009 I'm working on my first shopping cart app. im very nearly finished, but ive got stuck on the bit where a new item is added. problem is the form that this info is taken from doesnt seem to be reutrning a value. really cant figure out where im going wrong. function productDump(){ $result = mysql_query("select * from products"); while($r=mysql_fetch_array($result)) { $title = $r['title']; $description = $r['description']; $price = $r['price']; $pid = $r['id']; echo "$title<br />$description<br />$price <select name=qty id=qty> <option value=01>1</option> <option value=02>2</option> <option value=03>3</option> <option value=04>4</option> <option value=05>5</option> </select> <input type=submit name=submit value=add /><hr />"; if (isset($_POST['submit'])) { $qty= $_POST['qty']; $url="this.php?action=add&pid=$pid&qty=$qty"; echo "$qty"; /* echo "<script>window.location = \"$url\"</script>"; */ } else { } } } thanks for any help ~~bionic Quote Link to comment Share on other sites More sharing options...
bionic25 Posted September 4, 2009 Author Share Posted September 4, 2009 just realised i didnt explain this very well. the form that the $_post gets info from is on the same page using the echo server self thing. the problem is that $qty doesnt reutrn a value. how can i get the value from the select option box chosen to add it to the url redirect and therefore add it to the basket. pleeeease help this is frustrating the hell out of me! Quote Link to comment Share on other sites More sharing options...
tom2oo8 Posted September 4, 2009 Share Posted September 4, 2009 $url="this.php?action=add&pid=". $pid ."&qty=". $qty ; Try that? Hmm i realy dont get exactly what your asking.. What so $qty just dont work? Quote Link to comment Share on other sites More sharing options...
bionic25 Posted September 5, 2009 Author Share Posted September 5, 2009 thanks for the answer. the problem isnt in constructing the new url. the problem is what you said: $qty doesnt return anything thats why im calling echo "$qty"; to check if it has a value. the most success ive managed to have is using, print_r when i manage to get it to print something like Array ([0]=>4 [1]=>1 [2]=>1 [3]=>1) or something like that. however i couldnt separate these either. its a real headache and the only problem i rly cant figure out any ideas| Quote Link to comment Share on other sites More sharing options...
bundyxc Posted September 5, 2009 Share Posted September 5, 2009 function productDump(){ $result = mysql_query("select * from products"); while($r=mysql_fetch_array($result)) { $title = $r['title']; $description = $r['description']; $price = $r['price']; $pid = $r['id']; echo " $title<br />$description<br />$price <select name=qty id=qty> <option value=01>1</option> <option value=02>2</option> <option value=03>3</option> <option value=04>4</option> <option value=05>5</option> </select> <input type=submit name=submit value=add /><hr /> "; if (isset($_POST['submit'])) { $qty= $_POST['qty']; $url="this.php?action=add&pid=$pid&qty=$qty"; echo "$qty"; //echo "<script>window.location = \"$url\"</script>"; } else { print('$_POST[\'submit\'] variable not set. Dumping contents of $_POST: <br />'); print_r($_POST); } } } Quote Link to comment Share on other sites More sharing options...
bionic25 Posted September 5, 2009 Author Share Posted September 5, 2009 Thanks again for looking into it. still no luck though, its doing what is was before, just returning '01' whatever number u select. heres some screenys with your new code implemented before selection: after choosing option '4' arghh this is soo annoying Quote Link to comment Share on other sites More sharing options...
kratsg Posted September 5, 2009 Share Posted September 5, 2009 Can you do a var_dump($var) so we know what exactly is going on? Quote Link to comment Share on other sites More sharing options...
bundyxc Posted September 5, 2009 Share Posted September 5, 2009 Which variable do you want him to dump? Quote Link to comment Share on other sites More sharing options...
kratsg Posted September 5, 2009 Share Posted September 5, 2009 Oh sorry >.< I meant the $qty variable, as it seems to be giving him problems. <?php var_dump($qty); ?> Quote Link to comment Share on other sites More sharing options...
bionic25 Posted September 5, 2009 Author Share Posted September 5, 2009 i dumped $qty and got 01string(2) "01" as out put after i choose one of the options Quote Link to comment Share on other sites More sharing options...
kratsg Posted September 5, 2009 Share Posted September 5, 2009 Hmm, I remember having a similar problem, except when I was working with the <select> it kept submitting it as an array of all possible values. Try the following: $qty = null;//ADD if (isset($_POST['submit'])) { $qty= (int)$_POST['qty'];//ADD the (int) $url="this.php?action=add&pid=$pid&qty=$qty"; echo "$qty"; //echo "<script>window.location = \"$url\"</script>"; Could be a result of cacheing, the (int) added for the $_POST will make sure that an int is set in the $qty variable (just because I love ints) Quote Link to comment Share on other sites More sharing options...
bundyxc Posted September 5, 2009 Share Posted September 5, 2009 Hmm.. any chance you have the 'Live HTTP Headers' addon for Firefox? Quote Link to comment Share on other sites More sharing options...
bionic25 Posted September 5, 2009 Author Share Posted September 5, 2009 its just outputting '1' now. its still totally ignoring the option i choose. any more ideas? im getting out of my depth now! as for the firefox thing i dont think so, ive tried in IE too and exactly the same Quote Link to comment Share on other sites More sharing options...
kratsg Posted September 5, 2009 Share Posted September 5, 2009 Let's try one more... wrap the value=0# with quotes.. <option value='03'>3</option> Although, IMO, it shouldn't make a difference >.< Quote Link to comment Share on other sites More sharing options...
tom2oo8 Posted September 5, 2009 Share Posted September 5, 2009 Let's try one more... wrap the value=0# with quotes.. <option value='03'>3</option> Although, IMO, it shouldn't make a difference >.< Maby get rid of the '0' and just make it the number ? Quote Link to comment Share on other sites More sharing options...
bionic25 Posted September 5, 2009 Author Share Posted September 5, 2009 see why u thought that didnt work though. im using numbers in that form to standardise string length in session when users choose a load of items i.e. upto 99. made no difference unfortunately. maybe i should totally rethink this. is there another way i could get the users quantity preference? otherwise i spose i could try having the form on another page and then have <form action="php-page.php"> and have the function on that page any opinions|? Quote Link to comment Share on other sites More sharing options...
kratsg Posted September 5, 2009 Share Posted September 5, 2009 see why u thought that didnt work though. im using numbers in that form to standardise string length in session when users choose a load of items i.e. upto 99. made no difference unfortunately. maybe i should totally rethink this. is there another way i could get the users quantity preference? otherwise i spose i could try having the form on another page and then have <form action="php-page.php"> and have the function on that page any opinions|? Let's try debugging the form a bit more: Instead of using method='post', change to method='get' and see how the URL looks. If the url updates correctly based on the numbers... then there must be something else in the code that sets $qty. Quote Link to comment Share on other sites More sharing options...
bionic25 Posted September 5, 2009 Author Share Posted September 5, 2009 yeah i tried that earlier. although it doesnt fix it it looks promising. heres the output in add bar qty=4&submit=add&qty=1&qty=1&qty=1 the qty=4 at the start IS THE CORRECT choice. how can i get this working properly though??? Quote Link to comment Share on other sites More sharing options...
tom2oo8 Posted September 5, 2009 Share Posted September 5, 2009 http://mystic-mountains.22web.net/test.php Ok is all you want to do is display qty for the moment? Well displaying the 01 -04 worked fine for me , but i litrally striped down the whole of the code to just the box , and an echo of $_POST['qty'] <form action=?page=none method=post> <select name=qty id=qty> <option value=01>1</option> <option value=02>2</option> <option value=03>3</option> <option value=04>4</option> <option value=05>5</option> </select> <input type=submit name=submit value=add /> </form> <? echo "Qty = {$_POST['qty']}"; ?> Quote Link to comment Share on other sites More sharing options...
kratsg Posted September 5, 2009 Share Posted September 5, 2009 yeah i tried that earlier. although it doesnt fix it it looks promising. heres the output in add bar qty=4&submit=add&qty=1&qty=1&qty=1 the qty=4 at the start IS THE CORRECT choice. how can i get this working properly though??? And there is our problem! It's submitting it right... but you have other form elements named "qty" as well that seems to submit "1". "qty=4&submit=add&qty=1&qty=1&qty=1" Why does your URL look like that? Your $_GET['qty'] can only retrieve the last value set in for 'qty' which is not 4, but 1. Quote Link to comment Share on other sites More sharing options...
tom2oo8 Posted September 5, 2009 Share Posted September 5, 2009 Edit: Nope its not what i thought it might be Quote Link to comment Share on other sites More sharing options...
kratsg Posted September 5, 2009 Share Posted September 5, 2009 yeah i tried that earlier. although it doesnt fix it it looks promising. heres the output in add bar qty=4&submit=add&qty=1&qty=1&qty=1 the qty=4 at the start IS THE CORRECT choice. how can i get this working properly though??? And there is our problem! It's submitting it right... but you have other form elements named "qty" as well that seems to submit "1". "qty=4&submit=add&qty=1&qty=1&qty=1" Why does your URL look like that? Your $_GET['qty'] can only retrieve the last value set in for 'qty' which is not 4, but 1. Any chance it could be that theres the select with the id qty and also with the name qty No chance :-P If you wish to have your form element submit data, it must have a 'name' attribute/value. Quote Link to comment Share on other sites More sharing options...
bionic25 Posted September 5, 2009 Author Share Posted September 5, 2009 as in add to form tag <form ... name="qty"> ?? Quote Link to comment Share on other sites More sharing options...
bionic25 Posted September 5, 2009 Author Share Posted September 5, 2009 yeah i tried that earlier. although it doesnt fix it it looks promising. heres the output in add bar qty=4&submit=add&qty=1&qty=1&qty=1 the qty=4 at the start IS THE CORRECT choice. how can i get this working properly though??? And there is our problem! It's submitting it right... but you have other form elements named "qty" as well that seems to submit "1". "qty=4&submit=add&qty=1&qty=1&qty=1" Why does your URL look like that? Your $_GET['qty'] can only retrieve the last value set in for 'qty' which is not 4, but 1. how can i make this so it retrieves the chosen value then?? Quote Link to comment Share on other sites More sharing options...
kratsg Posted September 5, 2009 Share Posted September 5, 2009 as in add to form tag <form ... name="qty"> ?? Don't need to. Is the code in your original post the whole code for that page? Or perhaps you can post the WHOLE CODE and nothing else. 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.