Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

133
Visualizações
jQuery Ajax Post with data

why it isn't working. Am trying to call a php file when onclick of a button occurs with some parameters. It is getting executed till alert statement in jsfile.js. After that the ajax part is not getting executed.. Help me out.. Thanks in advance..

main.html

<!DOCTYPE html>
<html>
<head>
    <title></title>

<script src="jsfile.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
        <button onclick="cart(0)"> hi </button>
        <p id="disp"></p>
</body>
</html>

jsfile.js

function cart(id1)
{

    var id=id1;
    alert("enterd "+id);
    document.getElementById("disp").innerHTML ="hi";
        $.ajax({
        url:"/add.php ",
        type:"POST",

        data:{
          item_id: id,
        },
        success:function(response) {
          //document.getElementById("total_items").value=response;
         document.getElementById("disp").innerHTML =response;
       },
       error:function(){
        alert("error");
       }

      });

}

add.php

<?php
    if(isset($_POST['item_id']) && !empty($_POST['item_id'])){
    //if(count($_POST)>0)
        echo "success";
        exit();
    }
?>
about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

In your jsfile.js file, please correct the following points which I have mentioned as comments on the following code.

function cart(id1)
{
    var id=id1;
    alert("enterd "+id);
    document.getElementById("disp").innerHTML ="hi";
        $.ajax({
        url:"/add.php ",
        method:"POST", //First change type to method here

        data:{
          item_id: id,
        },
        success:function(response) {
         document.getElementById("disp").innerHTML =response;
       },
       error:function(){
        alert("error");
       }

      });

}
about 4 years ago · Santiago Trujillo Relatório

0

Change it and try

 type:"POST" ->  method: "POST"

If your jquery version is >= 1.5 use it this way.

$.ajax({
  url: "add.php",
  method: "POST",
  data: { item_id: id},
}).done(function(response) {
  ......
}).fail(function( jqXHR, textStatus ) {
  ......
});

Load Jquery Before your JS file.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda