Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a php page which contains a form with a text box and a submit button, i want to display any message if someone enters 'hello' in the textbox and submits it. I have made a similar page with both php and html on the same page, but when i load the php page, it shows the hello message as the page loads and i want it to display it then i submit the form. Following is my code.

PHP
if($_POST['txt'] == 'hi')
{
echo helo;	
}
else 
echo what;



<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="this.php" method="post">
<input type="text" name="txt" value="" />
<input type="submit" value="submit" />
</body>
</html>

This page displays the 'what' message as the page loads which i don't want to load first. Both php and html code is on the same page
Posted
Updated 11-Aug-11 2:57am
v4
Comments
reid0588 11-Aug-11 9:17am    
can you not put a label onto your page. the "pseudo" code would be something like

on button.click

label.text=textbox.text

There is a number of ways to do it. You simply need to tell the page loaded before post and as a result of the post. Of course what you write displays "what" as $_POST["txt"] returns empty string — look at your HTML.

One very simple method would be using different URLs — with URL query string. Suppose your page URL is "http://mydomain.org/this.php". In your form, use:

HTML
<form action="this.php?posted" method="post">


Now you can tell the posted form page a from the same page loaded using original URL:

if (QUERY_STRING == 'posted')
   echo "Thank you for posting your information";
else
   echo "Hello! Please fill in your data and press 'Post'";


—SA
 
Share this answer
 
v2
First this is the code u informed in the HTML

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
 
<body>
<form action="this.php" method="post">
<input type="text" name="txt" value="" />
<input type="submit" value="submit" />
</form></body>
</html>




This is the php Code of this.php

PHP
if(isset($_POST['txt'])) { ?>
<html>
<body>
Your message has been sucessfully submitted Thanks.
</body>
</html>



<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
 
<body>
<form action="this.php" method="post">
<input type="text" name="txt" value="" />
<input type="submit" value="submit" />
</form></body>
</html>



Explain

The
PHP
if(isset($_POST['txt']))
checks whether the txt is submitted if yes then show the block inside the if statement else shows the else statement


This was just a simple explanaion!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900