1

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>

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;
?>
2
  • You need to look up AJAX Commented Mar 8, 2011 at 20:57
  • 1
    Also, you should always parse any data you receive via POST/GET to remove malicious code. Commented Mar 8, 2011 at 20:58

7 Answers 7

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I realize this may be a little confusing, so let me know if this needs more explnation.
That's alright, I understood it and it works perfect. I don't know why I didn't think of that. Thanks.
This kind of programming involves thinking of things in different ways than building web pages or writing compiled code. I'm glad I could help.
0

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.

Comments

0
<?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.

2 Comments

I would move $key=$_POST['key']; inside of your if() statement, to avoid any Notice messages.
@jnpcl Indeed, I was merely copying and pasting the OP's code together as they pretty much had it already.
0

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.

Comments

0

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

}
?>

Comments

0
<?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;
?>

1 Comment

You should check that $_POST['key'] exists with isset(), otherwise it will throw a Notice if it's not.
0

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>

1 Comment

missing a closing ) on your if() statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.