(help)sending data to variables on external html file

Greetings! I’m very new to PHP programming and I have just learned how to send data from
input page to another page using form tag, below are my code.

・index.html

heredoc input 1
input 2

・check.php

<?php $a=$_POST['input1']; $b=$_POST['input2']; $display = <<<heredoc
$a $b
heredoc; print $display; ?>

Result

[table]
[tr]
[td][input1][/td]
[td][input2][/td]
[/tr]
[/table]

what I want to achieve is

・I want the html part to be on external file

Then I tried several patterns

pattern #1

・check.php

<?php $a=$_POST['input1']; $b=$_POST['input2']; $display = <<

・check.html









$a $b


Result

include(‘check.html’);

it displays plain text between heredocument function.

pattern #2

・check.php

<?php $a=$_POST['input1']; $b=$_POST['input2']; $ext=include 'check.html'; $string = <<

Result

$a $b
1

it displays table and text of the variable itself, the variable is not converted to data from index.html

and I have no idea why “1” is displayed.

pattern #3

・check.php

<?php $ext='ext'; function ext() { $gaibu = include('check.html'); return $gaibu; } $string = <

Result is the same on pattern #2, based on what I have researched if you want to use function inside heredoc
statement you have to put the function inside a variable and call it with {}

My question is

・how can I pass the data to the variable($a,$b) using external file(include() or require())?
・does external file have to be a php file?
・are there any other better techniques?
・is what I want to achieve possible?

I’ve been reading alot of forums but I can’t seem to solve it, appreciate any help Regards.

Well, actually, this is a bit tricky. (due to the way you are trying to do it.)

So, first, you can not pass variable thru an included “heredoc” file by using the variable names.
Because this is text data. To explain this, you have to understand how PHP actually works.

PHP is server-side only processing. ALL of the PHP code is handled on the server BEFORE
the browser even sees it. The PHP processing is done inline. Therefore, it works down your
code and processes everything in order. Now, include files are loaded from the server and placed
exactly as-is where you place them. They should be processed in order and can include variables.
And, “heredoc” is a text grouping tool. What that means is that it is used to place text into a
variable. So, if you use it, it would be tricky to parse in a variables content as it is text-orientated
and the data is INSIDE a variable. You would have to use string functions to alter the variable’s data.

So, how to fix this. Dump the heredoc code. Create your “pattern#1” version again. But, dump the
“heredoc” and dump the $display variable. Just use the “include” command. It will work great for you.

Here is a new version of your “pattern#1”: (NOTE: Please use the PHP button to insert code next time!)
check.php
[php]

<?php $a=$_POST['input1']; $b=$_POST['input2']; include('check2.php'); ?>

[/php]
And, check2.php
[php]

$a $b
[/php] Also note that I named the included file a .php file so the server knows to process it as PHP not HTML. (This will fill in the variables as they should be.)

Try that and let us know if it does not work or any further questions. I did not test this as short on time!

Good luck…

Good Day Mr.ErnieAlex

Thank you for the explanation of how php works, as you have suggested me the way you recommended
The result is still plain text of the variable name ???

index.html
[php]

heredoc input 1
input 2 [/php]

check.php
[php]

<?php $a=$_POST['input1']; $b=$_POST['input2']; include('check2.php'); ?>

[/php]

check2.php
[php]

<html>
	<body>
		<table border="1">
			<tr>
				<td>$a</td>
				<td>$b</td>
			</tr>
		</table>
	</body>
</html>

[/php]

Result

$a $b

I also tried wrapping check2.php code with <?php ?> but it returns error after clicking “Submit” button,
also tried wrapping the variable with php

check2.php
[php]<?php

$a ?> <?php $b ?>[/php]

but the result was blank page.
what am I doing wrong? :frowning:

Regards.

Actually, the output of your file is correct as you programmed it.
Let’s explain PHP again so you are clear on it.

First, PHP is only SERVER-SIDE code. It does not exist after PHP sends the output to the browser.
Therefore, you can NOT use any PHP code inside of the browser. Now, in your routines, you use a
second file called check2.php. I forget why we needed a second file. Now, when programming PHP,
you can combine both HTML, PHP and Javascript on one page. But, you must remember that all of
the PHP processing is handled on the SERVER. Then, it is all shipped off to the browser where the
HTML and Javascript takes over. Also, remember that the OUTPUT of the PHP can be HTML or JS or
tables, just about anything you need to display. (But, the code itself goes away before placed inside
the browser.

Now what does that mean for your example. You have an index page in simple HTML which calls a
PHP scripted page named check.php. Normally inside that page you would combine PHP and HTML
to check various inputs from the origial index.html page and would handle output based on them.
Or, you can leave it as-is if you wanted these two pages separate. That is your decision. I will give
you samples of how each would work below.

Before showing you examples, please note inside of your second PHP file, you have $a and $b. This
is inside code that is designed as HTML not PHP. So the $a is just two characters to be shown on the
page and not a PHP variable. ALL PHP code must be inside PHP tags. Therefore to display a variable
in the middle of HTML, you would need to use something like: <?PHP echo $a; ?> to print out the $a
variable.

So, a simple fix for your second check file would be to change the two lines where variables are shown:
check2.php (new version)
[php]

<?PHP echo $a; ?> <?PHP echo $b; ?>

[/php]
Please note, that the PHP code will output the value inside of the variables $a, $b, not the text of them.

To combine the two check files into one to make it all much easier, you can use this format. Basically,
you would only have one check file in this manner:
check.php (Combined with check2.php)
[php]

<?php $a=$_POST['input1']; $b=$_POST['input2']; ?>
<?PHP echo $a; ?> <?PHP echo $b; ?>

[/php]
Remember that you can combine PHP, HTML, CSS and Javascript into one page. You just have to be
careful to remember that PHP is executed SERVER side and the rest is handled in the browser.

Hope that helps. Good luck…

Good day Mr.ErnieAlex

Thank you for you advise it helped me alot!(it works) I missed the “echo” part on html. ;D

Great! Always a nice feeling when a project works…

Come back if you have have a new problem.  I will mark this one solved...
Sponsor our Newsletter | Privacy Policy | Terms of Service