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?

1
  • You can use references, but if you echo something using variable value, it will not change in that string, of course. Commented May 17, 2013 at 8:57

4 Answers 4

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

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

2 Comments

I understand, but lets say I couldn't move that to the top of the page, how could I then do it?
@SamHam, you can use ob_start, but that would make the code even more unreadable.
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.

1 Comment

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

Comments

-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'];
}

1 Comment

Would you know how or what to search for?

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.