Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!