1

This is my code :

<html>
    <head>
        <title>Magento 2 </title>
        <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    </head>
    <body>
        <script type="text/javascript">
                function ajaxCall(){
                    $.post( "insert.php", function( data ) {
                        alert(data);
                        //$( ".result" ).html( data );
                    });
                }
        </script>
        <a href ="" onclick ="javascript:ajaxCall();"style="text-decoration:none;"><input type ="button" value="Executa"></a>
    </body>
</html>

the insert.php file contains:

<php 
   echo 1;
?>

I don't know what i wrote wrong ? It doesn't alert ! I tried to see also with firebug, but it doesn't display anything. Im wondering what im doing wrong ?. Thx

Attila Naghi
  • 2,535
  • 6
  • 37
  • 59

7 Answers7

2

in your href="" add a "#" or javascript:void(0)

like this it will work.

 <a href ="javascript:void(0)" onclick ="javascript:ajaxCall();"style="text-decoration:none;"><input type ="button" value="Executa"></a>
Maz I
  • 3,664
  • 2
  • 23
  • 38
2

Prevent the default event of the a tag:

Javascript

function ajaxCall(event){
    event.preventDefault();
    $.post( "insert.php", function( data ) {
        alert(data);
                        //$( ".result" ).html( data );
    });
}

HTML

<a href ="" onclick ="javascript:ajaxCall(event);"style="text-decoration:none;"><input type ="button" value="Executa"></a>
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
1

Different name for onclick handle and the real function.

 <a href="" onclick="ajaxCall();"style="text-decoration:none;"><input type="button" value="Executa"></a>
Mardie
  • 1,663
  • 17
  • 27
1

Using a inside an tag doesn't work with every browser. Try to replace the with a .

nowhere
  • 1,558
  • 1
  • 12
  • 31
1

need href ="javascript:void(0);"

try this

<a href ="javascript:void(0);" onclick ="ajaxCall();"style="text-decoration:none;"><input type ="button" value="Executa"></a>
Satish Sharma
  • 9,547
  • 6
  • 29
  • 51
0

just put that onclick functinality on the button. and avoid the anchor tag. anchor tag by default got a functionality.

<input type ="button" value="Executa" onclick ="javascript:ajaxCall();"/>
Joe
  • 618
  • 1
  • 9
  • 18
0

add "return false;" to the end of onclick function;

and

between onclick and style should have space :)

like this

<a href ="" onclick ="javascript:ajaxCall();return false;" style="text-decoration:none;"><input type ="button" value="Executa"></a>
liu lei
  • 41
  • 4