I have
insert.php
verify.php
in Insert.php I have a form with some fields and some other fields which are generated by ajax calls based on selections. I also have an image uploader where the image is uploaded to the server as soon as the user chooses the image. The preview of the image is then showed when upload is done. When the form is submitted, it sends form.serialize to verify.php.
In verify.php the user sees and verifies the data from insert.php form.
If user thinks that everything is correct then he can submit it. or click on the "Edit" button to go back. When going back some of the info are not available. fields that are generated through ajax are not available.
Anyway to fix it ?
I add element to insert.php using
$('#subcategoryPlaceHolder').html(result);
I'm not sure I understand your app completely, but when working with data like this it's generally a bad idea to go use the back function in the browser.
A better solution would be to save the data that needs to be remembered into the session, and when a user clicks Edit it reloads the form using the data stored in the session, without submitting the form. When a user thinks everything is correct they can submit the form, saving the data, and then you can delete the session data.
I ended up using two divs.
#editDiv and #verifyDiv
using jquery I hid the one I didnt need and the form data was there all the time.
Related
I'm using ajax to create list of replies.
In the end of the list I added textarea form that allow user to add reply (also with ajax).
The problem is that i call to my JS in my main PHP page so when user want to submit reply the page doesn't "know" the js code. If i add the js to the ajax php file/page the code will work but then it will be duplicated many times and when user will submit form the text will be submitted many times...
In my console i see that the JS file duplicate every time i load the replies list
How can i prevent it?
Disable the submit button right after user presses it once. Using jQuery:
$("#button").attr('disabled','disabled');
Make sure to remove disabled attribute on AJAX error so user can re-submit the form with new data. You can remove disabled attribute like this:
$('#button').removeAttr('disabled');
[improved]
if you want to not repeat data when navigation or F5 press, simply free the $_POST vars after doing whatever you want, and check if isnt set (before clean, of course) redirect you wherever you want. example:
/* HERE do the job with ajax response. */
if(!isset($_post['foo'])) header('Location: wherever.php');
$_POST['foo']=NULL;
If you're using $_GET... don't do it, use $_POST... $_POST is your friend.
Finaly ensure that if you press F5, you don't re-send form vars by post (otherwise you will get the same). If it's happening, clear your inputs with jQuery on page load.
$('#inputID').val('');
I have a simple php form that submits (POSTS) data on pressing the SUBMIT button and a 'thank you' page is displayed and the data is stored in a database.
Usually on this thank you page if you press the BACK button on the browser and then the FORWARD button on the browser you are brought back to the same thank you page but the form is not submitted again.
In the last few days when I do the BACK and FORWARD on the browser the form resubmits the data and there's a duplicate entry in the database. This happens only in Chrome.
Have I made some errors in the settings in Chrome or is there some other problem somewhere?
The typical solution is known as POST–Redirect–GET. Essentially, your form posts to a page which inserts the data into the database or whatever other actions are necessary and then redirects to another page. That other page doesn't actually do anything but just displays a success message or something. This way, you have two entries in the history: the form and the success page. The form-posting page is never added to the history; pressing back or forward will skip the submission.
Generate a value and put that inside a hidden field. If the user submits the form store that value (must be unique). If one tries to submit the form again with the same generated value, then do not execute your insert or update.
You could set a cookie or session that says the form has already been submitted, and, if that is set don't resubmit the form, but that is basically a band-aid and may not even work...
What you should REALLY be doing is avoiding duplicates by checking the input values against existing values in the db, such as email or username. You should also set your email and username fields to UNIQUE in your database so you'll never get duplicate email addresses or usernames - solving your problem.
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')) {
// 1. check if $_POST['email'] already exists in the database
// 2. if email doesn't exist, insert data
}
Th issue is, when you reached the success page and refreshed, the browser has to resubmit the cached data; because the page where it reached is dynamically generated. Now when you click on the okay, the data which was previously stored in the $_POST variable is resubmitted. To stop it, you would have use dynamic binding instead of static binding.
A simple solution to this issue is:
Make the action attribute of the form blank i.e <form action="">.
Call a javascript method onclick of the intended button.
Add the Action attribute in the JS method and submit the form.
I'm developing an application in mobile view using CakePHP, where there's no Javascript code can run.
My scenario:
In user login form, users clicked submit button. But before the data submitted and users go to the next page, I make an 'interruption' page (renders a new view). In there, contains "Facebook" and "No, thanks" button. When they click "Facebook", they will connect their Facebook accounts. But, if they click "No, thanks", the login continue.
My question is, how can I make that "No, thanks" button? Because if I use <input type="submit"> button, the form that contain its data is in previous page, so this button will not be clicked. And, how to store POST data, and when that "No thanks" button clicked, then, the data submitted?
You can either store the data in session, or you can make the "no thanks" button the submit button of a form where all the data from the previous page is stored in hidden form fields.
Personally for a number of reasons, I would prefer to utilize sessions, but since you are even asking this question, I am guessing you are unfamiliar with usage of session data.
A simpler question would be:
To make these two forms on single page but with LOTS of vertical or horizontal space in between user login form & 'interruption' page so user seeing the login form can't see interruption section although both are within same <form></form> tag. Then, the the submit button is simply a link to interruption section of the same form. this link is simple an <a> tag with inline linking, which would hide the login form & display interruption page without needing javascript. similar to links to Go to Test Section A on this page
http://www.dynamicdrive.com/dynamicindex5/bookmarkscroll.htm
Further then in the interruption section you can use a real submit button which makes a POST back to request the server.
[edit]
This workaround is based on assumption that user doesn't scroll far enough. Because most users won't, making it 1step process for them instead of 2process which is specially important on mobile. And if somebody does then the empty form gets submitted to the server & then we can run validation & return a error to the user. where he/she can fill the complete form. so This is a good workaround better than storing the data in session & making two POST request for every user login over mobile network which are generally unreliable.
A better solution might be just to combine both forms into one form. Then you don't have to make 2 http requests. Maybe all you need to do is to add one extra button to the original form "Login with FaceBook" next to the other submit button.
My client wants to have a 3 page form. The first page allows the user to enter data including a uploaded file. the second page confirms this data. and the third page submits the data to the database and directories.
Via post, I can keep saving the data to a hidden input fields, thats no problem. My problem is the uploaded file. how do I hold that document from page to page ? I am using Cakephp but any advice would help, thanks
You can always just create the illustion that the form is utilising three different pages. Use AJAX to accept and validate/request the user confirm their submitted data. If in this view they accept it initiate a POST to submit all that data.
You really don't need three physically different files to achieve this but you can still let it appear in three stages to keep your client happy.
You just upload the file to temp directory and keep the value in hidden variables just like other form data . If form successfully submitted then the image copy to desired location other wise delete the image
You can easily fake these 3 pages using CSS. Or even 2, as "third page" is actually a server script which has nothing to do with pages in the browser.
Just make your form, then put an event on the submit button which changes divs to whatever "confirmation page" he wants. and then actually send the form using a button on this page.
that's all
An uploaded file is always held temporarily. The server env var should tell you where it is. In Ruby's rack it is stored in the params var. So I guess there is a similar params var in php which has a hash with all the necessary information.
Since the file would be uploaded on the first step, one option is to put the file's location in a hidden input field along with the rest of the data (either there, or put it in the session). With CakePHP, if your file field looks somewhat like that:
<input type="file" name="data[User][image]" id="UserImage" />
Then you will be able to capture the location through
$location = $this->data['User']['image']['tmp_name'];
Which will correspond to something like /var/tmp/xxxxxx
On the last page, if the user confirms all the data, you just use move_uploaded_file() to put the file wherever you want on the server.
move_uploaded_file($location, '/new/location');
I have set up a database and have form data being submitted to it via $_POST in PHP, and I also have my page set up and working which pulls in the fields from the database and displays them. Not rocket science I know.
However what I want to do now is place a page inbetween the submission of the form and the insert in the database, to give the user a chance to check their entry.
I have created this page and used the $_POST data to display the data from the form (as it has not been entered into the database yet), however i'm wondering how, if the user approves the submission, I then INSERT it into the database. (I've tried running the INSERT query from the $_POST data on pressing a submit button, however because (I assume) ivew interrupted the flow between the original form submission and the INSERT query, all I get is a list of errors for unrecognised variables.
So what I have is this process:
form.php /user enters info using $_POST
preview.php /user is previewed info using $_POST and Session code starts (below)
submit.php /MySql query runs but returns all errors for undefined indexes
This is my session start code:
session_start();
if (isset($_POST['preview'])) {
$_SESSION['company_name'] = $_POST['company_name'];
etc etc - remaining form field names
}
POST data is not automatically passed on to other pages. Save the submitted data in the session and read it from there after the confirmation page, or insert all that data back into the page using <input type="hidden"> elements, so they can be resubmitted as POST.
A very simple illustration of the process using sessions:
form.php
<form action="confirm.php" ...>
...
</form>
confirm.php
<?php
session_start();
$_SESSION['data'] = $_POST;
?>
Please confirm:
Name: <?php echo htmlspecialchars($_POST['name']); ?>
...
Confirm
save.php
<?php
session_start();
$data = $_SESSION['data'];
// save $data to database
You should do it with 3 modes
Display
Verify
Insert
The quick an easy way is just to have a GET parameter or a hidden field that says the last state of the form. First time you display it its set to DISPLAY. Form submits, your script looks at the field, knows that after DISPLAY comes VERIFY, and presents the verification page. User submits, script looks at the field, knows that after verify you insert into the database, and you preform the relevant query.
Not sure how you were doing it before, but I assume that you were redirecting or just having the user click a link, which lost all the $_POST data
Unless I'm misunderstanding what you're saying/doing, I'd do something like this:
When the user clicks the "save" (or whatever) button, hijack that click event and use the form data to do the preview business. When they click "confirm", have that send the data to the server and save it to the database.
Again, I'm not 100% sure if that will work, but perhaps it's a place to start.
Good luck!