0

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);
            }
        }
        }
        ?>
Junius L
  • 15,881
  • 6
  • 52
  • 96
  • 1
    Click on "Network" and look at what is returned from backend. – Paul Spiegel Apr 29 '17 at 20:51
  • Small point with php returning json: this is the proper way to do it `header('Content-type: application/json');` `echo json_encode($value);` – Robert Apr 29 '17 at 22:02
  • Another point you probably shouldn't do is this: `var x=$('#dat').val();` and then further down the line in your jQuery ajax you do the following: `data: { x : x }` this is confusing – Robert Apr 29 '17 at 22:05

1 Answers1

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"
SergeyLebedev
  • 3,673
  • 15
  • 29
  • Maybe I'm wrong, but ` – Paul Spiegel Apr 29 '17 at 21:19
  • 1
    Topicstarter didn't set `type` attribute of his `button` element. "The default value for the `type` attribute of `button` elements is `submit`". - taken from here http://stackoverflow.com/questions/3314989/can-i-make-a-button-not-submit-a-form – SergeyLebedev Apr 29 '17 at 21:27
  • According to the linked post `type="button"` should also work. But i'm confused why there is an output in the console. – Paul Spiegel Apr 29 '17 at 21:33