Page 1 of 1
[solved] triggering form submit in a javascript function?
Posted: Thu Oct 06, 2005 7:52 am
by robster
Hello all
I have a function which, at the start, sets a variable called
pass to "true". It then checks all sorts of things and changes
pass to "false" if certain conditions are met.
What I want to then do, at the end of the function, is submit the form (named
pos_trans_add) via something like this:
Code: Select all
if ($pass == "true")
{
document.pos_trans_add.submit();
}
This of course does not work, but how can I do something similar inside this funtion? I have to say I'm BRAND spanking new at javascript, and very excited at what it can do to my forms!
Rob
Posted: Thu Oct 06, 2005 8:10 am
by n00b Saibot
does

document.forms[0].submit() work

Posted: Thu Oct 06, 2005 8:18 am
by robster
I've been playing around with lots of things since my post (as you do

) and got to this:
Code: Select all
if ($pass == "true")
{
alert('PASS = TRUE');
document.form['pos_trans_add'].submit;
}
else
{
alert('PASS = FALSE');
document.form['pos_trans_add'].submit;
}
The thing is, no alert box happens on either. So I suspect the form will trigger, it's just a silly pass = true thing...
Hold on, I am guessing a $ is not needed in javascript

(PHP me thinking all languages are the same again).
(checking now...)
mmmm, that got the pass = true or pass = false dialogue up, but it didn't run the form. I then removed the alert dialogue code and it still didn't submit the form....
I'm stumped.
Posted: Thu Oct 06, 2005 8:50 am
by feyd
if you have an element named "submit" inside the form you will get no submission via JS..
Posted: Thu Oct 06, 2005 6:12 pm
by robster
No, double checked that, there's no elements called submit within the form.
This is what the form looks like (minus the content), if that helps?
Code: Select all
<form name="pos_trans_add" method="post" action="pos_trans_add.php">
blah blah blah
</form>
When the javascript is run it definately does not go to action (pos_trans_add.php).
The button that triggers the javascript function looks like this though, maybe this can help?
Code: Select all
<!--BUTTON-->
<td rowspan="2" align="center" valign="middle">
<img name = "button_add" src="images/button_add.png"
onClick="check_transaction_type()"
onMouseOver="this.style.cursor='pointer'"
onMouseDown="document.button_add.src = 'images/button_add_down.png'"
onMouseOut="document.button_add.src = 'images/button_add.png'">
</td>
and just to be thorough, this is what the function looks like:
Code: Select all
<SCRIPT LANGUAGE = "JavaScript">
<!--
function check_transaction_type() {
var pos_employee = document.pos_trans_add.pos_employee;
var pos_product = document.pos_trans_add.pos_product;
var pos_service = document.pos_trans_add.pos_service;
var selected_employee = pos_employee.options[pos_employee.selectedIndex].value;
var selected_product = pos_product.options[pos_product.selectedIndex].value;
var selected_service = pos_service.options[pos_service.selectedIndex].value;
//Let's assume they passed the tests before we go ahead, we will change to false later if there's drama
pass = "true";
//if no EMPLOYEE has been chosen, let them know it
if (selected_employee == "")
{
alert('You have not chosen an Employee. Please do so before continuing with the transaction.');
pass = "false";
}
//if nothing has been chosen, let them know it
if (selected_service == "")
{
if (selected_product == "")
{
alert('No Service or product has been chosen. At least one service or product must be chosen.');
pass = "false";
}
}
//both Service AND Product have been chosen at the same time, let them know it
if (selected_service != "")
{
if (selected_product != "")
{
alert('You can only choose a service OR a product, not both at the same time. Please change your choice to only choose a product OR a service.');
pass = "false";
}
}
//if we passed the tests, then go ahead and submit the form, otherwise don't go anywhere (as we've shown the error already)
if (pass == "true")
{
document.form['pos_trans_add'].submit;
}
else
{
// do nothing... we've already given them their error messages
}
}
-->
</SCRIPT>
Posted: Thu Oct 06, 2005 6:14 pm
by feyd
and check_transaction_type() is what?
Posted: Thu Oct 06, 2005 6:15 pm
by robster
apologies, I just edited my post above to include it as you were asking

Posted: Thu Oct 06, 2005 6:30 pm
by feyd
your code is requesting the form method, not running it
i.e.
Code: Select all
document.form['pos_trans_add'].submit;
// should be
document.form['pos_trans_add'].submit();
Posted: Thu Oct 06, 2005 6:48 pm
by robster
I changed it to that, it didn't work
I then added an alert box just to be sure. Here's the code at present:
Code: Select all
if (pass == "true")
{
alert('pass = TRUE');
document.form['pos_trans_add'].submit();
}
else
{
// do nothing... we've already given them their error messages
alert('pass = FALSE');
}
As it is, the alert pops up with 'pass = TRUE', but no form submits

It's SO strange.
I wonder, this is a file that's being called from another file, let me look into that.
Posted: Thu Oct 06, 2005 6:51 pm
by robster
no, even running the file by itself it still doesn't submit.
Posted: Thu Oct 06, 2005 6:51 pm
by feyd
document.forms
not document.form

Posted: Thu Oct 06, 2005 6:55 pm
by robster
WOOOOOOOOOOO HOOOOOOOOOOO!
Feyd, you need one of those, don't applaud, throw money boxes.
I just want to thank you somehow, do you have an amazon wishlist or something?!
I find this kind of learning invaluable and the way you've just held my hand through that, well, I appreciate it a LOT.
Really, thanks
Rob
Posted: Fri Oct 07, 2005 1:12 am
by n00b Saibot
I wonder why document.forms[0].submit() didn't work though
