completely replace an HTML file with another using PHP - php

I am working on a html form, which I only want to be usable once at all.
So if one person has filled it in, another person should not be able to fill it in again. Not even the same person, and not even if he or she clicks on going back in his or her browser.
So far I have multiple ways in mind what could work:
Use PHP to completely replace the form.html page with another .html file, which I have already stored on the server for this purpose, or
Use PHP to partly replace the form.html page, even though it has no added value over completely replacing the html page, or
Adding a hidden field to my form.html file (which I have to change to a .php page in this case), posting a "1" to a database which I can retrieve everytime the form.php page loads in a way that it shows the form when a 0 is set, and doesn't when a 1 is set. In this case I also wonder if there isn't a simpeler solution to create a database just to store one "1".
I know how to use google, and usually I always manage to find a solution, but now I am feeling clueless. Please someone help me with a simple solution.

I have used the rename command at the end of my mail script, and this way it works:
mail($to, $subject, $comment, $headers);
rename('../mailtest/replace/emailform.html', '../mailtest/emailform.html');
?>

Related

Form, external php, refresh

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

How to make a url parameter stick in php

Okay php newbie here, please bear with me. I am not sure if this is a redundant question but here goes. I have a reference code i want to stick to my url. example: site.com/index.php?refcode=123. That's fine right? we can put anything on there. Naturally the visitor goes to the index page. But if the visitor then clicks on other buttons that leads to other pages in my site, the parameter is gone. Like I want to track which code the visitor has when he sends me an email when he later decides to go to my contact page. How can this be done with php? or can this be done with jquery?
You would be best off saving the url variable into a session variable instead. The session variable will stick with the user so you have access to it no matter what page they go to.
$_SESSION['refcode'] = $_GET['refcode'];
Make sure to use session_start()
But if you do want to do it the way you have asked you can modify all urls on your page and add:
'?'.$_SERVER['QUERY_STRING']
This will add the query string to your url so the next page they go to would still have it. But that does seem like a lot more work.

edit a file via php

I've look around, and not found anything yet. So I'm going to ask a question, sorry if this is 'nooby'
What I want to do is this:
Have a page with a form on it, IE: Form.php
and I want to allow myself to edit another page, IE: index.php
Kind of like a really BASIC two page CMS, edit it on the form.php page, and then it saves on index.php doesn't overwrite, but saves it under the current post that's already there.
Sorry for the 'vague' question, however want to do this fast :P
You can do it the way Patashu said with a database but if your only going to be editing one page you can do it with http://php.net/manual/en/function.fopen.php.
On the Form.php have it open index.php using PHP then have a textarea field that echos out the index.php and then using the Fopen function save over index.php. Make sure you secure Form.php with a password or even using a database. It also depends what programming language you will have inside index.php
Instead of editing HTML or PHP using a PHP file, you should make a database, store content in it that you want to be dynamic, and retrieve from the database when the PHP for the dynamic page executes. Go read about SQL :)
(reposting so it can be chosen as selected answer, if you want!)

Browser goes Back after PHP page is executed

I have a form which sends data to a proccess page. Is it possible when the code in the process page is executed for the browser to jump back to the previous page?
EDIT
The reason i want this, is because i have a set of parameters which contains checkboxes. These parameters are echoed out via a while loop. I have a table where it shows which of those parameters are active. I would like to check the checkboxes by defualt where the corresponding parameter is in the table.
|||||||||||||||||||||||||||||||| Example ||||||||||||||||||||||||||||||
PARAMETERS:
T-Shirt: checked
Distance: checked
Race: unchecked
TABLE (parameters)
• T-Shirt
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||the example above checks the box if it already exists in the parameters table. When the user clicks back after the process page, the boxes he selected is checked.
Hope i am clear enough
Regards
I think a nice solution might be to implement a front controller for your webbage, a central script to include the appropirate page. Using a system like that, the user stays on the same page all the time even when other pages are loaded.
I haven't used a front controller in quite some time, so I won't write examples due to potential errors and poor coding. You can probably find some examples using google though, I wouldn't be surprised if the subject has been brought up here on Stackoverflow before either.
Even though I'll have to point out Griffin's solution is the best, if you're dead set on a redirect, simply have your PHP script execute the following lines:
echo '<script>document.history.go(-1);</script>';
die();
Or even
die('<script>document.history.go(-1);</script>');
It's a dirty solution, so I must advise against it.

Help with Sessions Issues

This just a same script in different server:
The link below works. When you enter the correct characters it display success.
http://mibsolutionsllc.com/captcha_test/test.php
This link below works. Even you enter the correct characters. Display always error. The session value doesn't give function the right way.
http://www.universitywomenshealthcare.com/uwh-content/captcha_test/test.php
If it's a sessions issue, how would one go about fixing this?
Try to echo out what the answer is for the input and check your current settings when you are comparing the input vs. the captcha.
why don't you put your php code in this page? In your second page (http://www.universitywomenshealthcare.com/uwh-content/captcha_test/test.php), somehow session variable is not keeping the originally assigned captcha value. Have you put session_start() in this second page. We will be able to help you more if you can send these two php pages.

Categories