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
Related
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.
I'm using Codeigniter and wants to know how I can make a checkbox that submits the form on click?
Secondly, this checkbox will be one of several checkboxes that will act as a filter like products > $20, products < $30, how do i pass it in the url? I'm thinking /1+2+3
Haven't worked with Codeigniter much, but I can answer how to make the form submit on checking the checkbox with JS:
<form id="something">
<input type="checkbox" name="foo" id="foo" value="yes" />
</form>
<script type="text/javascript">
$("#foo").click(function() {
if ($(this).is(":checked"))
$("#something").submit();
});
</script>
The javascript questions seem to have been solved already; let's step to the codeigniter ones.
You can pass the url in one of those two ways.
Idiomatic but limited: as /1/2/3/4/etc. The controller function handling that url could both use func_get_args to read them or, if you already know how many parameters will be passed at the most, give a default value of null to all non-necessary paramenters;
Not Codeigniterish but seriously better for search parameters: enable the query strings on your config file, pass arguments as you would normally with GET (min=2&max=3 and so on) and get their value with CI's input class ($min = $this->input->get('min')).
This has nothing to do with PHP, nor CodeIgniter. The solution is to submit the form in the onclick event of the element.
<input type="checkbox" name="filterx" onclick="document.forms[0].submit()" />
You can use the OnSubmit event of the form to nicely format the url, if you like.
To do this, you can
get the values of all desired elements,
build a nice url from it,
set the url using location.href = yourniceurl,
cancel the regular submit by returning false.
Note that both solutions require javascript to be enabled. So it is a good thing to have other means of submitting the form (submit button). Don't rely on submitting by pressing Enter. Opera will use the Enter key for toggling the checkbox instead of submitting the form.
If you like, you can hide the submit button using Javascript, that way, users having Javascript will have their form auto-submitted, while users without can use the button.
You will need to make sure that your server side form validator not only accepts the nice url, but the ugly url (which posts values like ?filterx=on) too.
Is there a way to check which link/button was clicked from a form besides using
if(isset($_POST['myvar']))
{
//if true do this
}
else
{//do this}
or a querystring?
My page has a jquery drop down and thus the above isset function is not working as the form for login is contained in the dropdown.
I'm not too familiar with PHP, but going the jQuery route, you can create a hidden field
<input type="hidden" name="postSource" id="postSource" />
and then wire up some event handlers to modify that value before the page is posted.
$('#myControl1, #myControl2').change(function()
{
$('#postSource').val($(this).attr('id'))
});
.NET uses a similar technique, I think. Then on server side, you just access the posted value of the 'postSource' field.
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 several forms inside DIVS on my page.
I have one form which contains a text field and is always visible, and this is where the user hits 'enter' key and submits...
I want to get values selected in the other forms on the page, and submit them all together, not one by one, so that my PHP code can use "ALL VALUES" and search a mysql database...
Is this possible by javascript using the "<form onsubmit>" to call a javascript?
any codes would be appreciated...
thanks
Without some Javascript hocus-pocus, you can't. One form = one request.
You can do it with JS, and you have a few options. The easiest would be to loop through all the forms on the page, and basically duplicate all the input fields and values into one form and then submit that combined form.
With jQuery it'd go something like this:
$("form").submit(function() {
combineAndSendForms();
return false; // prevent default action
});
function combineAndSendForms() {
var $newForm = $("<form></form>") // our new form.
.attr({method : "POST", action : ""}) // customise as required
;
$(":input:not(:submit, :button)").each(function() { // grab all the useful inputs
$newForm.append($("<input type=\"hidden\" />") // create a new hidden field
.attr('name', this.name) // with the same name (watch out for duplicates!)
.val($(this).val()) // and the same value
);
});
$newForm
.appendTo(document.body) // not sure if this is needed?
.submit() // submit the form
;
}
You need to make a script which will collect the data from the forms, and inject them into the only form that is visible. Only one form will be submitted, you can not submit multiple forms.
You can create multiple hidden fields, or you can construct a single hidden field in that form, then use javascript to collect all the data from the various forms, then create a JSON string, set the value of the hidden one, and submit.
Edit:
Say you have a single hidden input like this:
<input type='hidden' name='hiddenfield' id='hiddenfield' />
you could use JQuery to do this:
$('#hiddenfield').val('myvalue');
To get the value from other forms is as simple as calling $('#elementid').val()
before form submission. To use JQuery, go to the jquery website, download the library, and link it (follow their installation guide).
you can add an onsubmit to that form, and then collect other values with javascript:
<input type="hidden" name="hidden1" id="hidden1" />
<input type="hidden" name="hidden2" id="hidden2" />
<script>
document.getElementById("the_form").onsubmit = function(){
document.getElementById("hidden1").value = document.getElementById("other-field1").value;
document.getElementById("hidden2").value = document.getElementById("other-field2").value;
};
</script>
Wrap the whole Page in your form tag (if possible) and use the server side code, along w/ Javascript, to handle your business rule validation.
kind of a hack solution, but it should minimize the necessity for Javascript "hacks" depending on your skill level with javascript.