Jump to content

[SOLVED] Array help needed


bionic25

Recommended Posts

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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|

Link to comment
Share on other sites

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);
	}
}
}

Link to comment
Share on other sites

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:

scrn1

 

after choosing option '4'

scrn2

 

arghh this is soo annoying

 

 

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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|?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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']}";
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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??

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.