1

So let's say I have a PHP script which takes the POST data out of a HTML form, like this:

<?php
$data = $_POST['data'];
echo $data;
?>

And I have a HTML form in a PHP file, when the button is pressed it sends the data to the other PHP file, and the data will be echo'd, now I want the data to be echo'd in the file that has the form, not in the single PHP file. I hope I explained correctly. I know I can put the HTML and PHP in one file, but I don't really want to do that.

EDIT: OK, this is a better explanation, I need to take the data that's POSTED to a PHP file and echo it in another PHP file, understand now? I hope so.

10
  • 2
    You'll be better off using AJAX for this. Ajax will post html forms to php files and then you can determine what is then displayed back to the user within the success function in the ajax call Commented Nov 16, 2013 at 17:15
  • 1
    I read this question like 5 times and i still don't understand what you really want to achieve. Commented Nov 16, 2013 at 17:16
  • Have you looked at include and require functions and variations? Commented Nov 16, 2013 at 17:17
  • How about I use $_GET then file_get_contents? Would that work? Commented Nov 16, 2013 at 17:18
  • It's better to use AJAX, it does exactly what you are asking Commented Nov 16, 2013 at 17:18

2 Answers 2

3

first method: use output buffer

example file1.php

<?php echo $_POST['data']; ?>

example file2.php

<?php 
ob_start();
require "file1.php";
$output1 = ob_get_clean();
?>

read more about output buffers and nesting them: https://www.php.net/manual/en/ref.outcontrol.php


second method: use return

example file1.php

<?php return $_POST['data']; ?>

example file2.php

<?php 
ob_start();
$output1 = require "file1.php";
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Yea I know that, but the data is dynamic as it's a post method.
0

If you use AJAX you will be able to easily accomplish what you are asking for. Your front end form will post to a php form and then you can return the values you like inside the AJAX success function. A similar question has been asked here and the answer explains how to use it well - Ajax tutorial for post and get

4 Comments

I'll take a look at that, although I'm not experienced in AJAX, thanks!
cleverweb.nl/php/jquery-ajax-call-tutorial - this might help you out a bit. Effectively you set up an ajax call on a form on your page, send that data to your php file which you can read as normal POST data and then set that data to variables, echo it out into the document and the ajax can then show it on success. Theres a bit more to it, but highly suggest learning it :)
Hmm, nice, thanks, I sure will, is there's a way to do it with pure PHP?
There is absolutely no need to do it with just php when you can do it in AJAX easier. In php you will need to post to the same page as the html code is and then go from there. To do reading the contents of a file on post isn't as good as AJAX. AJAX is your best solution for this issue

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.