Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I handle multiple forms on the same page with dynamic fields names when I will not know them ahead in my php processing file
    primarykey
    data
    text
    <p>This issue may sound stupid but believe me, I have been searching on the web and it seems not many people have ever met such a problem. Think about a situation like you have users updating a page with messages or pictures exactely as it is on facebook or google plus.</p> <p>Although this sound simple as one may think it is about inserting form variables received from $_POST, it is a little bit tricky. First, the browser send all different forms available on the page.</p> <p>Let's say there have been 7 picture posts with messages.When the page refresh from the last posts, it will show under each picture/message a form with dynamically generated fields names, like "user_id_$user_id" where $user_id is the number of row from the user table in the data base.Another field name may look like picture_$picture_id where $picture_id is the number of row from the picture table in the data base. Now as you can see I can't know ahead which form will be send with which variables.</p> <p>There should be a number of solutions.</p> <p>using a loop and go through the $_POST array</p> <p>The problem is all available forms on the page are send with prefilled variables and an empty variable (the message)!only the one clicked will have all variables since that one will have been filled by the user, which makes it difficult to find the ones with data, and beside as I don't know neither the names of the variable nor the form name how can I process this to insert them in my data base.</p> <p>The other solution I tried is using field names as array like Then I reiceve array of values. This is a step ahead since I would find data at the exact positions whichever the form send. The problem remains of the other junkies that would be send from the other forms.</p> <p>I even tried using a pop up and I was unable to pass fields names to the loaded htmlform file.The same using AJAX to load an html form file </p> <p>It could have been simpler for me if the browser was not sending all the available forms with junky variables that I don't use and I think this is an area that needs to be adressed by browser developpers.Normally, the clicked form should be the one being sent, but this is not the case.Remember I can't know the name of the form or the name of fields ahead.</p> <p>So please If I make sense, can you find me where I went wrong? or is there any work around? I know it is possible but I have no idea how.</p> <p><em>Code added from @Staulen Green's comments below:</em></p> <pre><code>&lt;?php do { ?&gt; &lt;form action="processor.php" method="post" name="form_&lt;?php echo $row_Rec_blogstatus['id']; ?&gt;" id="form_&lt;?php echo $row_Rec_blogstatus['id']; ?&gt;" &gt; &lt;input type="text" name="comment[]" size="32" value="" /&gt; &lt;input type="submit" name="go" value="Enter" /&gt; &lt;input type="hidden" name="comment[]" value="&lt;?php echo $row_inside_names['Id']; ?&gt;" /&gt; &lt;input type="hidden" name="comment[]" value="&lt;?php echo $row_Rec_blogstatus['id']; ?&gt;" /&gt; &lt;/form&gt; &lt;?php } while ($row_Rec_blogstatus = mysql_fetch_assoc($Rec_blogstatus)); ?&gt; </code></pre> <p>As you see the number of forms on the page depend on the number retourned by $row_Rec_blogstatus. On submission of one of the form, the other forms will be submited. This what I am seeing:</p> <pre><code>comment%5B%5D=Hello kity&amp; go=Enter&amp; comment%5B%5D=1 &amp;comment%5B%5D=124&amp; comment%5B%5D= &amp;comment%5B%5D=1 &amp;comment%5B%5D=123&amp; comment%5B%5D=&amp;comment%5B%5D=1&amp;comment%5B%5D= 122&amp;comment%5B%5D=&amp;comment%5B%5D=1&amp;??comment%5B%5D=121&amp;comment%5B%5D=&amp;comment%5B%5D= 1&amp;comment%5B%5D=120&amp;comment%5B%5D=??&amp;comment%5B%5D=1&amp;comment%5B%5D=119&amp;comment%5B%5D= &amp;comment%5B%5D=1&amp;comment%5B%5D=1??18&amp;comment%5B%5D=&amp;comment%5B%5D= </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload