Muhammad Arslan Jamshaid
Muhammad Arslan Jamshaid

Reputation: 1197

how to send javascript dynamic data to php variable without ajax

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

Upvotes: 1

Views: 1434

Answers (2)

Christofer Eliasson
Christofer Eliasson

Reputation: 33865

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/

Upvotes: 1

rationalboss
rationalboss

Reputation: 5389

<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>

Upvotes: 0

Related Questions