Help with Student Assignment on Forms

Help with Student Assignment on Forms

Page 1 of 1

2 Replies - 1148 Views - Last Post: 15 March 2010 - 09:59 PM Rate Topic: -----

#1 mbhamel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-March 10

Help with Student Assignment on Forms

Posted 15 March 2010 - 05:21 PM

I am trying to create an associative array with 5 products and 5 quantities using the following. It is not working not getting anything in $array10 how would I go about getting them in the array and printing it out.

<?php
$count=count($_POST['name']);
for($i=0;$i<$count;$i++){
	$name = $_POST['name[i]'];
	$qty = $_POST['qty[i]'];
	$array10[] = array("$name" => $qty);
} 
print_r ($array10)
?>

<form action="<?php $_SERVER['PHP_SELF']; ?>" method='post'>
	Enter Product: <input name = name[] size = 20 type=text />
	Enter Qty: <input name=qty[] size=5 type=text /><br></br>
	Enter Product: <input name = name[] size = 20 type=text />
	Enter Qty: <input name=qty[] size=5 type=text /><br></br>
	Enter Product: <input name = name[] size = 20 type=text />
	Enter Qty: <input name=qty[] size=5 type=text /><br></br>
        Enter Product: <input name = name[] size = 20 type=text /> 
	Enter Qty: <input name=qty[] size=5 type=text /><br></br>
Enter Product: <input name = name[] size = 20 type=text /> 
	Enter Qty: <input name=qty[] size=5 type=text /><br></br>

	<input type="submit" value="Submit" />
	
</form>

This post has been edited by JackOfAllTrades: 15 March 2010 - 09:47 PM
Reason for edit:: Added code tags. PLEASE!!! [code]...PUT YOUR CODE IN HERE!!!...[/code]


Is This A Good Question/Topic? 0
  • +

Replies To: Help with Student Assignment on Forms

#2 333OnlyHalfEvil   User is offline

  • D.I.C Addict

Reputation: 24
  • View blog
  • Posts: 674
  • Joined: 20-March 09

Re: Help with Student Assignment on Forms

Posted 15 March 2010 - 05:48 PM

Do you have the HTML form and the php code in separate files?
Was This Post Helpful? 0
  • +
  • -

#3 aklo   User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 233
  • Joined: 23-January 09

Re: Help with Student Assignment on Forms

Posted 15 March 2010 - 09:59 PM

They are automatically inside an array already since you already did name[]
you just have to use foreach loop to print them out.
So if i need to print any values from an array usually i will think of foreach. I'm new to php so there maybe be other ways to do it but foreach is what i know so far.

<?php

$name = $_POST['name'];
$qty = $_POST['qty'];

?>

<form action="<?php $_SERVER['PHP_SELF']; ?>" method='post'>
Enter Product: <input name = name[] size = 20 type=text />
Enter Qty: <input name=qty[] size=5 type=text /><br></br>
Enter Product: <input name = name[] size = 20 type=text />
Enter Qty: <input name=qty[] size=5 type=text /><br></br>
Enter Product: <input name = name[] size = 20 type=text />
Enter Qty: <input name=qty[] size=5 type=text /><br></br>
Enter Product: <input name = name[] size = 20 type=text />
Enter Qty: <input name=qty[] size=5 type=text /><br></br>
Enter Product: <input name = name[] size = 20 type=text />
Enter Qty: <input name=qty[] size=5 type=text /><br></br>

<input type="submit" value="Submit"/>

</form> 

<?php
//Use this to have a clear look at the array
echo "<pre>";
print_r($name);
echo "</pre>";


echo "<pre>";
print_r($qty);
echo "</pre>";


$i = 0;
foreach($name as $key =>$value){
	echo $name[$i]. "<br />";
	$i++;
}

?>



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1