I'd like to know what is the difference between HTTP web form POST and button submit click, are they having totally different functioning or they are identical to each other?
I have fiddler, where I tried to emulate the incoming and ongoing POST and GET statements, POST sends the request and concatenate the input area parameters at the end of request, but that still doesn't give me clue whether, POST and sumbmit are identical or not.
HTTP POST:
It is one of the HTTP method to send your data to your respective
script path.
This is the secure method where data is not being shown
in the URL as HTTP GET method.
The data will be send to the desired script via
the headers.
SUBMIT BUTTON CLICK :
It is one of the event of web form through which the data into the
form inputs will get posted to the script path present in the
action attribute of form.
Now what will happen if the form has the two submit buttons?
First thing is that this is the rare case when we need two submit buttons because we can click one button at a time only.
Lets say we have following two buttons in some demo form as follow
<form name="profileForm" action="saveprofile.php" method = "POST">
// Your input fields go here
<button class = "any_button_name" type= "submit " name ="save_profile" value ="save_profile" >Save Profile </button>
<button class = "any_button_name" type= "submit " value="preview_profile" name ="preview_profile" >Preview Profile </button>
</form>
Here in code value attribute has been added to know which button has been clicked.
Now at PHP Side you can checked the which button has been clicked.
// code from saveprofile.php
if('save_profile' == $_POST['save_profile']) {
// save profile code
}
if('preview_profile' == $_POST['preview_profile']) {
// save profile code
}
Hope this may help to clear your doubts.
Related
I have two simple HTML form buttons that call PHP functions to send emails now. Everything works fine except for one thing. If one of the buttons is clicked to send a report, and then the page is refreshed, the function seemed to be called again and the emails well go out again with a page refresh by a user using a browser refresh button.
If the page is refreshed, the email will go out again for the last button clicked. So, if I click on Button 1 and then refresh the page, I will get two reports for button 1. If I click on button #1, and then click on Button #2, only the second report will go out. If I click on button #2 and then #1, only Report #1 will go out again.
So, no matter how many buttons are clicked, refreshing the page will cause the last button click (only to repeat). Trying to unset the request parameter (in code below) has no effect all on the repeats caused by a page refresh.
I don't understand why (on a Page Refresh) the page is seeing the last button click made as set, and why the unset command is not working.
Thanks for any help.
if( isset( $_REQUEST['email_this_weeks_report'] )) {
unset($_REQUEST['email_last_weeks_report']);
#send email now email code for this week
}
if( isset( $_REQUEST['email_last_weeks_report'] )) {
unset($_REQUEST['email_last_weeks_report']);
#send email now email code for last week
}
<form>
<input class="ui-button ui-widget ui-corner-all" type="submit"
name="email_this_weeks_report" value="Email This Weeks Report Now" />
</form>
<form>
<input class="ui-button ui-widget ui-corner-all" type="submit"
name="email_last_weeks_report" value="Email Last Weeks Report Now" />
</form>
Clicking the button submits the form.
The data in the form is bundled up and included in the request.
Aside: You're using method=GET, the default, but you're not making a "safe" request. You are doing something, not just getting information. You should use a POST request.
When you click refresh, you tell the browser to make the request again and display a new version of the page.
Since the request includes the query string which says "send a particular email", it sends that email again.
Unsetting values in $_REQUEST has no effect because when the browser makes a new request with the same data in it: $_REQUEST just gets filled up again.
You should deal with this by using the PRG pattern:
Submit the form using method=POST
Have your PHP script process the data in the form (i.e. send the email) then redirect to a different PHP script
Have the new PHP script display the result (in this case there is no result, its just the form, you could use a plain HTML document with no PHP in it).
Change
<form>
To
<form method='POST'>
A simple way to prevent multiple submissions is to add a random token to the form in a hidden input.
<input type='hidden' name='formtoken' value='<?= uniqueid() ?>'/>
Every time the page is fetched from the server, the value of this hidden variable will change. So on the server side, you can prevent the same form being resubmitted by checking whether a form with this unique ID has been submitted before.
session_start();
$sessionToken = $_SESSION['formtoken']? : null;
$currentToken = $_POST['formtoken']? : null;
// If no session token yet: form has never been submitted
if(!$sessionToken):
// save the current token in session so we'll recognize it next time
$_SESSION['formtoken'] = $currentToken;
/* ok to send the email */
// ElseIf current token was already used: Duplicate form submission
elseif($sessionToken === $currentToken):
/* don't send the email!*/
// Else session token exists, but current token is new: User fetched a new form from server
else:
// update the session token
$_SESSION['formtoken'] = $currentToken;
/* ok to send the email */
endif;
When the user refreshes, the browser will ask if she wants to re-submit the form. If she does, you will know because the current token and the session token will be the same. It's up to you to decide how to handle it.
Refreshing a page with a GET or a POST form will resubmit the data (albeit it will ask you first in the POST scenario, which you should use by the way).
Try redirecting the user after form submit
if( isset(...) ){
// Do your logic
header('Location: https://you_site.com/your-form-page?thank-you');
exit;
}
This must be done before anything else is outputted on the page.
is it possible in html to have a button that submits the data and at the same time lets you delete some data? i have here:
...some form inputs here...
Reject
<button type="submit" class="btn btn-primary">Accept</button>
Back
i want my accept button to delete the request after accepting it. so basically, i need to have those to work on sync. is that possible? if not, i know there is another way.
Submitting a form doesn't "accept" or "delete" anything - it simply passes data to the script defined in the forms action. You need to have a function in the script that you pass the data to that does what you want, and trigger it with the data you pass.
So if you are POSTing to deleteRequest.php (or.asp or whatever) it would have
If ($_POST ['addAndDelete'] == 'true')
{
//add stuff
//delete stuff
//launch a rocket
}
I know that it probably isn't possible to submit a form from one button to 2 various locations so I was wondering if someone knew of a solution.
When I click submit on a form I have the following form tag:
<FORM ACTION="http:site.com/servlets/RequestServlet" method="post">
But I want the form to have 2 action parameters. This way as soon as a user fills this form out, submits the form, the form is submitted to a SERVER AND a PHP script that will pull out the parameters submitted by the the form and email the user details about their form submission such as request ID etc.
Over here the Servlet is submitted to so that a record can be made in the network but an email is required to be sent to a user after form submission in-case they need to reference later on to a rep about their request id for further assistance.
How can I achieve the submission to a PHP script in addition to the submission to the Servlet that's already occurring?
NOTE: I cannot modify the Servlet in any way as it does not belong to me. All I want to do as add the email function for a user to later reference their ticket id.
Set a javascript onclick parameter for the submit button. So it will post to the action you set, but also run a function such as:
function secondSubmit() {
url = "myphpscript.php";
data = { someData: "data", someOtherData: 2 };
$.post(url, data, function(returnedData) {
// can do something on return if you'd like here
});
}
someData and someOtherData will show up in PHP as $_POST["someData"] and $_POST["someOtherData"]. So basically, you will have the form submit to the first URL via the HTML form, and have the second form submitted via jQuery with this function. Alternatively, you can do both submissions in this function and have the form have no action.
For more info, see here: http://api.jquery.com/jQuery.post/
I need to dynamically add form elements to an HTML form as soon as the Submit button is clicked but before the POST data is sent to a server. The new elements must be "read" from a PHP file on my server.
HISTORY:
Currently my HTML form has "hidden" fields that are submitted to another server for processing. I have no control over the other server. My problem is that anyone can edit these hidden fields.
How can I dynamically add form elements to the POST data as soon as the form is submitted?
You can try it this way:
First disable the submit by changing the submit button type from 'submit' to 'button' (or whatever)
Put in onclick on that button to a javascript routine (here i use submit_form()).
Create an empty div within your form. (here i call it with id = 'dynamic')
Using jquery, this is the submit_form().
I think you will need to give it some time for these elements to bind properly before submitting. Maybe a short time delay before $("#myForm").submit();
Here is the code for the submit_form() function:
function submit_form()
{
$("#dynamic").append("<input type='hidden' name='input1' value='whatever'>");
$("#dynamic").append("<input type='hidden' name='input2' value='whatever'>");
$("#myForm").submit();
}
You can post the data to your server and after it post again to the external server with the new elements attached.
Your job is done on server side.
See also:
php server-to-server post?
If you need any control over what is submitted to the other server, you have to do that yourself. Make the form submit to your own server, then validate it, add your data and re-submit it to the other server.
You can use the CURL extension in PHP to post data from your server.
I have a form with a submit button. I have called a function on click of submit button.
function actionPage(form1)
{
form1.action="action1.php";
form1.submit();
return(true);
}
Now I want that the form data should be submitted to two different pages. These pages are on different servers.
I know that we can send the data to a particular page according to the conditions but I am not sure whether we can submit to two different pages at the same time i.e:
function actionPage(form1)
{
form1.action="action1.php";
form1.submit();
return(true);
form1.action="action2.php";
form1.submit();
return(true);
}
Right now it is showing action1.php
you cannot do it using simple form post submit. but you can do it using AJAX.
as you soon as you call submit() fn, the data from the form is posted to the action url page. hence you might end up page being loaded.
You cannot. You could submit data to multiple places using an XmlHttpRequest.
You can't submit the form that way to multiple places. You can do it on the client side via AJAX, or you can post can have the form post to a page that will submit the data wherever else it needs to go. With the AJAX approach, you will run into problems submitting to a different domain due to the same origin policy. I would suggest using cURL on the server side to send the data to other domains.
Just as simple as this:
<form name=f1 type=post action=''>
<input type='submit' value='first url' onclick="f1.action='/save'">
<input type='submit' value='second url' onclick="f1.action='/process'">
</form>
Taken from: http://www.plus2net.com/html_tutorial/submit-two.php