Jump to content

Taking user input from form to populate an array


morrism35

Recommended Posts

I know in java this was almost trivial to do.

1. Use a for loop for how many values you want to populate the array with.

2. Enter your user data and assign it to the array.

 

This does not seem so easy in php. This is kind of what i've been working on but it just endlessly keeps asking

me for the user endput even though i'm able to output the values, but I want this to be an area that i can sort etc.

 

 

for($count=0; $count<=3; ++$count){
if($_POST['submit']){
$Signs=$_POST['sign'];
 
?>
 
 
<form action='AlphabetizeSigns.php' method='POST'
enctype='multipart/form-data'>
Type in a zodiac sign<input type='text' name='sign'>
 
 
<input type='submit' name='submit' value='submit'>
Link to comment
Share on other sites

Not really sure what your doing but this will put the form data into an array. If you are wanting to save the array and keep adding to it you would need to put the data in a session.

<?php
if($_POST){
$signs[]=$_POST['sign'];
echo "<pre>";
print_r($signs);
echo "</pre>";
}
?>
<form method='post'>
Type in a zodiac sign<input type='text' name='sign'>
<input type='submit' name='submit' value='submit'>
</form>
Edited by benanamen
Link to comment
Share on other sites

Here ya go. This is not accounting for 12 names. You would just need to count the elements in the array before adding a new one.

 

<?php
session_start
();
if (
$_POST)
    {   
    
$_SESSION['signs'][] = $_POST['sign'];
    }
if (isset(
$_SESSION['signs']))
    {
    echo 
"Number of Elements: " count($_SESSION['signs']);
    s
ort($_SESSION['signs']);
    echo 
"<pre>";
    
print_r($_SESSION['signs']);
    echo 
"</pre>";
    }

?>
<form method='post'>
Type in a zodiac sign<input type='text' name='sign'>
<input type='submit' name='submit' value='submit'>
</form>
Edited by benanamen
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.