Prevent multiple submissions - php

I have a page that sends data to itself, i.e.using PHP_SELF.
Upon page refresh, i.e. when the page reloads, I place two buttons - Refresh Again, where I
send data using again using PHP_SELF. - An alert box pops up asking for confirmation, that usually occurs when date is sent again to a page.
The purpose of the second button - Reload without sending data, must reload the page.
Without the above pop up box appearing, i.e. without data being sent.
Is there a way to flush out data before it is sent again to the
server or to a page?
I send data via $_GET.
I check whether I receive any data as follows:
if(isset($_GET['getVariable']))
/* I place the buttons here, in a form, the action parameter of which points to the same page, i.e. `PHP_SELF`

You could make the refresh without submitting a new form:
<form name="refresh" action="" method="post">
<input type="submit" value="Refresh without submitting" />
</form>
Or use JavaScript to refresh on the current button.
<input type="button" value="Refresh without submitting" onclick="location.reload();" />
Also, use action="" instead of PHP_SELF.

Related

i want to prevent user from shifting in my web by writing url manually by php

i have main page and form this page there is some button "each button will refer the user to the correct page after click on it" i want to use PHP to prevent user from writing the URL manually also i used $_SERVER['HTTP_REFERER'] but it's not working with all browsers.
You can try to achieve it in this way:
on every page looking for some $_POST value, for example if (isset($_POST['refer']))
if isset the value that you're looking for print the page content, otherwise print error, redirect to homepage or do what you want to do when the user come in the page without clicking on your button
modify your button, on click not only redirect the user but submit a form with the wanted post value
You can add your value directly into button value
<form action="/yourpage.php" method="post">
<button name="subject" type="submit" value="VALUE_TO_SEND">BUTTON
TEXT</button>
</form>
VALUE_TO_SEND can be: the current url, or what you want to use to check the refer

$_POST and redirect with html submit button

I have a html form that has a series of radio groups along with php code that insert the radio group into a table. The insert works if I do not have a link to the next page, but if the link is in the following :
<form name ="input" action="demopg2.php" method="post"> does not insert but links to the next page.
<form name ="input" action="" method ="post"> inserts the data but does not link to the next page
the submit button code is:
<input type="submit" class ="button" name="submit" value="Submit" />
It sounds like you have:
A script which can both display a form and insert submitted data
Another script which shows the next page
You need to change the logic so that either:
The "insert submitted data" logic is moved from the first script to the second script or
The first script, if it has inserted data, redirects (with a Location header) to the second script instead of displaying the form
The second approach is generally the better one as it is more easily extended to give you a third possible output: Displaying the form with error messages if bad data is submitted.

Button INPUT that saves data and links at the same time

I'm doing a questionnaire (form) and I need to put a submit button that does two things:
Be a button type INPUT (because I need to use this kind of button on my PHP code, I've if(#$_POST['Next']) for save the dates of the form in my DB).
That this button will have a link for go to the next screen of the questionnaire. I tried with
<a href="demo2.html" target="_blank">
<input class="buttonNext" name="submit" type="submit" value="NEXT &#8592">
</a>
This code doesn't work, but with IE browser, on the page appears a circle next to my button that is the link. So the button doesn't work; it only saves the data, but doesn't link to the next page.
How can I solve it?
The form's action determines the URL where the form is sent, and this is just good for you (it is sent to the PHP that processes this step). It can save the data and/or return the form again to show validation errors. Once you decide to go to the next step, you can redirect the user to that url.
use if(isset($_POST['Next'])){ instead of #

Form Textbox Caching Problem

I have a PHP form, with various input fields and textboxes. If you submit and go back, all of the data that was submitted in the input fields remains, however the textboxes are blank. How can I get the data entered in the textbox to cache like the regular text inputs?
This is a client-side issue. Although there's no way to force the browser to cache the textarea input you can send the data back yourself using cookies if you want. One easy way to do so would be to store the textarea input in cookies when the form is submitted and then to check for the cookies and insert the values into the page source on subsequent requests to the server.
Check out this page for information on setting the cookies and this one to learn how to access the information the next time your form is accessed.
I generally prefer the back button myself, but if you want to re-populate all your fields, an alternative is to have the form page submit to self and then do like this:
<form action="whatever" method="POST">
<input type="text" size="20" name="text_field" value="<?php echo $_POST['text_field']; ?>">
<textarea name="text_area"><?php echo $_POST['text_area']; ?></textarea>
<input type="submit" value="Submit">
</form>

upload and process user selected file in html, javascript and php

I want to have a user select a file and then have php put the contents in db. Now the last part (processing the file in php) is easy. But is there a way I can process a user selected file whithout a new page load?
If I use the following:
<FORM ACTION="upload.php" METHOD="post" ENCTYPE="multipart/form-data">
<INPUT TYPE="file" NAME="somefile"><BR />
<INPUT TYPE="submit" NAME="submit" VALUE="Upload">
</FORM>
Page upload.php automaticaly loads after which I can insert the uploaded file in a database.
I would like to use a combination of javascript, php and xajax to process the file. I don't think something like this is possible:
<FORM ACTION="javascript:xajax_proces_file()" METHOD="post" ENCTYPE="multipart/form-data">
<INPUT TYPE="file" NAME="somefile"><BR />
<INPUT TYPE="submit" NAME="submit" VALUE="Upload">
</FORM>
Because the file is not uploaded when function xajax_process_file() is called. Or is it? I think I do not fully grasp the principle of uploads with javascript, html and php.
Any help and or clarification is much appreciated.
It may help to think of this as a two step process.
First, the user fills in the form and submits it - step one.
Second ( which is the default action ) the specified target file takes the input from the form and uses it to do whatever. You can almost think of a form "action" as a link - the default action of a link click is to display the result of the link. The same goes for a form action - display the result of a form action.
Now, it's possible via JavaScript to disable the default action of an element for a particular event. It is also possible via JavaScript to access a browsers HTTP mechanism to send/receive HTTP request (which is what every page request is - whether from your URL bar or a page link or a Google search result).
And that is what AJAX in simple terms is - using JavaScript to use a browsers HTTP mechanism to send requests to a web server and possible receive a response back without the use of a traditional click event. You then combine this with the use of JavaScript to "turn off" default actions and instead follow the action specified by you to get information from a server and add it to the page without ever having to refresh the page.
Many times to prevent the defualt action from taking place for a certain element, you return false in your code. The same goes for your form. Using javascript:
form.onSubmit = function() {
blah blah blah.....Use ajax to send the information to the form handler
return false; //Prevents the defualt action of the submit event
}
If you are really new to AJAX, I suggest you check out this tutorial and then this one. Lastly, I would recommend using a Javascript framework like jQuery to help you - it is awesome and does alot of great stuff, but also has easy and built in functionality for AJAX.
Here is another tutorial to do a form submit with no page refresh (uses jquery).
an alternative is to make the form directs the action to an iframe, after processing the query in the iframe, proceed by JS to clear the form of the father

Categories