1

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 ?

Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
Mick
  • 2,840
  • 10
  • 45
  • 61

3 Answers3

2

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
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 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. – Quentin Mar 28 '10 at 13:33
  • @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 Mar 28 '10 at 13:40
  • 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. – Quentin Mar 28 '10 at 13:49
1

Make a form like this:

<form id="form1" action="" onsubmit="pdf();return false;">
    <input class="buttn" type="submit" value="Test" value="Test" />
</form>
  • 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 Mar 28 '10 at 13:23
  • Is he submiting the same form? –  Mar 28 '10 at 13:32
  • 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. – Quentin Mar 28 '10 at 13:43
0

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 How to reliably submit an HTML form with JavaScript?

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335