Jump to content

Accumulate array data from a form before processing


MrGraphx

Recommended Posts

I am going crazy trying to figure out how to accumulate user responses, to post to an array, before submitting the data to a handling script. I create the array, with a maximum number of entries, and then I want the user to be able to go to as many fields in the array to entr different information. When they are finished, then submit it to a script that will print out each element of the array. In the example below, all I have is a previous and a next button. I will add the other submit button once I figure out how to use the next and previous ones. Any direction would be greatly appreciated. NOTE: When I press the next button, it will work one (1) time only and that is it. Here is the script:

<?php

$Info = $_POST["Info"];
$submit1 = $_POST["Previous"];
$submit2 = $_POST["Next"];

for ($i = 1; $i<= 5;$i++){
$a[] = "This is Number " . $i;
}

// Check to be sure the array was created
echo "The array length is " . count($a) . "<br />";

// Load the length of the array into a separate variable
$arr_length = count($a);

// I have to flip the array to be able to select the
// value I want to view
$a = array_flip($a);

$i = 0;
echo "Array Length = " . $arr_length . "<br />";
?>

<html>
<head>
<title>Personal INFO</title>
</head>
<body>
<form method="post" action="<?php

if($_POST['Previous']){
if($i > 0){
$i--;
}
}
if($_POST['Next']){
if($i < $arr_length){
$i++;
}
}
?>">
<input type = "text" width = "50" name = "Info" value = "<?php echo array_search("$i",$a) ;?>">
<br />
<br />
<input type="submit" value="Previous" name="Previous[]">
<input type="submit" value="Next" name="Next">
</form>
</body>
</html> [img src=\"style_emoticons/[#EMO_DIR#]/huh.gif\" style=\"vertical-align:middle\" emoid=\":huh:\" border=\"0\" alt=\"huh.gif\" /]
I'm really not sure what you were trying to do with your code, especially the code inside the "action" attribute of the <form> tag.

Here's some code that does the "Previous" and "Next". I started with your code and modified it greatly.
[code]<?php
for ($i = 1; $i<= 5;$i++)
    $a[$i] = "This is Number " . $i;
$current = (isset($_POST['index']))?$_POST['index']:1;
if (isset($_POST['next']))$current++;
if (isset($_POST['previous']))$current--;
if ($current > count($a)) $current = 1;
if ($current < 1) $current = count($a);

?>
<html>
<head>
<title>Personal INFO</title>
</head>
<body>
<form method="post" action="">
<input type = "text" width = "50" name = "Info" value = "<?php echo $a[$current] ?>" />
<input type="hidden" name="index" value="<?php echo $current ?>" />
<br />
<br />
<input type="submit" value="Previous" name="previous" />
<input type="submit" value="Next" name="next" />
</form>
</body>[/code]

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.