Which button does PHP choose when they have the same name?

I'm trying to make a very simple blog with php. My code snippet just generates a tile,text and the author plus an Edit button. Because I'm working with the data from an array, I read it with a for. Now when I want to edit a post it should take me on the site editpost.php but how do I mark which button belongs to which blogpost? Do I need to generate a new form for every new post?

  echo '<form id=\"myform\" name=\"myform\" method=\"GET\">';
for ($i = count($liste) ; $i >= 0 ; $i--){
    echo '<p>';
    echo '<h2>'.$blogs[$i][0].'</h2><br>';
    echo $blogs [$i][1].'<br>';
    echo 'Autor:'.$blogs [$i][2].'<br>';
    echo '</p>';
    if($_SESSION['email'] == $blogs[$i][2]){
    echo '<input type=\'submit\' name=\'updatepost\' value="Edit Post">';}
    if (isset($_GET['updatepost'])){
        header ( 'Location: editPost.php' );
echo '</form>';


PHP doesn't "choose" anything. They're processed strictly in the order that they appear in the HTTP request, from first-to-last.

Browsers USUALLY send things in strict html definition order - the order that fields appear in the html are the order they'll be sent out in.

That also means that if you send multiple fields with the same name, you'll end up with the value of the LAST of those duplicate fields. Which is why PHP has the [] naming hack. Note the following (not totally valid) HTML:

<input name="foo" value="bar" />
<input name="foo" value="baz" />
<input name="abc[]" value="def" />
<input name="abc[]" value="ghi" />
<input name="pqr[stu]" value="vwx" />

If this gets submitted to a PHP script, you'll end up with the following:

$_POST = array(
   'foo' => 'baz'   // note: baz is the LAST value in the form with this name
   'abc' => array(
       0 => 'def',
       1 => 'ghi'
    'pqr' => array(
       'stu' => 'vwx'

I see two easy solutions for this:

  • Change your <input> to a simple link and append a query string, like this: echo '<a href=\'editPost.php?id='.$i.'\'>'; In this case you don't need the entire <form> tag.
  • Include a hidden <input> containing the id, like this: <input type="hidden" name="id" value="$i">. As @VIDesignz says, in this case you have to move the <form> tag into the for loop to create a separate form for each post.

I would say the first way is easier, since what you are having here is no real form (with input fields) anyway.

You can also pass a value to each submit like this:

echo '<input type=\'submit\' name=\'updatepost['.$i.']\' value="Edit Post">';}
if (isset($_GET['updatepost'])){
    header ( 'Location: editPost.php?id='.key($_GET['updatepost']) );

Need Your Help

No Play Store update after directing test app to Market url

android google-play

I'm testing a version check feature in our app. Our Android client queries data from our servers that returns the minimum client version we will allow to access our service. Users running older ver...

Handy F# snippets

f# reference code-snippets

There are already two questions about F#/functional snippets.