Multiple Forms on One Page

Under each Article on my website, members can post Comments.

And beneath each Comment, other members can give the Comment a rating.

So if there are 30 Comments, I would have 30 Forms!!

What is the proper way to set up my Forms and Submit buttons so they work properly?

Here is an example…


	<form id="form_1" action="" method="post">
		<input type="submit" name="surveySubmit" value="Submit" />
	</form>

	<form id="form_2" action="" method="post">
		<input type="submit" name="surveySubmit" value="Submit" />
	</form>


(**NOTE: In addition to all of these “survey forms”, I have another Form which handle the sorting of Comments, so it needs to integrate into the final solution as well…)

Questions:
1a.) Can the NAME be the same across Forms?

1b.) Would I want it to be the same?

2a.) Can VALUE be the same across Forms?

2b.) Would I want it to be the same?

3a.) In general, what is the logic behind setting things up so I get the results I want?

Sincerely,

Debbie

Simplest way is to give each submit button a unique name. Then check if it was submitted, and process accordingly.

    <form id="form_1" action="" method="post">
        <input type="submit" name="submit_1" value="Submit" />
    </form>

    <form id="form_2" action="" method="post">
        <input type="submit" name="submit_2" value="Submit" />
    </form>


<?php

if( $_POST['submit_1'] ) {

    // Do stuff

}
else if( $_POST['submit_2'] ) {

  // Do stuff

}

?>

It’s a bit hackish, but will do exactly what you need it to.

[B]

1a.) Can the NAME be the same across Forms?[/B]

It would be bad practice to do so, generally speaking.

1b.) Would I want it to be the same?

Probably not.

2a.) Can VALUE be the same across Forms?

Yes.

2b.) Would I want it to be the same?

Depends on what you are expecting the forms to output

3a.) In general, what is the logic behind setting things up so I get the results I want?

What most people would do in this scenario is wrap the entire comment section inside ONE form, and then use a hidden input field to identify the member submitting a comment.

For example:


<form id="form_1" action="" method="post">
        <input type="submit" name="surveySubmit" value="Submit" />
        <input type="hidden" name="username" value="<?= $_SESSION['username'] ?>" />
</form> 

arout77,

Thanks for the suggestions, but I have to disagree on some points…

FIRST, when someone wants to “rate” a Comment, I only want them to rate ONE Comment at a time. (Yes, it would be possible to make it so someone could rate all Comments in one pass, but that’s not the logic I want.)

And I know from experience a few weeks ago that trying to wrap everything in one form failed miserably when I was using radio buttons!

So I’d say my One-Form-One-Comment design is best.

SECONDLY, since the PHP handling process is the same regardless of which Comment gets rated, it seems to me that having the same NAME would make sense.

Presumably a person could only click on one Submit button at a time, and thus only ever rate one Comment at a time, so having the same NAME should not cause issues, and it allows my PHP code to be easier.

Now if each Form did something different, then having a different NAME would make sense.

Right?


Here is a more detailed description of my web page…

First of all, my script has one Form which is used for sorting the Comments.

Then I have another Form - one for each Comment - that will be used to process 2-3 questions about the Comment (e.g. “Did you learn something?” and “Rate the Comment”).

For Question #1 there might be a radio button, and for Question #2 a dropdown, and each Comment would have these two questions.

What I am asking for help with in this thread, is setting up my FORMS and FORM ITEMS in such a way that my PHP knows whether the request is to Sort Comments or if it is submitting a “survey” on one of the Comments.

Follow me? :-/

Sincerely,

Debbie

It shouldn’t be a problem having two forms per-comment, one for rating, one for comment. Here’s a basic example with sample article array.

<?php
//echo "<pre>";
//print_r($_POST);
//echo "</pre>";
?>
<html>
<body>
<?php
$articles = array(
	'1' => array(
		'name' => 'Debbie',
		'article_name' => "Debbie's Traits",
		'article' => "Please list my Traits",
		'rating' => '4',
		'comments' => array(
			'14' => array(
				'comment' => 'Smart',
				'rating' => '4'
			),		
			'22' => array(
				'comment' => 'Thorough',
				'rating' => '5'
			),			
			'23' => array(
				'comment' => 'Detailed',
				'rating' => '3'
			)		
		)
	)	
);
//echo "<pre>";
//print_r($articles);
//echo "</pre>";

//Display
$cnt=1;
foreach($articles as $article_id => $arr1):

	echo '<div><a href="?article_id=' . $article_id . '"><strong>' . $arr1['article_name'] . '</strong></a> <br />
	<span style="font-size:smaller;">By ' . $arr1['name'] . '</span></div><br />
	
	<div>' . $arr1['article'] . '</div><br />
	
	<div>Overall Rating: ' . $arr1['rating'] . '</div><br />'."\\r";	
	
	if(count($articles[$article_id]['comments'])>0):
		foreach($articles[$article_id]['comments'] as $comment_id => $arr2):
		
			echo '<div><a href="?article_id=' . $article_id . '&amp;comment_id='. $comment_id . '">#' . $cnt . '</a><br />
			<div>' . $arr2['comment'] . '</div><br />
			
			<div>Comment Rating: ' . $arr2['rating'] . '</div>
			
			<form id="ratingform_' . $cnt . '" action="" method="post">
			<input type="hidden" name="comment_id" value="' . $comment_id . '" />'."\\r";
			
			foreach(range(1,5) as $r):
				echo '<div style="float:left; text-align:center; margin-right:5px;">' . $r . '<br /><input type="radio" name="rating" value="' . $r . '" /></div>'."\\r";
			endforeach;
			
	        echo '<input type="submit" name="addrating" value="Add Rating" />
	    	</form>'."\\r";
			
			echo '<form id="commentform_' . $cnt . '" action="" method="post">
			<input type="hidden" name="comment_id" value="' . $comment_id . '" />
			<textarea name="new_comment"></textarea><br />
			<input type="submit" name="addcomment" value="Submit" />
	    	</form>'."\\r";
			echo '</div>'."\\r";
			
			$cnt++;
		endforeach;
	endif;
endforeach;
?>

</body>
</html>

This version has one POST NEW comment form and rating by each comment.

<?php
//echo "<pre>";
//print_r($_POST);
//echo "</pre>";
?>
<html>
<body>
<?php
$articles = array(
	'1' => array(
		'name' => 'Debbie',
		'article_name' => "Debbie's Traits",
		'article' => "Please list my Traits",
		'rating' => '4',
		'comments' => array(
			'14' => array(
				'comment' => 'Smart',
				'rating' => '4'
			),		
			'22' => array(
				'comment' => 'Thorough',
				'rating' => '5'
			),			
			'23' => array(
				'comment' => 'Detailed',
				'rating' => '3'
			)		
		)
	)	
);
//echo "<pre>";
//print_r($articles);
//echo "</pre>";

//Display
$cnt=1;
foreach($articles as $article_id => $arr1):

	echo '<div><a href="?article_id=' . $article_id . '"><strong>' . $arr1['article_name'] . '</strong></a> <br />
	<span style="font-size:smaller;">By ' . $arr1['name'] . '</span></div><br />
	
	<div>' . $arr1['article'] . '</div><br />
	
	<div>Overall Rating: ' . $arr1['rating'] . '</div><br />'."\\r";	
	
	if(count($articles[$article_id]['comments'])>0):
		foreach($articles[$article_id]['comments'] as $comment_id => $arr2):
		
			echo '<div><a href="?article_id=' . $article_id . '&amp;comment_id='. $comment_id . '">#' . $cnt . '</a><br />
			<div>' . $arr2['comment'] . '</div><br />
			
			<div>Comment Rating: ' . $arr2['rating'] . '</div>
			
			<form id="ratingform_' . $cnt . '" action="" method="post">
			<input type="hidden" name="comment_id" value="' . $comment_id . '" />'."\\r";
			
			foreach(range(1,5) as $r):
				echo '<div style="float:left; text-align:center; margin-right:5px;">' . $r . '<br /><input type="radio" name="rating" value="' . $r . '" /></div>'."\\r";
			endforeach;
			
	        echo '<input type="submit" name="addrating" value="Add Rating" />
	    	</form>'."\\r";
			
			$cnt++;
		endforeach;
	endif;	
	
	echo '<form id="commentform_' . $cnt . '" action="" method="post">
	<input type="hidden" name="article_id" value="' . $article_id . '" />
	<textarea name="new_comment"></textarea><br />
	<input type="submit" name="addcomment" value="Submit" />
   	</form>'."\\r";
	echo '</div>'."\\r";
			
endforeach;
?>

</body>
</html>

Use a hidden field.

Or follow the first example I gave. Either way it will work.