Updating form handler when new inputs added to form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
seezee
Forum Newbie
Posts: 17
Joined: Thu Aug 05, 2010 9:59 am
Location: Oklahoma

Updating form handler when new inputs added to form

Post by seezee »

I've got a form which uses PHP to add labels & corresponding inputs whenever items are added to a database. I need to dynamically add corresponding actions in my form handler as well, but I'm not sure where to start.

Example of the form code (error checking removed & code simplified for clarity):

Code: Select all

<$link = mysqli_connect('p:localhost', 'username', 'password');

$output = mysqli_query($link, 'SELECT id, name FROM pages ORDER BY name');

while ($row = mysqli_fetch_assoc($output)) {
	echo '<li><a href="?page=meal'.$row['id'].'" name="meal['.$row['id'].']" title="'.$row['name'].'">'.$row['name'].'</a></li>
	<li>
	  <input id="mealSP-'.$row['id'].'" name="mealSP['.$row['id'].']" value="0" />
	  <label for="mealSP-'.$row['id'].'">Single Portion</label>
	</li>
	<li>
	  <input id="mealFP-'.$row['id'].'" name="mealFP['.$row['id'].']" value="0" />
	  <label for="mealFP-'.$row['id'].'">Family Pack</label>
	</li>';
	}
and the relevant lines in the form handler should look like this:

Code: Select all

$mealSP = $_POST['mealSP'];
$mealFP = $_POST['mealFP'];

$mealSP[0] = '	'.$mealSP[0].' Cheeseburger, SP<br />';
$mealFP[0] = '	'.$mealFP[0].' Cheeseburger, FP<br />';
$mealSP[1] = '	'.$mealSP[1].' Tamales, SP<br />';
$mealFP[1] = '	'.$mealFP[1].' Tamales, FP<br />';

if (preg_match('%^\s0{1,2}%',$mealSP[0])) { // 0 or 00 resets value to null so it doesn't display in $message
	$mealSP[0] = '';
}

if (preg_match('%^\s0{1,2}%',$mealFP[0])) {
	$mealFP[0] = '';
}

if (preg_match('%^\s0{1,2}%',$mealSP[1])) {
	$mealSP[1] = '';
}

if (preg_match('%^\s0{1,2}%',$mealFP[1])) {
	$mealFP[1] = '';
}

$message='Meals |'.$mealSP[0].$mealFP[0].$mealSP[1].$mealFP[1].'
so that for every item in the array, there's a corresponding line in the form handler to prepend a tab and append the item name, followed by FP or SP, then a check to see if the value begins w/ 0 or 00, then echo the non-null items in $message.

Hope that's clear. Any help appreciated.
User avatar
seezee
Forum Newbie
Posts: 17
Joined: Thu Aug 05, 2010 9:59 am
Location: Oklahoma

Re: Updating form handler when new inputs added to form

Post by seezee »

Okay, here's some preliminary work. Currently, it fails to increment the values from the sql query; all it does is retrieve the post values. Only the 1st item in the array appears, repeatedly.

Code: Select all

$link = mysqli_connect('p:localhost', 'username', 'password');
if (!$link) {
	$output1 = 'Unable to connect to the DB.! ';
	}
else {
	$output1 = 'DB connected! ';
	}
$db = mysqli_select_db($link, 'dbname');
if (!$db) {
	$output2 = 'Unable to find the specified the DB! ';
	}
else {
	$output2 = 'DB found! ';
	}

$output = mysqli_query($link, 'SELECT id, name FROM pages WHERE id > 200 AND id < 206 ORDER BY name');
	if (!$output) {
	$output3 = 'Error fetching meal names & IDs! ';
	}
else {
	$output3 = 'Meal names & IDs found! ';
	}
	
$messages = '<p>'.$output1.$output2.$output3.'</p>';
echo $messages;

$rowTB = mysqli_fetch_assoc($output);

echo '<h6>Single Portions</h6>';

if (isset($_POST["mealSP"]) && is_array($_POST["mealSP"]) 
&& count($_POST["mealSP"]) > 0) 
	{ 
		foreach ($_POST["mealSP"] as $mealSP) 
			{ 
				$mealSP = '	'.$mealSP.' '.$rowTB['name'].', SP<br />';
				echo $mealSP; 
			} 
	}

echo '<hr /><h6>Family Packs</h6>';

if (isset($_POST["mealFP"]) && is_array($_POST["mealFP"]) 
&& count($_POST["mealFP"]) > 0) 
	{ 
		foreach ($_POST["mealFP"] as $mealFP) 
			{ 
				$mealFP = '	'.$mealFP.' '.$rowTB['name'].', FP<br />';
				echo $mealFP; 
			} 
	}

//$mealSP[201] = '	'.$mealSP[201].' Baked Cornflake Chicken w/ Chives, SP<br />';
//$mealFP[201] = '	'.$mealFP[201].' Baked Cornflake Chicken w/ Chives, FP<br />';
//$mealSP[202] = '	'.$mealSP[202].' Baked Ham Omelette, SP<br />';
//$mealFP[202] = '	'.$mealFP[202].' Baked Ham Omelette, FP<br />';

if (preg_match('%^\s0{1,2}%',$mealSP[201])) {
	$mealSP[201] = '';
}

if (preg_match('%^\s0{1,2}%',$mealFP[201])) {
	$mealFP[201] = '';
}

if (preg_match('%^\s0{1,2}%',$mealSP[202])) {
	$mealSP[202] = '';
}

if (preg_match('%^\s0{1,2}%',$mealFP[202])) {
	$mealFP[202] = '';
}
$where_form_is='http'.($HTTP_SERVER_VARS['HTTPS']=='on'?'s':'').'://'.$SERVER_NAME.strrev(strstr(strrev($PHP_SELF),'/'));

$message='<p><pre>Meals |'.$mealSP.$mealFP.'</pre></p>';
echo $message;
Questions: should I be using a 'while loop' instead of a 'foreach'? how can i get both the submitted value & the queried value in the same statement? Also, how can I post code w/o all my quotation marks turning into html entities in this !@#$% forum?!
User avatar
ColonelSandersLite
Forum Commoner
Posts: 35
Joined: Sun May 09, 2010 1:32 am

Re: Updating form handler when new inputs added to form

Post by ColonelSandersLite »

Why do you need a seperate handler for each? Why not just use a single handler which loops through each and gets the name dynamically?


In pseudocode:

Code: Select all

Foreach selection
{
     if selection in choiceArray
     {
          echo "\t$selectionName";
     }
}

or something to that effect

Also:

those ' &#40; and &#41; could really stand to be cleaned up for legibility.
User avatar
seezee
Forum Newbie
Posts: 17
Joined: Thu Aug 05, 2010 9:59 am
Location: Oklahoma

Re: Updating form handler when new inputs added to form

Post by seezee »

ColonelSandersLite wrote:Why do you need a seperate handler for each? Why not just use a single handler which loops through each and gets the name dynamically?
Because I'm a rank beginner, and don't know any better. To that end, while I appreciate your pseudocode example, I'm not quite sharp enough to grasp its implications.
Also:

those ' &#40; and &#41; could really stand to be cleaned up for legibility.
See my final remark in the previous post.
Post Reply