We help IT Professionals succeed at work.

php array of name values

BTMExpert
BTMExpert asked
on
I need an array of inputs in a form but it's not working.

<td align="center"><input name="quantity[]" type="text" id="quantity" value="<? echo $rows['quantity']; ?>"></td>
<td align="center"><input name="code[]" type="text" id="code" value="<? echo $rows['code']; ?>"></td>
<td align="center"><input name="name[]" type="text" id="name" value="<? echo $rows['name']; ?>"></td>
<td align="center"><input name="color[]" type="text" id="color" value="<? echo $rows['color']; ?>"></td>
<td align="center"><input name="size[]" type="text" id="size" value="<? echo $rows['size']; ?>"></td>
<td align="center"><input name="ordernum[]" type="text" id="ordernum" value="<? echo $rows['ordernum']; ?>"></td>
<td align="center"><input name="shiptoname[]" type="text" id="shiptoname" value="<? echo $rows['shiptoname']; ?>"></td>
<td align="center"><input name="orderdate[]" type="text" id="orderdate" value="<? echo $rows['orderdate']; ?>"></td>
<td align="center"><input name="offlist[]" type="text" id="offlist" value="<? echo $rows['offlist']; ?>"></td>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>

<?php
// Check if button name "Submit" is active, do this
if(isset($_POST['Submit'])){
	print_r ($quantity);
}
?>

Open in new window


Comment
Watch Question

Commented:
"...not working ..." is not very helpful.  

What is not working?  
What happens when you run it?  
What error messages do you get?  
What output do you get?
First of all in your <input name="xyz[]" ....> that should be name="xyz"  I don't think you are trying to make an array of quantities or sizes.  You only want one value for each parameter.

When that form is submitted you will have a $_POST[] array that has values for any field that had a value in it when it was submitted.

Perhaps you could post the rest of the code that shows the whole <form> structure and we can help.  There isn't enough info given to fully understand the issue.



Author

Commented:
it's suppose to print out the $quantity array but i dont get anything.

http://www.phpeasystep.com/mysql/10.html

that's where i'm getting my code from
you are not getting anything because unless you have the $quantity array set else where you do not have one set up anywhere in that code snippet.

Author

Commented:
I can't get the <input name="quantity[]" /> to work for some reason.  It's not putting values into the array or I can' t pull them out with the php coding.  

I used this website : http://www.phpeasystep.com/mysql/10.html
And have exactly what they have but I changed the database connections to my own.

How does the name="quantity[]" work?

<form name="form1" method="post" action="">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td>
<td align="center"><input name="name[]" type="text" id="name" value="<? echo $rows['name']; ?>"></td>
<td align="center"><input name="lastname[]" type="text" id="lastname" value="<? echo $rows['lastname']; ?>"></td>
<td align="center"><input name="email[]" type="text" id="email" value="<? echo $rows['email']; ?>"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>

<?php
// Check if button name "Submit" is active, do this
if(isset($_POST['Submit'])){
	print_r ($quantity);
}
?>

Open in new window

I believe that what you will get with a name like quantity[] will end up as an array of all the inputs that have that name.
try doing a print_r($_POST['quantity[]'] or just a print_r of the $_POST itself.

Commented:
The print_r is not working because you don't have any variable named $quantity.

If what you are trying to do is print the value that input takes, then you need to have a

<form ... action="myscript.php" ...>

and a script named myscript.php to extract that value.
<td align="center"><input name="quantity" type="text" id="quantity" value="<? echo $rows['quantity']; ?>"></td>
<td align="center"><input name="code" type="text" id="code" value="<? echo $rows['code']; ?>"></td>
<td align="center"><input name="name" type="text" id="name" value="<? echo $rows['name']; ?>"></td>
<td align="center"><input name="color" type="text" id="color" value="<? echo $rows['color']; ?>"></td>
<td align="center"><input name="size" type="text" id="size" value="<? echo $rows['size']; ?>"></td>
<td align="center"><input name="ordernum" type="text" id="ordernum" value="<? echo $rows['ordernum']; ?>"></td>
<td align="center"><input name="shiptoname" type="text" id="shiptoname" value="<? echo $rows['shiptoname']; ?>"></td>
<td align="center"><input name="orderdate" type="text" id="orderdate" value="<? echo $rows['orderdate']; ?>"></td>
<td align="center"><input name="offlist" type="text" id="offlist" value="<? echo $rows['offlist']; ?>"></td>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>

<?php
// Check if button name "Submit" is active, do this
if(isset($_POST['Submit'])){
   print_r ($quantity);
}
?>

Open in new window

Commented:
Try this.
<form method=post action='myscript.php'>

<td align="center"><input name="quantity" type="text" id="quantity" value="<? echo $rows['quantity']; ?>"></td>
<td align="center"><input name="code" type="text" id="code" value="<? echo $rows['code']; ?>"></td>
<td align="center"><input name="name" type="text" id="name" value="<? echo $rows['name']; ?>"></td>
<td align="center"><input name="color" type="text" id="color" value="<? echo $rows['color']; ?>"></td>
<td align="center"><input name="size" type="text" id="size" value="<? echo $rows['size']; ?>"></td>
<td align="center"><input name="ordernum" type="text" id="ordernum" value="<? echo $rows['ordernum']; ?>"></td>
<td align="center"><input name="shiptoname" type="text" id="shiptoname" value="<? echo $rows['shiptoname']; ?>"></td>
<td align="center"><input name="orderdate" type="text" id="orderdate" value="<? echo $rows['orderdate']; ?>"></td>
<td align="center"><input name="offlist" type="text" id="offlist" value="<? echo $rows['offlist']; ?>"></td>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</form>

<?php
// Check if button name "Submit" is active, do this
if(isset($_POST['Submit'])){
   print_r ($quantity);
}
?>


Then in myscript.php:

<?php
$quantity = $_POST["quantity"];
print_r ($quantity);
?>

Open in new window

Author

Commented:
That's what i was thinking to but that doesn't work either
what I would suggest doing is a print_r($_POST) which will show you exactly what you are passing around and work from there.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.