0

I have used this function to submit to any url or 'self', with or without a querystring, many times without any problems.

function submitu(url, q) {
 var frm = document.<?php echo $formname ?>;
 if (url == '') {url = '<?php echo $thispage?>'; }
 frm.action = url+q; frm.submit(); }

If I try to move the PHP vars outside the function, as below, it stops working (frm undefined error)

var thispage = '<?php echo $thispage?>';
var frm = document.<?php echo $formname?>;
function submitu(url, q) {
 if (url == '') {url = thispage;}
 frm.action = url+q; frm.submit();}

I also tried var frm = document.forms[''];

I don't have any other conflicting javascript,

(1). Can anyone tell me why this is not working? (2). And why the first method also fails if the function is placed inside and at the top of the jquery $(function() {.....} ready function?

Many thanks

1 Answer 1

1

It fails because when outside the function, var frm = document.formname; will be run immediately when the page loads, i.e. before the form element has actually been constructed, so you get 'undefined'. When inside the function, it is only run when the function is run, by which time the DOM is complete and it can find it.

Sign up to request clarification or add additional context in comments.

3 Comments

Perfect,now I understand - Thank you Matt
Matt, exactly how does one 'accept' an answer, when someone answers a question, you instinctively look to see how you can reward them.
It's part of how the site works: there's a tick icon under the voting icons just to the left of each answer, which goes green when you click it. It's signal for other users that this answer worked for you and answers the question you asked (and does so better than any of the others that may have been posted if that's the case). It's seen as a good thing because it shows an authoritative answer has been found that other people can rely on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.