Jump to content

PHP Arrays


bgallegos
 Share

Recommended Posts

I might be over thinking this problem but hopefully someone can help me out. I have a form that gets a customer's pet information. The pet information form requires this information:-Pet Name-Pet Type (Dog, Cat, Bird, Horse, Fish) I would like to store the values from these two form fields into two seperate php arrays. The arrays are $petname and $pettype.Every customer must have at least one pet, so the first form will store it's information into the first spot of the arrays.How do I have it store the information into the php array? What code goes under the name of the form field?

<select name=" ">	<option value="Dog">Dog</option>	<option value="Cat">Cat</option>	<option value="Horse">Horse</option>	<option value="Fish">Fish</option>	<option value="Bird">Bird</option></select>

Let me know if I need to clarify any information.

Link to comment
Share on other sites

What code goes under the name of the form field?
Nothing. The input is evaluated in the next script (the script mentioned in 'action'):pagewithform.html
<form action="script.php" method="post"><input name="name"><br><select name="type"><option value="Dog">Dog</option><option value="Cat">Cat</option><option value="Horse">Horse</option><option value="Fish">Fish</option><option value="Bird">Bird</option></select></form>

script.php

$petname = array($_POST['name']);$pettype = array($_POST['type']);

Every customer must have at least one pet, so the first form will store it's information into the first spot of the arrays.
I did not understand this...
Link to comment
Share on other sites

Alright, at first there is one field for the 1st pet's name and one drop down for the 1st pet's type. There are two buttons below this: Add Another Pet, Remove This Pet. When you click Add Another Pet, javascript displays another field for the 2nd pet's name and one drop down for the 2nd pet's type. Remove This Pet simply removes the fields. This is why I need to store the pet names and the pet types into arrays. Based on what you told me though, I should be able to take care of it. Thanks for the help, and if you have any other suggestions I'll keep checking back.

Link to comment
Share on other sites

There's a shortcut to creating an array like that. If you have a lot of fields on a page and you want to store them in an array, include [] after the name.<select name="type[]">$array_of_types = $_POST['type'];$number_of_types = count($array_of_types);Just give all of the fields you want in the array the same name.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...