Jump to content

Multiple forms in a loop.


SamohtVII

Recommended Posts

Ok so I have a php script that gets all posts a user has created and prints them out. After each post is printed it has a form underneath to add a comment. This forms gets printed for all posts and it seems it can't differentiate between each. No matter what submit button i press it always wants to use the data, usually empty, that the last printed form has in it. Is there any way I can get a specific form to print out the data it has in it.Maybe there is a better way to do it but this is so close to working for me. so it would be [COMMENT AREA 1] [COMMENT AREA 2] <---comment: "this is a comment"------but would submit COMMENT AREA 1's data i.e. nothing [COMMENT AREA 3]

<?php /*print postswhile more posts{  */   ?><div id="commentID">    <form method="post" name="form">   <i>Name: </i><input id="name" type="text" name="name">   <i>Comment: </i><input id="comment" type="text"   name="comment" size="50">   <input type="submit" value="Submit" class="submit">   </form>	<div id="commentArea"><?php    /*Print comments*/    ?><?php}?>

ps: I have being doing php and this kind of stuff for a few weeks now so speak slowly. THanks

Link to comment
Share on other sites

Each comment area should have its own form tag so I don't see why it's giving data from a different form. Be sure that all your <form> elements are properly closed and in the right place. If you want to make sure the comment is added to the correct post add a hidden input to each form indicating the post it belongs to:

<input type="hidden" value="<?php echo $post_number; ?>" name="post"> 

When you add data to the comments database table:$post = intval($_POST['post']); INSERT INTO comments (post, user, other data ...) VALUES ($post ... ... )

Link to comment
Share on other sites

My form seems to be properly opened and closed and I do have an ID to link posts with comments:

<textarea id="formid" name="formid" hidden="true"><?php echo htmlentities($row['ID']);?></textarea>

That's how I found out about the problem. it will post just the ID to the table and no data. And it's always the last ID to be printed i.e. COMMENT AREA 1.I thought it might not be possible to loop a form with php but if you say it is then that's weird.

Link to comment
Share on other sites

I also forgot to mention which might be important is this.... :

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script><script type="text/javascript" >$(function() {$(".submit").click(function() {var formid = $("#formid").val();var name = $("#name").val();var comment = $("#comment").val();var dataString = 'formid=' + formid + '&name=' + name + '&comment=' + comment;if(formid=='' || name=='' || comment==''){  $('.success').fadeOut(200).hide();  $('.error').fadeOut(200).show();}else{  $.ajax({  type: "POST",  url: "comment.php",  data: dataString,  success: function(){  $('.success').fadeIn(200).show();  $('.error').fadeOut(200).hide();  location.reload(true);}});}return false;});});</script>

Just so it refreshes the page on a post. So all the submit buttons have the same id etc.

Link to comment
Share on other sites

OK, the problem is that Javascript is getting the data from an element with id "comment"

var comment = $("#comment").val(); 

You're going to have to give each textarea (and all your inputs as well) its own unique ID, then have Javascript work differently for each form. IDs cannot be used more than once in a document. You can't have two or more elements with id="comment"

Link to comment
Share on other sites

Ok. All I want the script to do is refresh the page. Is there a way i can tell the form to send it to comments.php i.e. action="comment.php" then have the submit button refresh the page? So it doesn't go through the script to send the data to the comment.php page. If not I have little to no idea how I would go about doing what you described. Ok here's what i got baring it's all possible to do in html. Stop me if it's nonsense. is it possible to say id=idfunction() or better yet id=<?php echo htmlentities($row['ID']);?>;If i can set the id to just the ID number they will all be unique. Then i can start at 1 and go through numbers until I hit that ID number then process that id's form? Thanks for the help. I'm so close yet so far.

Link to comment
Share on other sites

If you want the page to refresh, why use AJAX? If you remove the Javascript the form will submit loading another page. You might have to change the PHP script so that it redirects back when it's done.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...