0

I came across someone asking the question,

How can I pass a variable up the page (on the same page?).

I had a think about it but couldn't think of how to do it myself, so I was wondering if it is even possible?

So what he was trying to do was change the value of $a at the top of the page at the same time as the bottom value of $a.

Is it possible? If so how?

Thanks in advanced.

<?php
echo("Begining " . $a);
?>
<html>

<head>
<title>Test Variables</title>
</head>

<body>
<form>
<form action="<?=$PHP_SELF?>" method="POST">
<input type="TEXT" name="testf" size="10">
<input type="submit" name = "submit" value = "submit"></form><?php
if ("submit" == $submit) {
$a = $testf;
echo( "Bottom " . $a);
}
?>
</body></html>

Edit:

After seeing answers, maybe it can be done with jquery, ajax or javascript?

Sam Ham
  • 111
  • 2
  • 16

4 Answers4

1

No, you'll need to move the if statement to the top, and any variables you calculate that need to be in the if statement also to the top.

Something like:

<?php
if ("submit" == $submit) {
    $a = $testf;
}
echo("Begining " . $a);
?>
<html>

<head>
<title>Test Variables</title>
</head>

<body>
<form>
<form action="<?=$PHP_SELF?>" method="POST">
<input type="TEXT" name="testf" size="10">
<input type="submit" name = "submit" value = "submit"></form><?php
echo( "Bottom " . $a);
?>
</body></html>

It's considered a good practice to have all the logic before you start outputting the HTML anyways. Your HTML should ideally have as less logic as possible.

More info: https://stackoverflow.com/a/95027/320615 and https://stackoverflow.com/a/1088791/320615

Community
  • 1
  • 1
Dogbert
  • 212,659
  • 41
  • 396
  • 397
  • I understand, but lets say I couldn't move that to the top of the page, how could I then do it? – Sam Ham May 17 '13 at 09:03
  • @SamHam, you can use [`ob_start`](http://php.net/ob_start), but that would make the code even more unreadable. – Dogbert May 17 '13 at 09:07
1

You can probably bend over backwards to make that work somehow.

But the real answer is to handle all your business logic before you start outputting any HTML. You need to decide at the beginning of your code whether the current request is a form submission or not and set variables and HTML templates accordingly. Never mix business logic into the middle of your HTML templates.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • This seems to be the best answer in this thread so far. @Sam Ham. You should give this answer a try instead of trying to hack around with bad practice workarounds. – thpl May 17 '13 at 09:25
0

You have to use the $_POST['testf'] and $_POST['submit'] variable.

And also check if it exists with isset : php manual isset

<?php
echo("Begining " . $a);
?>
<html>

<head>
<title>Test Variables</title>
</head>

<body>
<form>
<form action="<?=$PHP_SELF?>" method="POST">
<input type="TEXT" name="testf" size="10">
<input type="submit" name = "submit" value = "submit"></form><?php
if ("submit" == $submit) {
$a = $_POST['testf'];
echo( "Bottom " . $a);
}
?>
</body></html>
PeterFour
  • 329
  • 4
  • 16
-1

You can't do it in PHP, but you can't change HTML once it's fully loaded using javascript (and jquery to make it easier).

EDIT : ok I read your code a bit quickly the first time : I don't understand the echo before the <html> tag, doesn't seem right. Also you want to give the value of a POST var to $a, so just :

if(isset($_POST['testf'])) {
    echo $_POST['testf'];
}
Yoh
  • 316
  • 3
  • 9