1

I have a form, with the 3 input type text.

<input type="text" id="one" onkeyup="multiply()" name="one">
<input type="text" id="two" name="two">
<input type="text" id="three" name="three">

<script>
function multiply(){
    one = document.getElementById('one').value;
    two = document.getElementById('two').value;
    document.getElementById('three').value = one * two
}
</script>

now i don't have value in three, but it is a dynamic one, when i submit forum to (forumSubmit.php)then i get error of

undefiend index three

I searched & found this can be done with ajax, but i don't want to use ajax, i want to make a page refresh

5
  • If you set the value-attribute of that element through JavaScript prior to posting the form, the value will be passed along as the form is submitted. Commented Sep 9, 2012 at 13:10
  • 1
    please review your code, a lot of stuff doesn't work like this Commented Sep 9, 2012 at 13:10
  • what do you mean by setting the dynamic value of javascript ? Commented Sep 9, 2012 at 13:11
  • @dbf I copied that's why :p but it is not working yet.. Commented Sep 9, 2012 at 13:13
  • If you want data to be processed by server, use a plain form and don't mess with js - as simple! Commented Sep 9, 2012 at 13:19

2 Answers 2

1

You could do something like this instead:

Markup

<!-- Use onkeyup on both inputs -->
<input type="text" id="one" onkeyup="multiply()" name="one">
<input type="text" id="two" onkeyup="multiply()" name="two">
<input type="text" id="three" name="three">
​

JavaScript

function multiply() {
   // Parse the values, and count it as 0 if the input is not a number
   // I also made the variables private to this function using the var keyword
   // There is no need to have them in the global namespace
   var one = parseInt(document.getElementById('one').value, 10) || 0;  
   var two = parseInt(document.getElementById('two').value, 10) || 0;
   document.getElementById('three').value= one * two;
}​

Working example

Put together a demo: http://jsfiddle.net/DjQNx/

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

Comments

0
<input type="text" id="one" onkeyup="multiply()" name="one" />
<input type="text" id="two" onkeyup="multiply()" name="two" />
<input type="text" id="three" name="three" />

<script type="text/javascript">
function multiply() {
    one = document.getElementById('one').value;
    two = document.getElementById('two').value;
    document.getElementById('three').value = one * two;
}
</script>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.