Form with PHP sends POST variables to same page with jQuery without reloading [closed]

-1

This basic code below will be used to filter via POST AND jQuery a SELECT on a PHP page.

The form sends to the same page, jQuery receives and sends to PHP, which receives via $_POST["campanha"]; .

The page should not be giving refresh but this is happening. Can someone help me with this error, please?

<html> 
<head>
      <script type="text/javascript" src="http://code.jquery.com/jquery-1.5.min.js"></script><scripttype="text/javascript">
        $(function(){
           $("#bot_enviar").click(function(){
            event.preventDefault();
               var varCampanha = $("#campanha").val();
               $.post('teste.php', {
                   type:"POST",
                   campanha: varCampanha
               }, function(response){
                    alert(campanha);
               });
           });
        })
    </script>
</head> 
<body>

<form action="" method="post" id="bot_enviar">
    <select name="campanha" name="campanha">
      <option value="1">1</option>
      <option value="3">3</option>
    </select>
<br>

<input type="submit" >
</form>



Campanha: <?php echo $_POST["campanha"]; ?>
</body>
</html>
    
asked by anonymous 16.08.2018 / 22:52

2 answers

2
___ erkimt ___ Form with PHP sends POST variables to same page with jQuery without reloading [closed] ______ qstntxt ___

This basic code below will be used to filter via POST AND jQuery a <input type="submit" > on a PHP page.

The form sends to the same page, jQuery receives and sends to PHP, which receives via click .

The page should not be giving refresh but this is happening. Can someone help me with this error, please?

%pre%     
______ ___ azszpr322949

There are some errors in your code. Your id="bot_enviar" is the button that sends and reloads the page. You did not put the event click on this button, but put it on form. Note that you put submit in the form instead of putting it in the button.

You should put the event click on the button or else put the event click on the form. What it does not do is put the event id="bot_enviar" in the form.

If you prefer to place the% <input type="button" id="bot_enviar"> event on the button, so take the $("#bot_enviar").click % of form and place it on the button. I would also recommend switching to $("#bot_enviar").submit .

If you prefer to place the event in the form, simply change the %code% to %code% . However, in that case I would change the name of the id to something that clearly demonstrates that it is the form, not the button.

    
______ azszpr322950 ___

Complementing the response of @ victor-stafusa (The changes he cited are essential):

You are calling the %code% method without invoking the event parameter in the click event:

%pre%

The correct one would be:

%pre%     
___
16.08.2018 / 23:35
1

Complementing the response of @ victor-stafusa (The changes he cited are essential):

You are calling the preventDefault() method without invoking the event parameter in the click event:

$("#bot_enviar").click(function(){ 

The correct one would be:

$("#bot_enviar").click(function(event){ 
    
16.08.2018 / 23:38