• 0

[php] is it possible to...


Question

Get the contents of an entire .html page, and display it in an editable html form? So once it's saved/submitted, it will save these changes in the original .html document?

Basically I'm designing a website as a personal project - I'm trying to learn more advanced things like php and I can't just read books and tutorials, I have to actually try and do things... but here I am stumped! At the moment the website it's all hard coded, but I've got the body content parts stored away in separate files, but I'm wondering if there's a way to get those files displayed in a text box and then allow them to be edited.

I'm just wondering if it's possible. I know there are CMS's like WordPress which will give me everything I need in that respect, but it's not really worth all the effort of re-writing everything and trying to implement how I've designed it into that sort of thing - which I doubt I'd be able to do anyway at this stage.

The other thing would be securing the whole thing down as well... I know it'd be a massive security risk and make spam and maliciousness incredibly easy but yeah... It's all part of this incredibly steep learning curve. :p

Thanks for any advice! If it's that much of a bother though, I'll just teach who I'm handing it over to how to edit things with FTP. :p

Link to comment
Share on other sites

Recommended Posts

  • 0

i prefer to do this in a database, create a table with a row that contains the editable content. then create a form to update it and create a page to display it. Plus it also learns u that side of it.

<?php

//connect to db code first..

$query = mysql_query("SELECT content FROM table WHERE name='Page1'");

$result = mysql_fetch_row($query);

echo $row[1];

?>

that is all you need to display the content from table where the row value 'name' = Page1

http://php.net/manua...l-fetch-row.php

You can use the same code to load it into the form, the variable being within the text area <textarea ...>echo $row[1];</textarea>

then to update the database:

//pass the form results to a variable using $varName = $_POST['textarea-name'];

mysql_query("UPDATE table SET content='$varName' WHERE name='Page1'");

http://www.w3schools...ysql_update.asp

Link to comment
Share on other sites

  • 0

Hmm, I think all this is beyond me. I'm just getting errors upon errors.. SQL may be in English but it's no less confusing! :(

I get 'invalid result resource'..

Edit: I gave Wordpress a go just to see if I was wasting my time trying it, and I was. It messes with the HTML I paste into the editor... My needs are far more simplistic than all of that.

Link to comment
Share on other sites

  • 0

are you connected to the database correctly?

a way to check that is:

$host = 'localhost';

$username = 'db_user';

$password = 'dbpass';

$con = mysql_connect($host, $username, $password);

if (!$con) {

die('Could not connect: ' . mysql_error());

} else {

echo "success";

}

also paste:

mysql_error();

after the query and it will return more information

Link to comment
Share on other sites

  • 0

you dont need a database for this. try this:

you will need to create a few files.

page.html - this will be the page you want to edit, it can contain anything you want. you will need to chmod this file so php has the permissions to write to it.

<html><head></head><body>Text!</body></html>

edit-page.php - this will be the page that has the form on it that you will use ti edit the page. make sure the path to page.html is correct.

<?php

$data = file_get_contents("page.html");

?>

<form action="edit.php" method="post">

<textarea rows="50" cols="20" name="text"><?php echo $data; ?></textarea>

<input type="submit" value="save">

edit.php - this is the script that will save the changes you have made to page.html, make sure you change the path in the line that starts $handle

<?php

$newdata = $_POST['text'];

$content = htmlentities($newdata);

$handle = fopen("/path/to/page.html", "w");

fwrite($handle, $content);

fclose($handle);

echo "success!";

?>

this is a basic script I wrote from memory so there is no input validation and there could be some parts wrong with it, use the links nvme posted to check the correct syntax.

also there is no error checking, so if something goes wrong you'll get a blank page or a php error.

im sure there are better examples of code out there, but this should do the job :p

Edited by Colin-uk
fixed some typos
Link to comment
Share on other sites

  • 0

Hmm, it seems to work pretty well. There's a problem though. :pinch: ... As always!

Basically theres a </textarea> tag in the .html document and it ends the form too early on edit-page.php xP

Does that make sense? >.>

Link to comment
Share on other sites

  • 0

Uhh, how would I implement this? Thanks.

My knowledge of any server side code is practically nil.. but I'm enrolling on a course on monday. :laugh: Go me.

Link to comment
Share on other sites

  • 0

Hmm I can't get any of this stuff working. I had another go at getting it to work in WordPress - while I ported the design ok, the way WordPress' editor messes with my code isn't working well for me.

I'm basically just trying to build an easy way to edit content on a website from within some kind of secured login page. I don't have much but I could offer $50 PayPal for help creating this code and some rudimentary html to go with it.

Only one person would be using it, and I could create a design on top myself... It's just the actual.. important stuff I need to do.

... It's a pain. I can't do it myself. :laugh:

Link to comment
Share on other sites

  • 0

It kinda.. Wiped my file. *laughs* I've no idea where to put it, I'm pretty much dumb when it comes to PHP code. I know what some things do but I can't really interpret it in a semantic way. xP

Link to comment
Share on other sites

  • 0

lol :)

ive updated "edit.php" in my original post above to include the new code part.

Hmm, I've uploaded everything but the original problem is still there. I've used the exact code, just changed the name of the file I want it to point to. Here's a screenshot of how it displays and how the 'View Source' code looks.

Screen%20shot%202010-08-14%20at%2001.34.01.PNG

As you can see..... the textarea ends too soon.. :[..! What did I miss? The php code I'm using is exactly as posted.

Link to comment
Share on other sites

  • 0

Your </textarea> tag is being interpreted as the end of the <textarea>

You need to convert the < and > to special characters (< >)

htmlentities() will convert it

&lt;?php

$data = htmlentities(file_get_contents("page.html"));

?&gt;

&lt;form action="edit.php" method="post"&gt;
&lt;textarea rows="50" cols="20" name="text"&gt;&lt;?php echo $data; ?&gt;&lt;/textarea&gt;
&lt;input type="submit" value="save"&gt;

Link to comment
Share on other sites

  • 0

That part I know, but I can't just take it out. I need some way to keep that there but not break either <textarea> tag. :/

Edit: Thanks, will they be converted back when the doc is saved? I thought Colin & Kudos n co already gave me that specific bit of php code.. doesn't appear to have worked. I'll have another look!

Link to comment
Share on other sites

  • 0

Use html_entity_decode (its the opposite of htmlentities) when saving to convert back.

&lt;?php

$newdata = $_POST['text'];
$content = html_entity_decode($newdata);

$handle = fopen("/path/to/page.html", "w");
$write($handle, $content);
fclose($handle);

echo "success!";

?&gt;

htmlentities goes from html to special characters (eg. > to >)

html_entity_decode goes from special characters to html (eg. > to >)

Link to comment
Share on other sites

  • 0

Hmm. Thanks, I've added all of that in now... but..

More problems (... :()

On clicking 'Save', I get this error:

Fatal error: Function name must be a string in /-directory-/test/edit.php on line 8

Line 8: $write($handle, $content);

Urg! :laugh:

Link to comment
Share on other sites

  • 0

Nope, no typo's that I can see.

I'll just paste everything I'm using..

edit.php:

&lt;?php

$newdata = $_POST['text'];
$content = html_entity_decode($newdata);

$handle = fopen("page.html", "w");

$write($handle, $content);

fclose($handle);

echo "success!";

?&gt;

edit-page.php:

&lt;?php

$data = htmlentities(file_get_contents("page.html"));

?&gt;

&lt;form action="edit.php" method="post"&gt;
&lt;textarea rows="10" cols="50" name="text"&gt;&lt;?php echo $data; ?&gt;&lt;/textarea&gt;
&lt;input type="submit" value="save"&gt;

If I change line 8 to $write($handle);$write($content) --- which I am assuming is really wrong.. it just wipes the .html file, but there's no errors shown.

Link to comment
Share on other sites

  • 0

Aha! It works. I never thought it would.

Thanks for all the help, guys.

I guess this is the basics down, huh.

Now I'll just have to work on getting it protected (password protection) and some sort of redirect instead of 'success'. :p

I feel cheeky asking for more assistance, I guess I'll see if I can do anything.

Link to comment
Share on other sites

  • 0

Oh man... *wipes forehead* It's adding slashes to everything when I save, and I haven't changed the code one bit.

.... :unsure: Somebody doesn't like me.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.