Form with multiple submit option. PHP - php

I have a HTML form page with following code :
<form action="chainresult.php" method="post" enctype="multipart/form-data" />
<input type="hidden" name="MAX_FILE_SIZE" value="3145728"/>
<input type="file" name="userfile" id="userfile" size="30" />
<input type="submit" value="GET SEQUENCE" />
</form>
<form action="helix_info.php" method="post" enctype="multipart/form-data" />
<input type="hidden" name="MAX_FILE_SIZE" value="3145728"/>
<input type="file" name="userfile" id="userfile" size="30" />
<input type="submit" value="GET HELIX INFO" />
</form>
My page has two browse options and two submit options which takes the use to 2 php pages. I want to have only one browse option with two options that takes the user to 2 different php pages based on what the user clicks.
Any help is appreciated!

You will need to combine the two forms into one (you don't even necessarily need the form tags), use JavaScript or jQuery to capture the submit button click, evaluate the input value based on your validation rules that route the form submission, and then post the values to a form, likely through ajax.

You can submit multiple forms but you will have to use Javascript. It should be doable with jQuery without too much sweat and tears. Something like...
$("#my-submit-button").click(function(){
$("#first-form").submit();
$("#second-form").submit();
})

I am not sure I understand your question correctly, but if you want to be able to post data to two different URLs, with two different submit-buttons, having two different forms is the only way to do it with plain HTML.
However, it would be possible to use JavaScript. In that case you could mash both forms together, evaluate the input before sending any information, and the post the data to different URLs depending on the input, using AJAX.
It is worth noting that going down the JavaScript-road, you would make the form unusable for anyone who has disable JavaScript.
I would probably suggest that you make it a single form, point it to an URL that can handle either case. So you always post the data to the same URL, and the server-side code would have to evaluate the input and decide what to do with it. That case you don't eliminate users that doesn't have JavaScript activated.

Not sure if I understood the question properly, but you could use jQuery to change the action attribute of your form, depending on what the user chooses. Something among the lines of:
<form id="myform" action="dummy.php" method="post" enctype="multipart/form-data" />
<input type="hidden" name="MAX_FILE_SIZE" value="3145728"/>
<input type="file" name="userfile" id="userfile" size="30" />
<input type="radio" name="formtype" value="uploadscript1.php" /> Option 1<br>
<input type="radio" name="formtype" value="uploadscript2.php" /> Option 2<br>
<input type="submit" value="GET HELIX INFO" />
</form>
And in jQuery:
$('input[name="formtype"]').change(function(){
$('#myform').attr('action', $(this).attr('value'));
});
I am not sure about the jQuery part, but it should work ok. Try experimenting with that. :)
Using this approach you should be able to send the same data to two different forms.

Related

Can not pass the action get param to the PHP file

I have a smarty project, and in the .tpl file, there is a form:
<form method="get" action="{$smarty.server.PHP_SELF}?action=func1">
<input type="text" name="username"/>
<input type="submit">
</form>
there is a question, if the php file have many function for different action requests, so in the template if have many forms, I want to through the action for distinguish.
but in my practice, see upper code, I write like this, this can not delivery the action to my php file.
I want to write the action in the form action, because this can be more standard. so I don't want to write it in a hidden input. why write in the action can not pass into the php file?
You could use submit button with specified name and value:
<form method="get" action="{$smarty.server.PHP_SELF}">
<input type="text" name="username" />
<input type="submit" name="action" value="func1" />
</form>
and then you'll get a global variable $_POST['action'] with value func1. But the value will be showing on your button title, so I offer you to find forms only by submit name, for example name='submit_form1'.

Input fields in one form required for one submit button, but not for other?

How can I differentiate two separate actions for my submit buttons in one form, whereas one SAVES data into database (as is), and other submits form as a valid request, >>but<< according to HTML5 native validation (I use "required" attributes in HTML form)?
My form has 66 fields and I don't want to validate them all in PHP AFTER submission (as it's too cumbersome and time consuming), or validate them all in JS BEFORE submission (as I am not that agile in JS, as in PHP), so I use "required" and "type" attributes in HTML5, which is very convenient and quite versatile way of validation (besides older browsers).
However I cannot find a easy way to bypass the "required" attribute, when using save button, but not using "Send for submission" button. Can you help suggest a solution? I guess the prefered way is some JS/jQuery way, as I cannot determine which button would be used before hitting the button (so that I can make a field required or not).
Sample code:
<form>
A: <input type="text" name="A" required />
B: <input type="text" name="B" required />
C: <input type="text" name="C" />
<input type="submit" name="save" value="Save"> <!--do not require anything-->
<input type="submit" name="send" value="Send for submission"> <!-- require everything-->
</form>
Of course my form is huge, as it has 66 fields, so best solutions here are possibly general and versatile.
Something like this might get you started:
$('#sub1').click(function() {
$('[name=A]').removeAttr('required');
$('[name=B]').removeAttr('required');
$('form').append('<input type="hidden" name="save" />');
$('form').submit();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form>
A: <input type="text" name="A" required /><br>
B: <input type="text" name="B" required /><br>
C: <input type="text" name="C" /><br>
<input id="sub1" type="button" name="save" value="Save">
<!--do not require anything-->
<input type="submit" name="send" value="Send for submission">
<!-- require everything-->
</form>
I've eventually come up with something like this.
HTML:
<input type="submit" name="save" value="Save" onClick="removeRequired(this.form)">
JS (using jQuery):
function removeRequired(form){
$.each(form, function(key, value) {
if ( value.hasAttribute("required")){
value.removeAttribute("required");
}
});
}
This is tested and working. It has this nice property, that I can keep the function elsewhere in files, to not clutch the php/html that is executing it.
Only improvement I'd like (and probably other would be interested in) would be to eliminate jQuery as it's only used for browsing each input element in form, which seems easy enough for pure JS. However I could not find an easy way, and run out of time and used above with jQuery. Thanks
To remove the required attribute from all tags before submitting, modify the code to:
$('#sub1').click(function() {
$('[required]').removeAttr('required');
$('form').append('<input type="hidden" name="save" />').submit();
});
jsFiddle Demo to show it works
Note this answer also demonstrates chaining the jQuery methods: $(tag).append().submit()

HTML/PHP : how can I have multiple form that send informations to the same page?

My problem is this :
I got 2 forms that are supposed to send different informations to the same page (via POST method). Each form has a submit button. However when I press any of the button, information from both forms are sent to the page.
Is it normal or is there something that I do wrong ?
I can already tell you without looking at your HTML.
You have to properly close the first form before opening the second. Easy mistake to make.
<form method="post" action="page.php">
<input type="text" name="something" />
<input type="submit" value="Submit" />
</form>
<form method="post" action="page.php">
<input type="text" name="somethingelse" />
<input type="submit" value="Submit" />
</form>

How to get a form name in php script?

For my php file, I need to grab the unique form name.
The php file is executed when a user clicks the submit button. However, there are multiple submit button each with the same id, but they all have unique names. I need the name when they click on the submit button.
you dont want elements in html with the same id - bad practice in general. Your page will likely load normally but an html validator will notice it as an error.
html validator: http://validator.w3.org/
without seeing your code, its difficult to give you a definitive answer. if you have miltuple forms you can use hidden inputs. e.g.
<input type="hidden" name="form_name" />
Otherwise you can use javascript to put data in the form when the button is clicked. example javascript using jquery
html:
<form id="formid" >
<button type="button" id="someid" onclick="submitForm('btn1')" />
<button type="button" id="someid" onclick="submitForm('btn2')" />
<input type="hidden" id="btnsubmitid" value="" />
</form>
js:
function submitForm(btnID){
$("#btnsubmitid").val(btnID);
$("#formid").submit();
}
1 way is to put a hidden input inside of your form.
<input type="hidden" name="formName" value="[name of form]" />
then in your php, you can get it using
$form-name = $_POST['formName'];
pretty sure there are other ways, but this came to mind first.

GET variables not passed when mixed in with URL

Why is it that when I pass parameters through encoded URL and GET method in HTML form, the URL encoded parameters are dropped?
e.g. Setup:
<form action="process.php?hello=world" method="GET">
<input type="text" name="foo" value="bar">
<input type="submit">
</form>
Result: the variable hello will not be recognized in process.php.
Is this bad practice?
Is this how PHP processes it, or is it related to how the browser send the request? Is there the same problem in other languages?
Yes, that is bad practice because it just doesn't work.
If you want to pass in "hidden form input" then you must use a hidden form element:
<input type="hidden" name="hello" value="world" />
As rezzif states in his comment, you can mix GET & POST like so:
<form action="/something?foo=bar" method="POST">
<input type="text" name="baz" />
</form>
As a general rule I avoid mixing the two though. I find it bizarre to have GET params in my form action.

Categories