0

I am fairly new to php and have only written basic post email returns. However what im trying to achive now is to return the data held within a variable back to the webpage in a specific location on the page.

So far I have a form that collects the data Name & Surname, this is collected by php.

$firstnameField = $_POST ['name'];
$surnameField = $_POST ['surname'];

All I want to do is print the data within these variables back to the screen in a specific location on the page. I have looked around but others are talking about databases and ajax, and I havent the slightest about that stuff.

Thanks in advance.

9
  • There are thousands of examples of this at php.net - did you try looking there? For posterity: php.net/manual/en/tutorial.forms.php Commented May 3, 2013 at 19:26
  • try to store these values in session and retrieve $_SESSION['name'] where you want Commented May 3, 2013 at 19:27
  • I did'nt know this page existed ill check it out. :) Commented May 3, 2013 at 19:27
  • how did you posted the name,surname? via form? javascript? Ajax & Jquery is a must in dynamic html manipulation, learn them. Commented May 3, 2013 at 19:28
  • I see PHP.net is a usefull link however i need the code to run server side rather than client side. is there a way to do this in a php script file ? Commented May 3, 2013 at 19:32

2 Answers 2

3

It's pretty simple: just use echo/print inside a <?php ?> code block wherever you want that variable printed in your document:

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $firstname = $_POST['name'];
}

?>

<html>

<body>

<?php if (isset($firstname)) { echo "<p>Hello, $firstname</p>"; } ?>

<form method="POST">
Enter a name: <input type="text" name="name" />
</form>

</body>

</html>
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you this looks perfect ill implement it and give it a try! :)
I cant seem to get this working its just returing the actual php code
if you see php code in your browser, then your server's misconfigured. and of course, this script has to be served up a webserver. your browser cannot run php directly.
Ahh I got it! :) All working now. I'm a very happy bunny! thanks for all your help everyone!
0
<?php
if ((isset($_POST["dataEntered"])) && ($_POST["dataEntered"] == "Yes"))
echo "Hello! " . $_POST["name"] . " " . $_POST["surname"];
else
{
?>
    <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" name="form1" id="form1">
        <table>
          <tr>
            <td>Name:</td>
            <td><input name="name" type="text" id="name" value="" size="32" /></td>
          </tr>
          <tr>
            <td>Surname:</td>
            <td><input name="surname" type="text" id="surname" value="" size="32" /></td>
          </tr>
          <tr>
            <td colspan="2"><input type="submit" value="Send Form" /></td>
          </tr>
        </table>
        <input type="hidden" name="dataEntered" value="Yes" />
      </form>
 <?php
}
?> 

Comments

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.