1

I was trying to add the two operator using javascript and then I wished to try it in ajax then I wanted to see the difference between two codes action but whenever I implemented the addition in javascript I got stuck because thepage show the result for a sec then it redirect back. Can anyone please tell me what is the wrong with this code?

<!DOCTYPE html>
<html>
<head>
    <title>ajax</title>
    <script>
        function loadFunction(){
            //window.alert("hello");
            var x= document.getElementById("d1");
            var y =document.forms["myForm"]["op1"].value;
            var z =document.forms["myForm"]["op2"].value;
            x.innerHTML="<h2>" +(parseInt(y)+parseInt(z)) + "</h2>";
            x.style.width="35%";
            x.style.border="1px solid #050";    
        }
    </script>
</head>
<body>
    <form id="myForm" onsubmit="loadFunction()" method="POST">
        <input type="text" id="op1" placeholder="operator 1" required></input> + 
        <input type="text" id="op2" placeholder="operator 2" required></input>
        <button type="submit">show the result</button>
    </form>
    <div id="d1">
        <h2>The summation will print here!!!</h2>
    </div>      
</body>
</html>
Ethereal soul
  • 709
  • 1
  • 7
  • 20

2 Answers2

3

The default action that browser performs each time you submit the form is to make those redirections. You have to explicitly turn this off. To do that add a event.preventDefault(), like this:

<script>
    function loadFunction(event){
        //window.alert("hello");
        var x= document.getElementById("d1");
        var y =document.forms["myForm"]["op1"].value;
        var z =document.forms["myForm"]["op2"].value;
        x.innerHTML="<h2>" +(parseInt(y)+parseInt(z)) + "</h2>";
        x.style.width="35%";
        x.style.border="1px solid #050";

        event.preventDefault();    
    }
</script>
Tomek Sułkowski
  • 7,081
  • 2
  • 17
  • 15
  • You have to add `event` also to where you attach the function to your form here: `
    `. Here's a working demo: http://jsbin.com/picalu/edit?html,output
    – Tomek Sułkowski Jun 24 '15 at 19:18
  • yes its working now ..thanks for the help.@Tomek Sułkowski ..i need to know one more thing .if its reloading the page or not? – Ethereal soul Jun 24 '15 at 19:20
  • 1
    No, it's not. If you prevent the default action, the only thing that's happening is what you have in your `loadFunction` function. – Tomek Sułkowski Jun 24 '15 at 19:23
  • can you please show an example between non ajax javascript and ajax javascript?@Tomek Sułkowski – Ethereal soul Jun 24 '15 at 19:27
  • To understand better what AJAX is and how to use it try one of those tutorials: https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started or http://www.w3schools.com/ajax/ – Tomek Sułkowski Jun 24 '15 at 19:49
1

The other possibility is to return false in onsubmit like that:

<script>
    function loadFunction(){
        //...
        return false;
    }
</script>

and

<form id="myForm" onsubmit="return loadFunction()" method="POST">

You can then control if form should be submitted by returning either true or false from loadFunction().

Furgas
  • 2,729
  • 1
  • 18
  • 27