3

I have a action.php which does some processing involving a MySQL-Database. In action.php I generate a variable

$author

with a string in it. When the script terminates it calls test.php by

header('Location: ../test.php');

Now while test.html is shown, I want to display the content of the stringvariable

$author

in a html-element. Like

<h2>echo $author;</h2>

How do I achieve that? Thank you for any responses in advance.

4
  • Can you post exactly how you are displaying test.html? I'm not sure what you mean by "it calls test.html". Commented Apr 4, 2011 at 21:33
  • php.net/manual/en/book.session.php Commented Apr 4, 2011 at 21:33
  • @TaylorOtwell Thanks for replying. I edited the part you asked for. Is it more clear now? Commented Apr 4, 2011 at 21:36
  • @Capsule test.php Sorry for type error. Commented Apr 4, 2011 at 21:39

6 Answers 6

1

You could put the contents of the $author in a session:

<?php
// action.php
session_start();
// Your code here

$_SESSION['author'] = $author;

// Redirect to test.php

<?php
// test.php
session_start();

echo '<h2>'. $_SESSION['author'] .'</h2>';

See:

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

1 Comment

You saved my day. Didn't thought, that sessions were so easy to deal with. Thank you very much.
1

you could store $author in a session variable or on the action.php page output a form with a hidden input with the value of $author and then submit it to test.php

to use session variables don't forget session_start(); and then $_SESSION['author'] = $author

Comments

1

In your action.php save your variable in session like this: $_SESSION['author'] = $author;

Then, in your test.php file you can use <h2><?php echo $_SESSION['author']; ?></h2>

Don't forget to start both .php files with calling session_start();

Comments

0

Use a template engine, e.g. Twig.

While you can use PHP itself as a template engine (include() your file and use <?=$var?> or <?php echo $var; ?> in it), using a real template engine is usually nicer as you won't even think about moving actual business logic into your templates when you have a good template engine.

Comments

0

You'll need to either store the value of $author in $_SESSION or in a cookie.

See session.php

1 Comment

Yes but be careful, you can't send a Set-cookie AND a Location header in the same batch so the session should be started in a script before the one sending the Location header.
0

Sounds like you are trying to pull author data from a database and display it in test.php. If so there is no need to "pass" the data to test.php, just grab data in test.php.

test.php

<?php
// Open DB handle
// Do query, get results.
// Store results in array ($aRes)
// Close DB handle
?>

<html>
.
.
.
  <body>
  <h2><?php echo $aRes['author'];?></h2>
  .
  .
  </body>
</html>

That is a very basic and obviously fairly pseudo template but hopefully it will give you a better idea of the relationship between server-side data and HTML.

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.