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

160
Visualizações
Ajax Call Is Not working properly for a specific if else statement of a particular query

Here is my code.I am pretty sure my code is right but it is not functioning properly just for the last query.All the remaining queries are working well and fine but the last query is not returning value back with some object which is not accessible however the updation of table is occurring . What is wrong? I am attaching the pic of the error being displayed.Error Image

enter image description here

First the code for frontend page :

<!DOCTYPE html>
<head>
</head>
<body>
<form>
<input type="text" id='dat'>
<button id='submit'>Submit</button>
</form>
<script src = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.2.3.js">
</script> 
<script>
$(function(){
    $('#submit').click(function(){
       var x=$('#dat').val();
       $.ajax({
           type:"GET",
           url: "promo_code_apply2.php",
           dataType: "json",
           data: { x : x },
           success: function(result){
               alert(result);
           },
           error: function(e) {
            // Handle error here
               console.log(e);
           }
     });
   });
});
</script>
</body>

Now the code for backend :

            <?php
        include('connection.php');
        include('functions.php');
        $p_code=$_GET['x'];
        $sql="select * invite_codes where promo_name='".$p_code."'";
        $res=mysqli_query($con,$sql);
        if(!$res)
        {
            $data=mysqli_error($con);
            echo json_encode($data);
            die();
        }
        else{
        if(mysqli_num_rows($res)==0)
        {
            $data="Invalid Code";
            echo json_encode($data);
        }
        else{
            $row=mysqli_fetch_assoc($res);
            $ph=$row['user_mob'];
            $sql2="update user_signup set no_of_referrals=no_of_referrals+1 where promo_name='".$p_code."'";
            $res2=mysqli_query($con,$sql2);
            if(!$res2)
            {
                $data=mysqli_error($con);
                echo json_encode($data);
                die();
            }
            else
            {
            $data="Conngrats";
            echo json_encode($data);
            }
        }
        }
        ?>
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You didn't set your button type and as it is inside the form its default type is submit. When your button is pushed your jQuery event handler is called, but after its completion the default action is called which is submit form action. It reloads your page and your jQuery handler wont get data back from PHP, because JavaScript state resets after reload. In order to prevent this behaviour you can either:

  1. Put return false at the end of your jQuery event handler - like this:

    $(function(){ $('#submit1').click(function(){ ... }); return false; // <--- prevents default action }); });

or

  1. Set button type type="button"
over 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