HTML/PHP same page script
Hey, I'm making a script that checks if a specified string matches the one in check.php. Here is my HTML code:
<form name="input" action="check.php" method="POST" id="key">
Key: <input type="text" name="key" />
<input type="submit" value="Submit" />
</form>
开发者_JAVA百科
However, when it runs check.php it goes to the default white page. Is there a way to get the result to display on the page where they typed the information on, like at the top or bottom? Here is my check.php (just for testing atm):
<?php
$key=$_POST['key'];
echo $key;
?>
In the most simple example here, rename your .html file .php. Now paste the PHP script into the body of your old HTML document. Change the action of the form to point to blank (this will send the form to the same document). Now your form doc also outputs the $_POST['key']. If the $_POST['key'] value is empty... you echo nothing. You may get a warning message, but no harm.
You need to incorporate Javascript into the mix here. Look at jquery, and in particular look at the jQuery.get() function.
This allows you to, in the background, retrieve data from the server and then manipulate the page using javascript based on the server response.
Ultimately what you want requires dynamic CLIENT side. PHP is a server side scripting language.
<?php
if(isset($_POST['key']) {
$key=$_POST['key'];
echo $key;
} else {
?>
<form name="input" action="check.php" method="POST" id="key">
Key: <input type="text" name="key" />
<input type="submit" value="Submit" />
</form>
<?php } ?>
Where check.php is the page you start from. Note that I was just using your code to quickly produce what you are looking for. You will probably want to do more than just echo the key and you will likely want to use a more well formatted HTML code.
You don't have any instructions on your check.php page to display anything, so it is going to remain the default white page. My suggestion would be to have your HTML form and the PHP login on the same page, and have it post to itself. You can then check to see if the form was submitted, and do any data validation on the submission that you would like.
Make sure the single page is a php file. Set your form action to point to it.
After (or before, or in the middle of) your html, put something like:
<?php
if (isset($_POST['key'])) {
// stuff that only occurs if the user posted a form
}
?>
<?php
$key = null;
if($_SERVER['REQUEST_METHOD'] == 'POST']) {
$key=$_POST['key'];
}
?>
<form name="input" action="check.php" method="POST" id="key">
Key: <input type="text" name="key" value="'<?php =$key?>/>
<input type="submit" value="Submit" />
</form>
<?php
if(!isNull($key)) echo $key;
?>
You should display your form on your check.php file. You need to display the result of the form only if it exists. How do we do that? Check the isset() function. (http://www.php.net/manual/en/function.isset.php) You will have something like that:
<?php
if(isset($_POST['key'])) {
$key=$_POST['key'];
echo $key;
}
?>
<form name="input" action="check.php" method="POST" id="key">
Key: <input type="text" name="key" />
<input type="submit" value="Submit" />
</form>
精彩评论