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.
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.
(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
Related
What I made so far:
A button that opens a new .html page (in a new browser tab), with some inputs to store data in a MySQL database (by pressing the submit button using <form> and a PHP script).
I want to display the content of my MySQL database on my index.html file (e.g. in a <p> or a table) by pressing (another) button.
Displaying the content by using a PHP script as when I save something in the database works (like descripted here https://www.w3schools.com/php/php_mysql_select.asp), but I don't want to display the results in a new site, I want to display it in my "index.html".
Is there a proper way for doing this or do I have to make the whole index.html file a PHP file?
If you don't want to change the .html to .php use ajax to fetch data from db when you click on your button ,then diplay it on your table
Not necessarily, what you should keep in mind is that a call to an html or php page is a request made to a web server which delivers the information back to the client, under this paradigm an index.html or index.php It is only executed once, if your SQL query is executed without modifications the first time the page is loaded, it would be enough to make the page design contemplate the space to show the result of said query, if what you want is that the client modify the SQL to show the new result of said query in your page index.html or index.php you should reload the page with the new query or else use ajax to only reload a part of the page and make ajax perform the loading the data with the new query.
In any case index.html can contain php or php code it can also contain html
index.php
echo "<html>";
echo "</html>";
index.html
<html>
<?php
echo "some php code";
?>
</html>
How can I use php to echo a page instead of linking to existing html page with hyperlink?
One example would be
<html>
<body>
click on this link to go back
</body>
</html>
Now, I don't want this link above to be a link to html page but to echo a page with php code when user clicks on click on this link to go back(to generate a page). This way, nobody can access a page after they logout.
Can php do this?
If someone logged out of your website or application I assume you will have a check whether or not this person is allowed to view the content.
Your question itself is very unclear to me. But it sound a bit if you want to do client-side coding (don't follow a link when it's clicked) with PHP which is not possible since PHP is a server side language. You will need Javascript to change the behavior of a link (for example, make an AJAX request which returns the content of another page).
Create a function, what the function should do is it should get triggered on a button click event and the code inside the function must send an curl request to the url you want and get back the html from it and echo it on your page
For answering the second part of your question!. you want no one to access the data without logging in so maintain $_SERVER['']; and sessions for users and validate if the user is inside a genuine session then show him content else no
I have a text file storing one string. I anticipate that the text file will be changing frequently, so in order to keep my page up to date, I would like to use PHP (preferably) to fetch data from the text file every 20 seconds so I can explode it into an array and use the contents accordingly. The variables would also need to update every 20s.
So: on page load, the contents are fetched and displayed. But the contents of the text file may be changed thus making the page outdated while a user may already have it open.
I tried META Refresh, but the whole page refreshes in the middle of browsing and interrupts the user.
Sorry for the confusing description, it's hard to explain. :)
I've searched the web for ages and not found an answer to my question. Please remember I am using a text file and not MySQL, since I'm only storing one string.
Thanks in advance.
If you want to stay with PHP, I'm afraid a refreshing HTML Meta is the solution :
<meta http-equiv="refresh" content="10; url=http://example.com/" />
Refresh the page every X seconds, so that the file gets reloaded.
Another way could be the use of frames, however I cannot seriously recommand it to you.
However, you can load a content without reloading the whole page, using Ajax. It allows you to perform a HTTP request to the server (using a Javascript code) and place its result on the current page, using Javascript as well. You could create a PHP script "my_string_parsed.php", which reads the file, and then parses/prints its content. Then, you could call this script through an Ajax request to http://yoursite.com/my_string_parsed.php, and place its result in a specified HTML tag on your page.
W3Schools.com provides an Ajax tutorial here : http://www.w3schools.com/ajax/
A warning concerning Ajax though : an Ajax content loading must never replace the typical HTTP behavior your browser and the server have. If the string in your file is the only content on your page, then the best solution would be the refreshing meta. Ajax should only be used to refresh parts of a page. Never the whole thing.
Why not using a database instead of a file. You could also use jQuery to update your page smoothly.
I'm using PHP CodeIgniter with jQuery.
Say for example I have a <table> (of employees perhaps).
Now above the said table I have an "Add Employee" button.
This button opens a jQuery-dialog with the <form>. When this form submits, it goes server-side of course (validates whether a duplicate entry or not), then it's sent back to corresponding view with the updated table.
What I want to do is to show a dialog that an employee is successfully added or that it is a duplicate entry.
I'm doing this by
appname/controller/action?addSuccess=true
or
appname/controller/action?addSuccess=false
everytime I return back to the view, then on my page, in my $(document).ready() I check for such query string and opens the corresponding dialog (success or error dialog). My problem now is that, if I refresh the page (same url) I get the dialogs again (although nothing happens serverside).
QUESTION: Is this approach good enough? Any good advice to improve this? :)
Thanks a lot!.
Instead of passing that info as a query parameter, why not store that info Code Igniters flash data. This data gets persisted across only one redirect. The next time you reload the page, the data will not be set (as CI will automatically remove it for you) and you wouldnt accidentally display the warning.
Form is submitted to server. Set appropriate value in CI's flash message system.
Redirect to another url? Or maybe display another view.
In the view html, use php to read that flash message value and set an appropriate value in javascript.
Read that js value and accordingly display the popup/modal/message.
The flash message data will be removed automatically by CI at next page refresh.
Some sample code meant for the redirected view html: (I dont do php...)
var shouldDisplayWarning = "<? php code that sets some value based on flash variable ?>";
if (shouldDisplayWarning === "yes") {
displayWarning();
}
Further reading: http://codeigniter.com/user_guide/libraries/sessions.html (half way down the page they mention flash data.)
I have a function that imports a files contents into mySQL and returns the results. If i refresh the page and click "yes" it will do it again doubling the output with the same content.
How can I stop this happening? In this particular case there is no URI in the address bar but on other functions there is.
You should just check if the contents in the database exist, if they don't, fill them. Otherwise don't run the function.
psuedo-code:
if !database.containsRecords
fillDatabase()
end
On top of this, it is always good practice to redirect after a POST request. So you would want:
fillDatabase();
header("Location page.php");
exit();
Query the database on each page load and see if it has already been populated. If it has been populated then don't attempt to populate it again.
You should use the POST-redirect-GET pattern.
After updating the database, send an HTTP redirect to a separate page that displays the results.
Refreshing the browser will reload that separate page.
I assume you use a POST form to upload the file.
You can include a hidden input field with an pseudo random unique id in your form.
If the user resends the data via POST you can check if you already processed this request.
Hidden fields are not save because a user might edit them, but you can detect accidental resubmits.