dfin Posted June 11, 2015 Share Posted June 11, 2015 (edited) I have a form where I need the text value to be passed to another form element later on. Right now, it have <form method='post' action='https://secure.autho...ay/transact.dll' > <input type='text' name='amount' value='50.00' /> Change: <input type="checkbox" name="chg" onclick="myF(this.form)"> <script type="text/javascript"> function myF(val) { var num = val; var n = num.toString(); alert("You have now changed the amount to pay: " + n ); } </script> <input type='text' name='x_amount' value='<?php echo $_REQUEST['num'] ?>' /> <input type='Submit' value='Pay Now'> </form> It will then return the Alert box & in place of + n, it says object HtmlFormElement. I tried doing a document.getElementById(this.form), but I think that gave me a null. I know I will later need to do a validation to make sure it is a number & 2 decimals, but for now I need my alert & variable to be passed right. I've tried + n, + val, had it to where it didn't create the toString...nothing works Edited June 11, 2015 by dfin Quote Link to comment Share on other sites More sharing options...
Solution CroNiX Posted June 11, 2015 Solution Share Posted June 11, 2015 (edited) A few things. 1) you can't use getElementById() unless there is actually something with the ID. You're not using ANY ID's on any of your form elements so it won't work. 2) onclick="myF(this.form)" You're sending the ENTIRE form to myF(). That's why you're getting an "object" in the alert() Try changing these lines: //give this form field an ID of 'amount' <input type='text' name='amount' value='50.00' id="amount" /> //no need to pass anything to the myF() function Change: <input type="checkbox" name="chg" onclick="myF()"> <script type="text/javascript"> function myF() { //get value from the form field that has the id of 'amount' var amount = document.getElementById('amount').value; alert("You have now changed the amount to pay: " + amount ); //alerts 50.00 unless value changed } </script> Edited June 11, 2015 by CroNiX Quote Link to comment Share on other sites More sharing options...
dfin Posted June 12, 2015 Author Share Posted June 12, 2015 ahhh.. thanks so much. I'm quite rusty and just getting back into this. i had some id's stuck here and there and didn't put em back with other trial and error's. i have it now also submitting to the value of another input field also. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.