Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

forms

Status
Not open for further replies.

byleth

Programmer
Joined
Feb 27, 2004
Messages
70
Location
PT
hi,

i have this simple form, the file is named 'home.php':

<html>
<header>
<title>Home</title>
</header>

<body>
<form method = post action = 'home.php'>
<input type = text name = 'test'>
<input type = submit name = 'submit'>
</form>
</body>

</HTML>

<?php

if ($_POST["submit"]) {

echo $_POST["test"];

}

?>



As you can see the 'action' of the form is itself but it doesn't work. If i put action='another.php' and put the above php code in this 'another.php' file it works.

I would like to stay in the same page with the form and the result of clicking the submit button, is there a way to do that?

Thanks.



 
You are falling into a trap here:
The submit button which you have has no value attribute, hence, the browser displays whatever is the default for that specific browser. Set the value to "Submit" or whatever you want and it will appear when you POST the script to itself.
You can inspect all the posted vars like this:
Code:
<html>
     <header>
         <title>Home</title>
     </header>
     
     <body>
         <form method = "post"   action = "home.php">
         <input type  = "text"   name   = "test">
         <input type  = "submit" name   = "submit" value = "CLick here">
         </form>
     </body>
     
</HTML>

<?php
print_r($_POST);
if ($_POST["submit"]) {
  echo $_POST["test"];
}
?>
It is advisable (in terms of standard compliance) to double quote all the attributes in HTML.
 
may be this is stupid, but are you issuing an "echo" out of <body></body>?

this is your code:
</body>
</HTML>


<?php

if ($_POST["submit"]) {

echo $_POST["test"];

}

?>
 
If your form and script are the same page, instead of calling it by its name (home.php) - try this:

action="$_SERVER[PHP_SELF]"

Try it and let us know!
 
I believe his/her echo command outside the HTML is just meant to show the availability of the posted values. Most browsers will show the output correctly.
 
I didn't know that DRJ478, in fact I never tryed it... It's good to know that.
 
I put a hidden field called postback with a default of 1 e.g.
<form ...>
<input type="hidden" name ="postback" value = "1">
</form>

If the form is new which it will be on the first invocation the $postback variable wil not be available so you know it's a new page. Subsequent submits of the page will have it available with a value of 1
 
CTRL+F5 could be your friend here ifyoare running IE on XP and having trouble seeing your changes take effect.

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 

DRJ478 was right, i forgot to set the value of the submit button, tx:)
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top