How can I set the form action through JavaScript?
You cannot invoke JavaScript functions in standard HTML attributes other than onXXX
. Just assign it during window onload.
<script type="text/javascript">
window.onload = function() {
document.myform.action = get_action();
}
function get_action() {
return form_action;
}
</script>
<form name="myform">
...
</form>
You see that I've given the form a name
, so that it's easily accessible in document
.
Alternatively, you can also do it during submit
event:
<script type="text/javascript">
function get_action(form) {
form.action = form_action;
}
</script>
<form onsubmit="get_action(this);">
...
</form>
Set form action with JavaScript
A simpler solution might be to add name
s to the hidden inputs then append them to the form so they get submitted by default process.
HTML
<input type="hidden" id="firstParameter" name="firstParameter" value="parameter">
<input type="hidden" id="secondParameter" name="secondParameter" value="anotherParameter">
JS
$('form').submit(function(){
const $form = $(this);
$('#firstParameter, #secondParameter').each(function(){
if(this.value){
$form.append(this)
}
});
});
How to use JavaScript to change the form action
function chgAction( action_name )
{
if( action_name=="aaa" ) {
document.search-theme-form.action = "/AAA";
}
else if( action_name=="bbb" ) {
document.search-theme-form.action = "/BBB";
}
else if( action_name=="ccc" ) {
document.search-theme-form.action = "/CCC";
}
}
And your form needs to have name
in this case:
<form action="/" accept-charset="UTF-8" method="post" name="search-theme-form" id="search-theme-form">
form action with javascript
A form action set to a JavaScript function is not widely supported, I'm surprised it works in FireFox.
The best is to just set form action
to your PHP script; if you need to do anything before submission you can just add to onsubmit
Edit turned out you didn't need any extra function, just a small change here:
function validateFormOnSubmit(theForm) {
var reason = "";
reason += validateName(theForm.name);
reason += validatePhone(theForm.phone);
reason += validateEmail(theForm.emaile);
if (reason != "") {
alert("Some fields need correction:\n" + reason);
} else {
simpleCart.checkout();
}
return false;
}
Then in your form:
<form action="#" onsubmit="return validateFormOnSubmit(this);">
Form action using javascript
Going out on a limb here. Going to assume you're really just asking why the form isn't submitting, in which case it's because you're missing the form-submit:
var form = document.getElementById("form4");
form.action = "http://localhost/profile_book/login_key.php";
form.submit()
change forms action with javascript
Firstly, the value in your JavaScript should be quoted, as it is a string:
function submitForm(){
document.getElementById('form1').action = '<?echo base_url().'index.php/folder/controller/controller_function/';?>';
document.getElementById('form1').submit();
}
Secondly, you're triggering the function on the change
event of a button, which probably won't work because buttons do not change. Try binding to the submit
event instead, on the form itself:
<form id="form1" name="form1" action="" method="post" onsubmit='submitForm();'>
But I'd do it in the JavaScript, using an event listener:
document.getElementById('form1').addEventListener('submit', function(e) {
e.preventDefault();
this.action = '<?echo base_url().'index.php/folder/controller/controller_function/';?>';
this.submit();
}, false);
How do I conditionally set a form action via Javascript?
Your code should work
In any case here is simpler version
document.getElementById("confirmForm").addEventListener("submit",function() {
this.method="post"
this.action = document.getElementById('confirmOptionCompare').checked ? "comparison.php": "bookConfirm.php";
})
<form id="confirmForm">
<p>Click confirm to go to the next step:</p>
<button type="submit" name="confirm">Confirm</button>
</form>
<input type="checkbox" name="confirmOptionCompare" id="confirmOptionCompare"/>
Related Topics
Submit Two Forms With One Button
What Does "./" (Dot Slash) Refer to in Terms of an HTML File Path Location
Get File Size, Image Width and Height Before Upload
Jquery Document.Createelement Equivalent
How to Communicate Between Iframe and the Parent Site
Is It Wrong to Place the ≪Script≫ Tag After the ≪/Body≫ Tag
How to Dynamically Change a Web Page'S Title
How to Append Text to a '≪Div≫'
Reading File Contents on the Client-Side in JavaScript in Various Browsers
Make Iframe Automatically Adjust Height According to the Contents Without Using Scrollbar
When to Use Functional Setstate
How to Get the Element Clicked (For the Whole Document)
When Is Nodelist Live and When Is It Static
Refresh a Page Using JavaScript or Html
Getting the Parent Div of Element
How to Style "Input File" With Css3/JavaScript