tell me there are 2 buttons

<form method="POST"> <input type="submit" name="nazvanie_knopki" value="НаТмитС" /> <input type="submit" name="nazvanie_knopki2" value="НаТмитС" /> </form> 

and different content for the site in php

 if( isset( $_POST['nazvanie_knopki'] ) ) { echo '1';} elseif ( isset( $_POST['nazvanie_knopki2'] )) {echo '2';} 

But when you click on the buttons in the form is reloading the page. How to do without rebooting ??

Thanks in advance!

2 answers 2

We will show, but it will be better if you learn to use the search.

 <!doctype html> <html> <head> <meta charset="utf-8"> </head> <body> <input class="ajax-form" type="submit" name="nazvanie_knopki" value="НаТмитС" /> <input class="ajax-form" type="submit" name="nazvanie_knopki2" value="НаТмитС" /> <input class="ajax-form" type="submit" name="nazvanie_knopki3" value="НаТмитС" /> <div id="ajax-content">0</div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> $(".ajax-form").click(function(e) { var form = {}; form[$(this).attr('name')] = 1; $.ajax({ url: '/ajax.php', type: "POST", data: $.param(form), success: function(data) { $('#ajax-content').html(data); } }); return false; // e.preventDefault(); - Π»ΠΈΠ±ΠΎ Ρ‚Π°ΠΊ }); </script> </body> </html> 

Sobsna itself ajax.php :

 <?php if (isset($_POST['nazvanie_knopki'])) { echo '1'; } elseif (isset($_POST['nazvanie_knopki2'])) { echo '2'; } else { echo '3'; } 

    Use e.preventDefault()

    jQuery

     $('form').on('submit', (e) => { e.preventDefault(); //Ρ‚ΠΎ, Ρ‡Ρ‚ΠΎ Ρ‚Π΅Π±Π΅ Π½ΡƒΠΆΠ½ΠΎ. Π­Ρ‚ΠΎ ΠΏΡ€Π΅Ρ€Ρ‹Π²Π°Π΅Ρ‚ стандартноС дСйствиС Π±Ρ€Π°ΡƒΠ·Π΅Ρ€Π° // ...ΠΊΠΎΠ΄ }); 
    • Stop and just return false in this case. - And