Jump to content

[SOLVED] Multiple HTML form arrays?


xnowandtheworldx

Recommended Posts

i'm having issues using multiple arrays within HTML forms. It's for adding quests for my game, and basically, there are 3 steps.

 

1. The admin goes to the page, enters the number of "objectives" for the quest.

2. They are brought to page with all the fields they need.

3. Form is submitted, and quest is added

 

Basically my problem is, when the form is generated, say they wanted 3 "objectives" on the form the html for those "objectives" would be as followed..

 

<tr>
         <td height="15"> </td>
</tr>
<tr>
<td align="right">Objective 1:</td>
<td><input type="text" name="objectives[]"/></td>
</tr>
<tr>
        <td align="right">Amount 1:</td>
        <td><input type="text" name="amount[]"/></td>
</tr>
<tr>
<td align="right">Type 1:</td>
<td>
        <select name="type[]">
        <option value="Killed">Kill</option>
                <option value="Collected">Collection</option>
	</select>
</td>
</tr>
<tr>
<td height="15"> </td>
</tr>
<tr>
<td align="right">Objective 2:</td>
<td><input type="text" name="objectives[]"/></td>
</tr>
<tr>
<td align="right">Amount 2:</td>
<td><input type="text" name="amount[]"/></td>
</tr>
<tr>
<td align="right">Type 2:</td>
<td>
	<select name="type[]">
	<option value="Killed">Kill</option>
	<option value="Collected">Collection</option>
          	</select>
</td>
</tr>
<tr>
<td height="15"> </td>
</tr>
<tr>
<td align="right">Objective 3:</td>
<td><input type="text" name="objectives[]"/></td>
</tr>
<tr>
<td align="right">Amount 3:</td>
<td><input type="text" name="amount[]"/></td>
</tr>
<tr>
<td align="right">Type 3:</td>
        <td>
	<select name="type[]">
	<option value="Killed">Kill</option>
	<option value="Collected">Collection</option>
	</select>
</td>
</tr>
<tr>
        <td COLSPAN="2"><input type="submit" name="submit" value="Add" /></td>
</tr>

 

So, the form arrays are, objectives[], amount[], and type[], but when I got to submit it returns just a white page, but when I only have one array with the form, it's perfectly fine, can you not have more than one array with forms? One more thing, i'll post how I parse the arrays below.

 

if($_POST['submit'])
{
      foreach($_POST['objectives'] as $objective)
      {
           $objectiveInsert .= $objective . ",";
      }
      foreach($_POST['amount'] as $amount)
      {
           $amountInsert.= $amount . ",";
      }
      foreach($_POST['type'] as $type)
      {
           $typeInsert.= $type . ",";
      }
}

 

see any problems anywhere? Or are multiple arrays with forms not possible? Any help would be nice.

Link to comment
https://forums.phpfreaks.com/topic/120760-solved-multiple-html-form-arrays/
Share on other sites

This piece of code...

 

if($_POST['submit'])
{
      foreach($_POST['objectives'] as $objective)
      {
           $objectiveInsert .= $objective . ",";
      }
      foreach($_POST['amount'] as $amount)
      {
           $amountInsert.= $amount . ",";
      }
      foreach($_POST['type'] as $type)
      {
           $typeInsert.= $type . ",";
      }
}

 

Doesn't output anything, so your only ever going to get a blank page.

yes, I know that, I was just showing you how I actually parse it, I do echo all of the variables to the page. The problem is, if I have more than one form array, it will only display a white page, I tried turning on error reporting, and nothing.

chances are it's just the echoing part that's failing on multiple arrays.  keep in mind, you will have arrays whether you're inserting only one objective or many.  we'd need to see the full processing page to help you out a little more.

Well here's my current code, just like you asked.

 

<?php
$admin = true;
$needToLogin = true;
include '../includes/header.php';
if($_GET['step'] == 1 || empty($_GET['step']))
{
echo "
<form method=\"POST\" action=\"{$_SERVER['PHP_SELF']}?step=2\">
<table class=\"content\" align=\"center\" width=\"50%\">
	<tr>
		<td align=\"right\">Objective amount:</td><td>
		<input type=\"text\" name=\"amount\" size=\"10\" /></td>
	</tr>
	<tr>
		<td COLSPAN=\"2\" align=\"center\">
		<input type=\"submit\" name=\"continue\" value=\"Continue ->\" />
		</td>
	</tr>
</table>
</form>";
}
else
{
if($_POST['submit'])
{
	foreach($_POST['objective'] as $objective)
	{
		$objectiveInsert .= $objective . ",";
	}
	foreach($_POST['amount'] as $amount)
	{
		$amountInsert .= $amount . ",";
	}
	foreach($_POST['type'] as $type)
	{
		$typeInsert .= $type . ",";
	}

	echo $objectiveInsert . "<br/>";
	echo $amountInsert . "<br/>";
	echo $typeInsert . "<br/>";
}
echo "
<form method=\"POST\" action=\"{$_SERVER['PHP_SELF']}?step=2\">
<input type=\"hidden\" name=\"amount\" value=\"$_POST[amount]\" />
<table class=\"content\" align=\"center\" width=\"50%\">
	<tr>
		<td align=\"right\">Quest name:</td><td><input type=\"text\" name=\"name\" /></td>
	</tr>
	<tr>
		<td align=\"right\">NPC(id):</td><td><input type=\"text\" name=\"npc\" size=\"10\" /></td>
	</tr>
	<tr>
		<td align=\"right\">Reward type:</td><td>
		<select name=\"rType\">
			<option value=\"Clicks\">Clicks</option>
			<option value=\"Item\">Item</option>
			<option value=\"Skill\">Skill</option>
		</select>
		</td>
	</tr>
	<tr>
		<td align=\"right\" onMouseover=\"ddrivetip('Item/skill name or click amount','black', 100);\" onmouseout=\"hideddrivetip();\">
		Reward:
		</td>
		<td><input type=\"text\" name=\"reward\" size=\"10\" /></td>
	</tr>
	<tr>
		<td align=\"right\">Parent Quest:</td>
		<td><select name=\"pQuest\">";

$parentQ = $db->Query("SELECT * FROM `database`");
while($pQ = $db->Fetch($parentQ))
{
	echo "<option value=\"{$pQ['id']}\">{$pQ['name']} - TG? {$pQ['tg']}</option>"; 
}

echo "
</select>
</td>
</tr>
	<tr>
		<td align=\"right\">TG?</td>
		<td>
			<select name=\"tg\">
				<option value=\"y\">Yes</option>
				<option value=\"n\">No</option>
			</select>
		</td>
	</tr>";

for($i = 1; $i <= $_POST['amount']; $i++)
{
	echo "
	<tr>
		<td height=\"15\"> </td>
	</tr>
	<tr>
		<td align=\"right\">Objective $i:</td>
		<td><input type=\"text\" name=\"objectives[]\" /></td>
	</tr>
	<tr>
		<td align=\"right\">Amount $i:</td>
		<td><input type=\"text\" name=\"amount[]\" /></td>
	</tr>
	<tr>
		<td align=\"right\">Type $i:</td>
		<td>
		<select name=\"type[]\">
		<option value=\"Killed\">Kill</option>
		<option value=\"Collected\">Collection</option>
		</select>
		</td>
	</tr>";
}
echo "
<tr>
	<td COLSPAN=\"2\"><input type=\"submit\" name=\"submit\" value=\"Add\" /></td>
</tr>
</table>
</form>";
}
include '../includes/footer.php'; 
?>

 

Just tested again, and when I have the form arrays in the code it will submit, but only return a white page, but if I remove them, it will submit fine, with my header/footer included, and the quest add form below, i've looked everywhere but just can not find what is stopping the script from executing. :\

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.