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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user2917204
  • 1,243
  • 2
  • 9
  • 11

2 Answers2

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: http://us2.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";
?>
jko
  • 2,068
  • 13
  • 25
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

Community
  • 1
  • 1
Andy Holmes
  • 7,817
  • 10
  • 50
  • 83
  • I'll take a look at that, although I'm not experienced in AJAX, thanks! – user2917204 Nov 16 '13 at 17:24
  • http://www.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 :) – Andy Holmes Nov 16 '13 at 17:26
  • Hmm, nice, thanks, I sure will, is there's a way to do it with pure PHP? – user2917204 Nov 16 '13 at 17:31
  • 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 – Andy Holmes Nov 16 '13 at 17:35