Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
113
rated 0 times [  114] [ 1]  / answers: 1 / hits: 32123  / 11 Years ago, mon, october 14, 2013, 12:00:00

I'm trying to add a button to my form that would essentially run some different php code than my regular form submission code, that would, instead of emailing me the form, convert my page into a nice pdf ready to print. I have everything working except that pressing the button is giving me an error.



Firebug says :
enter



Here's the code:



<form id=adaugareanunt name=adaugareanunt action=mailerPDF.php method=post>
<table width=535 border=0 cellspacing=2 cellpadding=3>
<tr class=TrDark>
//... more form code


and for the button:



<div style=text-align:right><img src=images/print-button.png onClick=chgAction() width=60px height=20px></div>


with the script:



<script language=JavaScript type=text/JavaScript>
function chgAction()
{
document.getElementById[adaugareanunt].action = mailerXFDF.php;
document.getElementById[adaugareanunt].submit();
document.getElementById[adaugareanunt].action = mailerPDF.php;

}
</script>

More From » jquery

 Answers
0

const EL_form = document.getElementById("form"); 
EL_form.action = "someOtherURL.php";
EL_form.submit();
// PS! Make sure you don't have any name="submit" inputs in your form

Don't use inputs with name="submit"


Make also sure, if you want to use the .submit() method - that you don't have any name="submit" input in your form. Call it differently if really needed like i.e name="button_submit".


Here's the issue with name="submit" inputs: they overtake the function submit since any element with a set name attribute becomes a property of that form Element.

Example of the problem:




// EXAMPLE OF THE ISSUE:

const EL_form = document.getElementById(form);

// Why does EL_form.submit() not work?

console.log(EL_form.submit); // It's the actual INPUT with name=submit
console.log(EL_form.submit()); // Therefore the Not a function error.

<form id=form>
<input type=submit name=submit>
</form>




[#74998] Sunday, October 13, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leog

Total Points: 225
Total Questions: 113
Total Answers: 118

Location: Oman
Member since Wed, Apr 12, 2023
1 Year ago
;