Ajax, send data without updating page

Asked

Viewed 45 times

0

Well, I did a Restful API integration, which searches all products from one e-commerce and sends Json to another site, and needed that when clicking the "Submit" button, the page did not update.

My problem is this, there is no data to insert in form, basically I use the form just to put the upload button

        <div style="position:absolute; margin-left: 65%;margin-top: -500px">
<form action="" method="post" id="formenv">
<button style="width:60px" type="submit"     class="btn btn-primary" id="expevt" name="expevt">
     <dt style="text-align:center">
        <span class="glyphicon glyphicon-export"></span>
        <b></b>
        </button>

I only know how to use ajax to send the data of a form, however, this button, just calls the integration Post,

    if(isset($_POST))
{
    if (isset($_POST['expevt']))
    {
        $post2 = new Posts();

        if(isset( $_POST['catwooid']) && $_POST['catwooid'] != '')
        {
            $post2->postOneCat($_POST['catwooid']);
        }else
        {
            $post2->postAllCat();
        }
        $post = new Posts();
        if(isset( $_POST['prodwoosku']) && $_POST['prodwoosku'] != '')
        {
            $post->postOneProd($_POST['prodwoosku']);
        }else
        {
            $post->postAllProd();
        }
    }
}

Would have like me to do the post without giving refresh?

  • There is a script in which this integration is run, correct? Do you have a form with a button in which there is no action, the script runs on the same page? The codes were a little confused in context, I imagine that the solution is very simple if you have a file already running all this integration, just click be captured, make a prevendefault() and call the script in ajax...

1 answer

-1

You can put a return false in your form and the rest makes normal

$('#expevt').on('click', function(){
   alert('Aqui você vai fazer seu ajax enviar' )
   //$.ajax()
})
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>

<script
  src="https://code.jquery.com/jquery-3.3.1.min.js"
  integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
  crossorigin="anonymous"></script>
  
  
<div >
        <form action="" method="post" id="formenv">
             <button style="width:60px" type="submit"    class="btn btn-primary" id="expevt" name="expevt">
                  <dt style="text-align:center">
                  Botão
                  <b></b>
                  </dt>
              </button>
        </form>
    </div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.