tags:

views:

59

answers:

3

Hello all,

Please have a look to the following code:

<?php

  $nomeDominio='';

  if (isset($_GET['infoDominio']))
  {          
      $nomeDominio = $_GET['nomeDominio'];
      echo "I'm getting ".$nomeDominio;
  }

  if (isset($_POST['atualizarDominio']))
  {
      echo "I'm posting ".$nomeDominio;
  }

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Test Case 99</title>
    </head>

    <body>

        <form name="infoDominio" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>"  method="get">

            <input id="nome_dominio" type="text" name="nomeDominio" value="<?php echo $nomeDominio; ?>"/>
            <br />
            <button name="infoDominio" type="submit">Obtem informacao</button>

        </form>

        <form action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" name="atualizarDominio" method="post">

            <input type="hidden" value="<?php echo $nomeDominio ?>" name="nome-dominio"/>
            <br />
            <button type="submit" name="atualizarDominio">atualizar domínio</button>

        </form>

    </body>

</html>

You can copy/paste - it will serve as test case.

Like this, IF we get and then we post: The value from GET WILL NOT pass into POST.

The thing is: If we just change the action= property of the second form element to, instead of having the $_SERVER['PHP_SELF'], to have just action=""; you will notice that the value WILL pass.

My question is: Why?

ADDITIONAL NOTE: This is not something to solve. Instead, this is something to understand why is it happening this way. Why, if we change the action on the second form to action="", the value stored in $nomeDominio pass from one conditional into another? The code sample can be used by itself, so you can perfectly test this very easily and see what I'm talking about.

+1  A: 
 {          
      $nomeDominio = $_GET['nomeDominio'];
      echo "I'm getting ".$nomeDominio;
  }

  if (isset($_POST['atualizarDominio']))
  {
      $nomeDominio = $_POST['nomeDominio']; //THIS HERE
      echo "I'm posting ".$nomeDominio;
  }

you are missing the line with comment THIS HERE

You wanted to pass the _GET['nomeDominio'] from the first form to a hidden field of the second form right? Then when we submit the SECOND form you echo nomeDominio's value again (from the second form's hidden field). You where missing and assignement in the $_POST: $nomeDominio = $_POST['nomeDominio'];

There you go. If you do not undesrtand I do not know how to say differently.

Iznogood
It's the name of the form. You can freely copy paste this code so see it for yourself.
MEM
I meant its because of that it fails. Try my fix.
Iznogood
@Iznogood - I will try right a way. In the meanwhile, what should I expect? What is a "fix" on this case?
MEM
I was mistaken. Look at my answer the solution is there you where missing an assignment.
Iznogood
@Iznogood - I test your previous suggestion, and I get the same results. I will now test your new one. :)
MEM
New one will work I am sure of it. You where never assigning $_POST['nomeDominio'] in the second form.
Iznogood
@Iznogood - that will work yes.. But it's defeating the all propose. The point is to pass a value from the GET conditional into the POST conditional. And what I'm stating is that: if we change the form action to action="" on the second form, it will do that. And I'm wondering why. :??
MEM
Well you are doing that. From the GEt to the hidden field back to the POST.
Iznogood
Sorry, didn't understand your answer... can you please elaborate. I'm fairly newbie on those matters. :(
MEM
Check ehe end of my answer.
Iznogood
@Iznogood: You wanted to pass the _GET['nomeDominio'] from the first form to a hidden field of the second form right? NO. :) I understand you perfectly. :) The issue is solved. My question was: "why am I having this" and NOT "how can I solve this". Normally however, they get mixed, but that's not my fault. Still, I found half of the answer I was looking for. ;) I was having this issue, because the FORM name had the same as the button name, leading to inconsistencies. However, that is not a fully answer. But I would take it. :s
MEM
@MEM oh well. Thats a argument to formulate yoru question better next time I guess. If I helped you accept my answer or give me a point. If you dont think I deserve it its ok too.
Iznogood
Sure you deserve Iznogood. The point is given. You have help me thinking on new ways to formulate this question, and dialectic always helps. :)
MEM
@MEM your welcome then friend :)
Iznogood
A: 

You are being inconsistent. The top form uses nomeDominio for the element name, where as the bottom form uses nome-dominio. My hunch is that is why one shows up and the other does not, you are accessing the wrong name.

EDIT

Further elaboration:

if (isset($_POST['nomeDominio']))
{
    echo "I'm posting ".$_POST['nomeDominio'];
}

Replacing that code, and assuming you chose the nomeDominio for the name, that should work.

Brad F Jacobs
Thanks premiso. I'm not following however. Please have patience. I have put the same name on both. The "issue" remains exactly the same. You can copy/paste the code to see it.
MEM
@premiso - Please have a look to my additional note on my question. Thanks a lot.
MEM
A: 

If I'm understanding you correctly, you want to be able to propagate the $_GET value even through a POST method. You can do this by appending the query string to the action attribute of the second POST form:

<form action="<?php echo htmlentities($_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'] );?>" name="atualizarDominio" method="post">

EDIT: Ok, I think I understand a bit better.

In the first case, (with the second form action as $_SERVER['PHP_SELF']), you are forcing the form to post the data to the page without all the added $_GET data (if you look at the URL, the $_GET data is appended to the file name after the ?), so when you look for $_GET['infoDominio'], it doesn't exist any more, and therefore $nomeDominio is still set to an empty string. When you send the POST form, the $_POST['atualizarDominio'] IS set, and you get the I'm posting message, but with no value set in $nomeDominio.

Now when you change the action of the second form to "", you are telling the browser to send the user to the same page you were just on, which includes all the $_GET data in the URL (check it - you find the ?nomeDominio=whatever&infoDominio= in the address bar still). When you submit the second form after having submitted the first form, all the $_GET data is propagated, and so $_GET['infoDominio'] is set, $nomeDominio is assigned whatever value you put in the first form, and thus shows up in the page after submitting the second form.

The fact that the form name and the submit button name are the same shouldn't affect it.

If I'm still misunderstanding what you're asking, please let me know. Otherwise I hope this helps.

Aether
No you are not understanding me correctly. :) I don't want to SOLVE anything. I would like to just UNDERSTAND why it behaves like this. Have a look into Additional Note on the question. :)
MEM