Page 1 of 1

HTML Form question

Posted: Wed Apr 07, 2010 11:44 am
by mikeman
Is there a way of having all values input from a form assigned to a unique variable when the form may contain random numbers of input boxes?

What I have a is a form that is generated by an initial form. Depending on what the user enters in the first form the the second form contains a list of varying length. I want to use each part of the list and process it but am not too sure how to do this?

Re: HTML Form question

Posted: Wed Apr 07, 2010 12:57 pm
by requinix
It's available. Somehow. Exactly how depends on what the form HTML looks like...

Re: HTML Form question

Posted: Wed Apr 07, 2010 1:00 pm
by thinsoldier
Name the variable length portion of the form as an array so you can then loop through all the elements in the array.

<input type=text name=dognames[] value=rex />
<input type=text name=dognames[] value=spot />
<input type=text name=dognames[] value=yeller />

Re: HTML Form question

Posted: Wed Apr 07, 2010 1:23 pm
by mikeman
Ok that makes some sense. I will quickly explain what I am trying to do:

1st form:
Input box where user enters a string. The user puts asterixes in the string where they want to break the string up. The string gets broken up into multiple bits and stored in an array.
ie:
String = HelloWolrdGoodbye
String with asterixes = Hello*Wolrd*Goodbye
Array = ( [0] => Hello[1] =>World[2] =>Goodbye )

2nd form takes the array and echoes back each position of the array with a drop down box next to it:
Hello - Drop down box
World - Drop down box
Goodbye - Drop down box

The user will then chose options from the drop down box for each part of the array and I will put in some functions to handle each part. It will then return the final processed data.

But because the array can be of varying length I am not certain on how to do it? Can the 2nd form put the new information into another array?

My eventual aim is to have a code generator that lists names, numbers and filenames. It will take the initial string and break it into parts. From there it will either repeat any static text, increment numbers or list filenames so users will not have to type it all out.

Thanks.

Re: HTML Form question

Posted: Wed Apr 07, 2010 3:10 pm
by requinix
mikeman wrote:Ok that makes some sense. I will quickly explain what I am trying to do:

1st form:
Input box where user enters a string. The user puts asterixes in the string where they want to break the string up. The string gets broken up into multiple bits and stored in an array.
ie:
String = HelloWolrdGoodbye
String with asterixes = Hello*Wolrd*Goodbye
Array = ( [0] => Hello[1] =>World[2] =>Goodbye )

2nd form takes the array and echoes back each position of the array with a drop down box next to it:
Hello - Drop down box
World - Drop down box
Goodbye - Drop down box

The user will then chose options from the drop down box for each part of the array and I will put in some functions to handle each part. It will then return the final processed data.

But because the array can be of varying length I am not certain on how to do it? Can the 2nd form put the new information into another array?

My eventual aim is to have a code generator that lists names, numbers and filenames. It will take the initial string and break it into parts. From there it will either repeat any static text, increment numbers or list filenames so users will not have to type it all out.

Thanks.
Then what thinsoldier said is exactly what you need. Does need quotes though.

Code: Select all

<input type="text" name="dognames[]" value="rex" />
<input type="text" name="dognames[]" value="spot" />
<input type="text" name="dognames[]" value="yeller" />
Then $_POST["dognames"] will be an array.

Re: HTML Form question

Posted: Wed Apr 07, 2010 6:07 pm
by thinsoldier
mikeman wrote: 2nd form takes the array and echoes back each position of the array with a drop down box next to it:
Hello - Drop down box
World - Drop down box
Goodbye - Drop down box
unless you're forced to support browsers with no javascript this looks like a job for "ajax"

Re: HTML Form question

Posted: Wed Apr 07, 2010 6:09 pm
by mikeman
Ok I have added an array to pickup the information and this worked and when I printed it out the elements submitted came back in the array. But what I was missing was the value from the option box, this did not pass over. I need this info to determine which function I need to run. Any ideas?

Thanks.

Re: HTML Form question

Posted: Wed Apr 07, 2010 7:38 pm
by requinix
How about you save us all some time and just post what you have?

Re: HTML Form question

Posted: Thu Apr 08, 2010 2:05 pm
by mikeman
Ok - this is as far as I got. In the input box - enter this as an example:

test*the*code*gen*12

Code: Select all

<html>
<body>
<legend>
<form action="formhandler.php" method="post" name="Code generator">

<fieldset class="style1">
    <textarea name="fullstring" cols="100" rows="5"></textarea>    
    <p>Input full string creating a break using the * symbol.</p>
</fieldset>   
 
<div align="center"><input type="submit" name="submit" value="Break code down" /></div>
  
</form>
</legend>
</body>
</html>
This is the handling code:

Code: Select all

<?php
//Text box inputs
$fullstring = $_REQUEST['fullstring'];

//Check if user has set input
if(isset($fullstring)) {
echo "Your full string is: " . $fullstring . "</br>";
} else {
echo "You have not set the type";
}

$arraystring = explode("*", $fullstring);
?>
<html>
<body>
<br>
<INPUT type="button" value="Return to Generator!" onClick="location.href='index.php'">
<br>
<br>
<br>
<form action="results.php" method="post" name="Code generator">
<?php
reset($arraystring);
foreach($arraystring as $part){ 
echo "<div><table width='800' border='1'>
<tr><td><textarea name='partstring[][]' cols='100' rows='1'>" . $part . "</textarea></td><td>
<select name='type' size='1'>
  
  <option selected>Repeating Text</option>
  <option>Number that increments</option>
  <option>Filename</option>
</select></td></tr></table></div>";

} 
?>

<input name="looper" type="integer" size="10" maxlength="4" />
Loop this many times

<div align="center"><input type="submit" name="submit1" value="Make my code!" /></div>
</form>
<br>
<br>
</body>
</html>
And this is my thrid handling form:
[syntax]
?php
//Text box inputs
$partstring = $_POST["partstring"];
$looper = $_POST["looper"];

print_r($partstring);
?>
[/syntax]
Eventually I will write some code to handle the 3 types of input - ie static, number or filename, guess it will be something like this:

[syntax]
switch ($partstring) {
case "Repeating Text":
settype($partstring, "string")
$repeater = 0;
while ($repeater < ($looper)) {
echo $partstring1;
echo "</br>";
$repeater++;
}
break;

case "Number that increments":
settype($partstring, "integer")
while ($repeater < ($looper)) {
echo $partstring;
echo "</br>";
$repeater++;
}
break;

case "filename":
settype($partstring, "string");
$folder = ("./images");
$dir = opendir($folder);
$file_list = "<ul>";
while ($file_name = readdir($dir))
{ if (($file_name != ".") && ($file_name != "..")){
$file_list .= "<li>$file_name";
}
} $file_list .= "</ul>"; closedir($dir);

echo "$folder" . "$file_list";

default:
echo "case where nothing was set";
break;
}
[/syntax]