tags:

views:

62

answers:

3

Hi I have the following script in my form

function pdf() {
var frm = document.getElementById("form1");
frm.action = "http://www.abbysoft.co.uk/index.php";
frm.target="_blank"
frm.submit()
}

this is called from the following in my form <input class="buttn" type="button" value="Test" onclick="pdf()"

The code work up to the frm.submit() but it will not submit

Can anyone offer any advice please ?

+2  A: 

You should end your statements with ;. The following should work

function pdf()
{
    var frm = document.getElementById('form1');
    frm.action = 'http://www.abbysoft.co.uk/index.php';
    frm.target = '_blank';
    frm.submit();
}

assuming you have a form like this:

<form id="form1" action="#">
    <input class="buttn" type="button" value="Test" onclick="pdf()" value="Test" />
</form>

Also make sure that by any chance you don't have an input with name submit inside your form as this would override the submit function:

<input type="text" name="submit" />
Darin Dimitrov
No. While it is good style to explicitly end statements with semi-colons because certain cases don't otherwise end the statement where you might expect, that isn't the problem here.
David Dorward
@David, I suggested this as a good practice, I agree with you that probably is not the issue here, but you never know, what if the script is compressed with a packer?
Darin Dimitrov
A decent packer will fix that. If it isn't a good packer then I would expect it to error before successfully setting the target property.
David Dorward
+1  A: 

Make a form like this:

<form id="form1" action="" onsubmit="pdf();return false;">
    <input class="buttn" type="submit" value="Test" value="Test" />
</form>
M28
this is a better approach but Darin Dimitrov is right about the syntax error. Also he should just return true in pdf() instead of submiting and returning false shouldn't he?
the_drow
Is he submiting the same form?
M28
Its very difficult to tell what is actually being suggested here, but it looks like it boils down to "Use onsubmit instead of onclick" — that won't solve the problem, and likely wouldn't fit the situation which looks like wanting to submit to a different address if a *particular* button is clicked.
David Dorward
A: 

You haven't given us the code you are using to create the form, nor have you told us what (if any) errors are reported by the browser, however, the usual cause for this issue is having a submit button named or ided submit.

Any form control is accessible from the form object with a property that matches its name (and another one that matches its id if that is different). This clobbers any existing properties of the form (other than other controls) including the submit and reset methods.

The simplest solution is to rename the control so it doesn't conflict with an existing property.

Alternatively, see http://stackoverflow.com/questions/1999891/how-to-reliably-submit-an-html-form-with-javascript

David Dorward