I’m playing around with a basic guestbook type arrangement, using a file to store the data instead of a database. I have a page we’ll call main that contains a form for the user to input their comment, and an iframe to display the list of comments. When the user hits submit, the php script takes over and writes the comment into the data file. This data file is the source html for the the iframe. What I need to do is then force a reload of the iframe. Any ideas how I can do this?
(Just using a header (“location: main”) construct doesn’t force the iframe to reload)
How’s your JavaScript? Search Google for “javascript reload frame” or “javascript reload iframe” for some examples of how to do this. I’d imagine that after you ran your PHP script to update the text file, you can put a little JS at the end of it to refresh the frame.
Of course it might be able to be done using PHP but I can’t tell you how that’d work.
The php is in its own file, called by the form on the main page. The data is just a text file. I’m not sure how such javascript would be executed (since the php doesn’t actually generate a page).
You can use PHP’s header() method to redirect the page, like so:
header("Location:yourpage.html");
Replace yourpage.html with the name of the page containing your IFRAME. That will redirect the browser back to wherever you want. The URL you give it can be relative or absolute, it’s all the same. Documentation
One caveat, though. header() only works if you haven’t written any output to the browser yet. Since you say all your script does is read form variables and write to the text file, you should be fine, but just so you know.
Ah, so you did. I tend to skim these, rather than reading in depth. Anyway, I did a little test case, and here’s what I came up with:
<html>
<body>
<iframe src="test.txt" name="theframe" id="theframe" width="400" height="200"></iframe>
</body>
<?
if (isset($_REQUEST["submit"])) {
writeToTextFile();
reloadIframe();
}
drawForm();
function drawForm() { ?>
<form action="iframe.php" method="get">
<input type="submit" name="submit" value="Submit" />
</form>
<? }
function writeToTextFile() {
$fileHandle = fopen("test.txt", "a");
fwrite($fileHandle, "Here's a new line
");
fclose($fileHandle);
}
function reloadIframe() { ?>
<script type="text/javascript">
document.getElementById('theframe').src="test.txt";
</script>
<? } ?>
</html>
The meat of it is the Javascript in reloadIFrame(). You can give the IFRAME an id and then refer to it with Javascript like any other element. Just set the src attribute to the original text file and it should (hopefully) force a reload.
Hmm, I don’t think your code will work either. You see, the php is in an external file (ie, not inline with the document) and therefore I can’t reference any java functions in the main page (as your code does).
When the user hits the submit button, their browser takes the form elements from the tags.html page and sends a request to your server for the page named tagboard.php. Inside that request it includes the user entered values from the form elements from the tags.html page.
You php page should process these values and add them to your data file and then return a valid html document that the browser will treat as any other web page.
We can’t see the contents of your tagboad.php page because your server is processing the page and returning the generated errors as an html page.
The easiest solution given what you already have is to have the php page open the file, write the info, and then redirect the user back to the tags.html page using either JavaScript or the php header function above. (You’ll need to use the header function before any normal print commands, though, it must be the first thing the page outputs back to the web browser).
If I understand your description correctly, this is what it does now. Tagboard.php uses the header command to send tags.html back to the browser. The problem is this doesn’t seem to cause a reload/refresh. It seems the browser just pulls the page back from cache, ergo the updated elements (specifically the iframe’s source) are not displayed. Actually clicking the refresh button works to update it.
(I mean, ya, I could just stick the php, form, and iframe all on the same page as in Daver419’s example, but then I’d have to copy/paste all that code everytime I wanted to use this element on another page.)
If it is caching you’re worried about, some sites use dummy strings based on date, time, or whatever, that are appended to the url but don’t mean anything to the php parser (it just ignores them). The browser thinks it is a new page, so tries to get the data.
It seems like the simplest solution would be to remove the target element from the form. That way the PHP script would run in the same browser window as tags.html, and then redirect back, causing the IFRAME to reload without any special finagling.
Also, the reason that header() call isn’t working, is that the error messages spit out by fopen(), fwrite(), and fclose() count as output to the browser, which is verboten if you want to use a header() redirect. I’m not sure if you were running into these errors in your original example, but that can be fixed by enabling write access on the directory you’re running this script in (in Unix, chmod ugo+r).
About those errors - no, they don’t usually occur. I just forgot to fix the rights when I uploaded the files to the server (as opposed to running the scripts locally).