0

My html has a table cell which shows the content of a text file (read in by javascript) containing the names of former visitors. This works perfectly.

There is also a form for inserting the visitor's name. On submit an external php is started which adds the new name to the text file. This is working perfectly too.

At the moment the form begins with this:

<form method="post" action="write2txt.php" target="_blank">

But what I want is: After submit the html simply should refresh to show the updated visitor list.

newbieforever
  • 217
  • 1
  • 4
  • 15
  • is there a code you could show? – Mehrad Oct 14 '16 at 20:15
  • You'll need AJAX for that. http://learn.jquery.com/ajax/ – chris85 Oct 14 '16 at 20:16
  • No, please no ajax ... – newbieforever Oct 14 '16 at 20:18
  • That's nice. Good luck with that. Did you have a programming question? This site is for questions, not a place to dump your to-do/wish lists. – Marc B Oct 14 '16 at 20:20
  • If you don't use AJAX, then submitting the form should load a new page. Why doesn't that page show the updated list? – Barmar Oct 14 '16 at 20:23
  • QMarc B: Sorry! It was a long long way for me (as a newbie) to find solutions for the realy difficult parts (reading from and writing to a text file, string operations, ...) only by using javascript and php in the external php file, so I hoped that there is a simple enough solution for this part too. – newbieforever Oct 14 '16 at 20:26
  • @Barmar: No, at the moment the php is shown after submit, and I have to refresh html 'manualy' to show updated data, – newbieforever Oct 14 '16 at 20:29

2 Answers2

1

(read in by javascript) & no ajax, means what?

Hints

  • javscript files are catched, keep that in mind
  • why not adding the date with php in the first place
  • if the table and the adding is in one file, adding should before outputing the table
  • if you have seperate file redirect back with header('Location: www.xyz.com');

After reading your last comment above, use:

header('Location: www.xyz.com');

after adding to update automaticly

JOUM
  • 239
  • 1
  • 3
1

Don't use target="_blank" in your form, since that displays the results in a new window. Change write2txt.php so that after it writes to the file, it sends back a redirect to the original page's URL, instead of producing its own output.

<?php
// code to update file
header("Location: form.php");
exit();

This will make the browser reload the original page, which will get the updated contents of the file.

If you don't want to use AJAX, this is the general idea of how you have to do it.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • OK, this with header("Location: form.html") in the php file seems to work, the php is not shown, but I still have to refresh html 'manualy'... – newbieforever Oct 14 '16 at 20:56
  • You shouldn't need to do that. That should reload the original page just as it does when you go to the page by hand. – Barmar Oct 14 '16 at 20:58
  • No, I have to do it manually. The caching of the text file (@JOUM) seems not to be the reason, I already use ...src="txtfile.txt?rando"... where rando is a random number. – newbieforever Oct 14 '16 at 21:21
  • Is there a public URL for the web page that you can give me? – Barmar Oct 14 '16 at 21:22
  • It looks like a caching problem. If I try it with the console open, it updates, because I have it set to disable caching with the debugger. Your cache buster doesn't work because you don't have a random number in the URL. It needs a random number that changes each time you call it. – Barmar Oct 14 '16 at 21:43
  • So it should be `?rando=123456` one time, `?rando=32556` another time, etc. – Barmar Oct 14 '16 at 21:44
  • Thank you, Barmar ('target' & 'header' & my stupid '?rando' trial) and JOUM (textfile caching)!!! Finally solved! The last, extremly difficult problem for me was how to addapt [] to be used with a variable for the textfile.txt (at the end with an appended random no). My first attempt was: [document.write('')]. This was not working, so I was forced to look endlessly for other methods. – newbieforever Oct 15 '16 at 14:22
  • But finally I discovered that this my first attempt was absolutely correct, I only have to escape the last > (I now use [... ')] and this works). I have too look why this is necessary, but I am happy to have a solution...due to your hints, Barmar & JOUM!!! – newbieforever Oct 15 '16 at 14:23
  • See http://stackoverflow.com/questions/236073/why-split-the-script-tag-when-writing-it-with-document-write for why you have to escape `` – Barmar Oct 15 '16 at 14:25
  • Could somebody add 'useful' marks to Barmar's and JOUM's postings (I am not authorized) and mark the question as solved? – newbieforever Oct 15 '16 at 14:26
  • Since you asked the question, you can accept the most useful answer by clicking the check mark next to it. – Barmar Oct 15 '16 at 14:29
  • That's the reputation you need to upvote. Everyone can accept answers to their own question. – Barmar Oct 15 '16 at 14:35