views:

239

answers:

2

I'm using file_put_contents() to post text from a text field. I want to post the data to a specific part of the page.

file_put_contents('filename.php /* #fooid */', $formData, FILE_APPEND);

Is there a way to add an id to post to?

Thanks in advance.

+1  A: 

Until I get clarification, here's what I think you might be needing:

<div>
    <div id="something">
        Something else here
    </div>
    <div id="fooid">
        <?php echo file_get_contents('filename.txt'); ?>
    </div>
    ...

etc. I just don't understand what you're trying to do.

Tatu Ulmanen
That would work. But the form doesn't save the contents to a .txt file. This is my code...$content = $_POST['content'];file_put_contents('fileName.php', $content);The only problem is that it does not keep my page layout.
PHPNooblet
What does saving and retrieving contents of one text field have to do with keeping your layout? And why on earth do you have to save it to a file first, can't you just echo the value itself from `$_POST['content']`?
Tatu Ulmanen
As my name says im basically a idiot when it comes to php. If I just post the content will it be saved to the page? and will others be able to see the content that was posted?
PHPNooblet
Perhaps a good place to start would be to google for some basic PHP tutorials that cover form handling. Also you might be interested in saving your results in a database. As you don't have a specific problem, but you're rather struggling with the fundamentals of PHP, I don't think anyone here can really help you.
Tatu Ulmanen
I just had an idea. I could post my html page layout along with the new content. Wow I should of thought of that before. Thanks for the help I will try it and post the results.
PHPNooblet
+1  A: 

OK, I'll bite.

What you are trying to do here is to have POST from one PHP page actually modify the code on another PHP page. Not trying to be condecending here, since I know you're new at this, but that is a VERY BAD IDEA in bold and all caps for so many reasons I can't even begin to explain it. I suspect the reason that others have had such a hard time understanding your question is that it would never have even occurred to them to try doing something like that. That's how bad it is :)

That said, what you really want to do is take some data that you are posting from one page and display it in another. Normally you would accomplish this by storing the posted data in a database, then retrieving that data from the database on the other page and displaying it.

Rather than get into how to work with databases in PHP, I'll give you a simpler way to do it. What you want to do is have the form doing the POST write the posted data to a text file and then read that text file and display its contents on the other page. You could do that fairly simply like this, where Admin Page is the page you are posting from and View Page is the page you want to view the data on:

Admin Page

<?php
$data = $_POST["mydata"];
file_put_contents("data.txt", $data, FILE_APPEND);
?>
<form method="POST">
<textarea name="mydata"></textarea>
<input type="submit" name="submit" value="Submit"/>
</form>

So, the admin page takes the post data and writes it into a file called "data.txt"

View Page

<html>
<body>
<!-- more html... -->
<div id="fooid">
<?php echo file_get_contents("data.txt") ?>
</div>

And the view page reads the data.txt file and displays the contents at the correct place.

Note that this is all sans error handling & input validation which are of course important things, but it gives you the general idea.

Eric Petroelje