I'm trying to create a back end for a food truck whose menu constantly changes. I want to make a form that can update text on another page. I'm new to PHP. I think I need a way for the form to update html. So far I have the form made so it will submit the text but the text dosn't remain on the page after it's closed. Is this possible in PHP or do I need to use something else? Here is what I have so far.

http://comp.uark.edu/~cdavies/menu/menuadmin.php

Recommended Answers

All 28 Replies

You won't be able to keep your changes if all you're doing is having PHP process a form and then display the results because you're not saving it anywhere.

Two solutions:

1. Have the PHP that processes your form and then writes the output to a file that you can access. When you update the form, your PHP should overwrite the existing file with the new form data.
What you'd need is 3 (minimum) files:
-form.html Displays your form and points to 'update.php'
-update.php Processes the form and writes the values to 'menu.html'
-menu.html Displays the newest menu

2. Use a mysql database. You would need these files:
-form.html Same as above
-update.php Processes the form and updates a table in your mysql database with the data collected from 'form.html'
-menu.php Selects the proper fields from your mysql table, assigns them to a variable, and then displays the newest menu by including the variables within the same basic HTML you'd use for 'menu.html'.

Each has its own advantage, but will give you pretty much the same result. If you use mysql, you can store and retrieve the information in whatever way you need, but if it's just a basic menu that you're after, writing the form's information to an HTML page will do that too. If you're planning on expanding this, I'd go with mysql, otherwise, go with the first option.

Good luck, let me know if you need more help.

-Ty

Member Avatar for diafol

Can only echo Ty's advice. If you're new to php, I get the inkling that you have limited knowledge of MySQL. Apologies if this is not the case. Therefore, for a simple menu replacement system, overwriting a file (e.g. menu.html) may be the way to go.

It may be overkill, but you may find a WYSIWYG editor useful for saving your edits. CKEditor is pretty good. You then simply overwrite menu.html with the new content. Otherwise, your form may get overcomplicated with loads of different inputs.

Just an idea.

An implementation would be (using Ty's model):

form.php (inaccesssible to all except admin - i.e. you).

1. load existing menu.html content into wysiwyg editor from file_get_contents().
2. make your edits.
3. send form to update.php

update.php (inaccessible to all except admin)
1. accept data (e.g. $_POST) - validate and sanitize
2. write new data to file (menu.html) with file_put_contents().


Personally, I'd have a file called menu.php which has the doctype declaration, head, header footer etc, but which includes the menu.html file as an include file:

include("path/to/file/menu.html");

This means you don't have to mess with DTD, head, footer etc within the editor - just the actual menu itself.

So basically I need to submit the form data to a page that is basically in the background. Is this the syntax I should use, or am I way off?

Page turns the formdata into a variable:

$formdatavariable = $_POST;

Turns the actual file name for the page to be updated into a variable:

$pagevariable = finalpage.html

Updates the final page with the data from the form

int file_put_contents ( $formdatavariable , $pagevariable )

I've tried it a few times with no luck.

Yeah, if your text area has a name attribute as "formdata", then you store that as $formdatavariable as you are showing in your last post.

I see two problems though:

1. $pagevariable needs to be a string, so you need quotes:

$pagevariable = "finalpage.html";

And you're using the parameter variables backwards. You need that line to look like this:

int file_put_contents($pagevariable, $formdatavariable);

The correct syntaxt for file_put_contents can be found here: http://php.net/manual/en/function.file-put-contents.php

Member Avatar for diafol

OK, first of all - $_POST

If you only have the one form field called 'formdata' this will be ok. Otherwise you'll need to access the applicable $_POST variable for each one.

I wouldn't use 'int':

file_put_contents(...,...); //should suffice.

If you're using just the filename, make sure it's in the same folder as the formhandler, otherwise make a RELATIVE reference, e.g.

file_put_contents($formdatavariable,"../path/" . $pagevariable); //should suffice.

<?php
$formdatavariable = $_POST;
$finalpagevariable = "finalpage.html";
int file_put_contents ( $finalpagevariable , $formdatavariable );
?>

This is what it should look like, correct?

Also, how do I set up the final page, like is there a way to tell it where to put the form data I'm sending?

file_put_contents() will create a file if one does not exist. Currently, if you send your form to the code in your last post, php will either create or overwrite a file called "finalpage.html" and fill it with the data from your form. So if you fill your form's text area (named 'formdata') with the text "Spaghetti and Meatballs", finalpage.html will output:

Spaghetti and Meatballs

If you look at the contents of "finalpage.html" though (view the source code), you will see that is no HTML tags- the only text contained in "finalpage.html" will be whatever you enter into the 'formdata' text area. So this will work if you're only interested in plain text. Otherwise you will need to put HTML in the 'formdata' text area manually (which could lead to submission errors if you're not careful) or else use your PHP code to concatenate HTML formatting at the beginning and end of $formdatavariable.

For example, you could do:

<?php
$formdatavariable = $_POST['formdata'];

$html_begin = "<html><head><title>Today's Menu</title></head><body><u><i>";
$html_end = "</i></u></body></html>";
$output = $html_begin.$formdatavariable.$html_end;

$finalpagevariable = "finalpage.html";
file_put_contents ( $finalpagevariable , $output );
?>

So this time if you entered "Spaghetti and Meatballs" into your form and press submit, the source code for "finalpage.html" will be

<html><title>Today's Menu</title><body><u><i>Spaghetti and Meatballs</u></i></body></html>

And if you view it in a browser, you will get a page with the title "Today's Menu" and the text:

Spaghetti and Meatballs

(Note that the text is underlined and italicized because of the <u> and <i> tags I included in the PHP.)

But I'm not quite sure what you mean by "set up the final page" though. Could you specify a bit, in case I misunderstood?

Member Avatar for diafol

see previous post about wysiwyg editor?? Should work

I'm using notepad2 to do this in. I'm really not sure what I'm doing wrong. At this point I'm just trying to get it to do the bear minimum, overwrite whatever is in the html file when I hit submit, and I can't seem to make it work. Is there special permissions that have to be set or something? You can see it here:

http://comp.uark.edu/~cdavies/menu/menuadmin.html

Also, is there a way to keep it from going to the php page when I click submit? Then I can just use the refresh the preview iframes on my admin page to see the changes.

<html>
<body>
<table>
<tr>
<td>
Menu left:
<form action="menuleft.php" method="post">
<textarea name="updateleft" cols="50" rows="4" "></textarea><br>
<input type='submit' value='submit' /><br />
</form>
Menu left preview<br>
<iframe src="menuleft.html"></iframe><br>
</td>
<td>

Menu right:
<form action="menuright.php" method="post">
<textarea name="updateright" cols="50" rows="4"></textarea><br>
<input type='submit' value='submit' /><br />
</form>
Menu right preview<br>
<iframe src="menuright.html"></iframe><br>
</td>
</body>
</html>

This is the source code for the page you provided the link for. I'm including it because it contains values I'm going to use here:

The source code for "menuleft.php" should look like this:

<?php

//Get text area value when form is submitted
$menu_text = $_POST['updateleft'];

//Update "menuleft.html"
$filename = 'menuleft.html';
file_put_contents($menu_text,$filename);

//Redirect back to your original page
header("location:http://comp.uark.edu/~cdavies/menu/menuadmin.html");

"menuright.php":

//Get text area value when form is submitted
$menu_text = $_POST['updateright'];

//Update "menuright.html"
$filename = 'menuright.html';
file_put_contents($menu_text,$filename);

//Redirect back to your original page
header("location:http://comp.uark.edu/~cdavies/menu/menuadmin.html");

That should do what you need it to. It will send you to your "menuleft.php" or "menuright.php", but will automatically redirect you once everything has been completed (shouldn't take more than a moment). You do need PHP to have writeable permissions to do this, but that's up to your server's configuration.

To make it so that you never leave the page at all, you would need to change "menuadmin.html" to "menuadmin.php". The 'action' for both forms needs to point to "menuadmin.php", and your textareas could have the same name (the alternative would be using a conditional if() statement).

Something like this would work (this example assumes both textareas are named "update"):

//Instead of $menu_text = $_POST['updateright'];
$menu_text = $_POST['update'];

Enclose the php in the proper tags, and put it above the opening <html> tag on "menuadmin.php", so that file would look like this:

<?php

//PHP code goes here (you don't need the 'header' function anymore)

?>

<html>

//Put the modified HTML here

</html>

Well I feel like I've done everything correctly, and it still isn't working. The only conclusion I can draw is that the server I'm on doesn't support PHP. I'm about to move the whole thing to another server so I'm not to worried about that. You have gotten me going in the right direction and hopefully it will all work on the new server.

THANK YOU VERY MUCH!

Simple test to see if your server supports PHP:

Make a file called "info.php" that contains this:

<?php
phpinfo();
?>

Upload to your server and point your browser to it and see what happens.

PHP needs write permissions to create a new file and modify permissions to change a file's content. Do you know if your server has these privileges enabled for your working directory?

Alright I'm back. I've moved the site to Go Daddy hosting.

http://www.timbertheband.com/menu/menuadmin.html

For some reason I'm getting a syntax error now, which I guess is better than getting nothing like before.

Parse error: syntax error, unexpected T_STRING in D:\Hosting\7317202\html\menu\menuleft.php on line 3

This is the code I'm using, mostly from what I've been provided in this thread.

<?php
$menu_text = $_POST['updateleft'];
$html_begin = "<html><body><table bgcolor="black"><td><tr><font size=5 color="yellow">";
$html_end = "</font></tr></td></table></body></table></html>";
$final_page = $html_begin.$menu_text.$html_end;

$file_name = 'menuleft.html';

file_put_contents($final_page,$file_name);
header("location:http://www.timbertheband.com/menu/menudadmin.html");
?>

You need to escape your quotes or change them to single quotes on line 3:

<?php
$menu_text = $_POST['updateleft'];
$html_begin = "<html><body><table bgcolor=\"black\"><td><tr><font size=5 color=\"yellow\">";
$html_end = "</font></tr></td></table></body></table></html>";
$final_page = $html_begin.$menu_text.$html_end;

$file_name = 'menuleft.html';

file_put_contents($final_page,$file_name);
header("location:http://www.timbertheband.com/menu/menudadmin.html");
?>

Personally, I like single quotes, but if you want to keep them as double quotes, you need to add a \ before them, or else PHP will think you are ending the string.

Well I've made the suggested changes, and it still won't work. How do I tell if the server just won't allow me to rewrite files with this command? Is just a basic godaddy hosting account. Is there another way to do what I'm trying to do?

Well I've made the suggested changes, and it still won't work.

Does it give you an error message, or just do nothing?

For your second question- to check if PHP can write to a file, save this script as a .php file and then try accessing it from your browser (make sure it's in the same directory as "menuleft.html"):

<?php
$file_to_check = "menuleft.html";
if(is_writable($file_to_check)){
    print("Yes");
} else {
    print("No");
}
?>

If it says "Yes", then you are able to write to menuleft.html and something is still wrong with your code. If you get a "No" then PHP doesn't have permission to write to that file.

As for your last question, there's always more than one way to do things. I think there were some suggestions earlier in this thread.

Alright so I'm still working on this. I talked to godaddy support and they said I need to create a php5.ini file and put " allow_url_fopen = On ", I've done that and recycled the app pool, it still does not seem to be working. Is there any other way to do what I'm trying to do with out having to change the php settings. Another command or something? Any ideas?

This may be a long shot, but TRY (you can always go back and change it if it doesn't work) renaming "php5.ini" to simply "php.ini". I think that "php5.ini" only works with php files with the ".php5" extension.

Not sure if I made that all up, but maybe it's worth a try.

If there's no change, there might be other settings you need to specify in "php5.ini", such as allowing file_put_contents() or fwrite().

I tried your suggestions, renamed php.ini and added allow file_put_contents() and fwrite(). I tried both allow and on, and I tried both combinations with the file named both ways. I'm pretty sure that I read that when using a windows server, which I am, I have to use php5.ini or it won't work. I've been trying to figure out what setting file_put_contents() is. This is the phpinfo() page for my domain.

http://www.timbertheband.com/phpinfo.php

Can you please post the PHP and HTML code again so we can see the most recent stuff you've been working with?

<?php

$menu_text = $_POST['updateleft'];
$html_begin = "<html><body><table bgcolor='black'><td><tr><font size='5' color='yellow'>";
$html_end = "</font></tr></td></table></body></table></html>";
$final_page = $html_begin.$menu_text.$html_end;

$file_name = 'menuleft.html';

file_put_contents($final_page,$file_name);
header("location:http://www.timbertheband.com/menu/menuadmin.html");
?>

This is the most recent code and the main admin site is
http://www.timbertheband.com/menu/menuadmin.html

I've also tried it with this code and I'm till getting permissions errors

<?php
$myFile = "menuleft.html";
$fh = fopen($myFile, 'w') or die("can't open file");
$html_begin = "<html><body><table bgcolor='black'><td><tr><font size='5' color='yellow'>";
$html_end = "</font></tr></td></table></body></table></html>";
$menu_text = $_POST['updateright'];
$stringData = $html_begin.$menu_text.$html_end;
fwrite($fh, $stringData);
fclose($fh);
?>

What do you mean by permission errors? Are you getting an error from PHP, or are you getting your die("can't open file"); as an error?

What do you mean by permission errors? Are you getting an error from PHP, or are you getting your die("can't open file"); as an error?

A permissions error from PHP

Warning: fopen(menuright.html) [function.fopen]: failed to open stream: Permission denied in D:\Hosting\7317202\html\menu\menuleft.php on line 3
can't open file

all of my permissions are wide open read, write, execute for all users

I'm a bit lost on this one, I'll admit... It's hard for me to check for errors when it's your environment (not your code) that has the errors.

That being said, what have you set CHMOD 777 to? You say it's 777 for all users- but where are those permissions applied (ie, the directory, the files, etc?).

Check that your 'menuleft.html' and 'menuright.html' files are also set with these permissions (because if it can't open the stream, I wonder if there are read permissions set to those files). Also, make sure the PHP file itself is executable and writable.

I don't know what method you are using to set your permissions, but a lot of people make the error of CHMODing the directory that they're working in, and often fail to CHMOD the individual files within that directory as well.

As far as I can see, your code is good for what you're trying to do, so you probably don't need to worry about that. Also, have you contacted you host (Godaddy?) about the issue?

Actually I finally got this problem solved. I migrated from a windows server to a linux one and then everything worked perfectly. Thanks for the help though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.