0

I need your help to try to sort out an issue with ajax callback. What happen is that the php script is called without issues, the query works fine and it generate the json output, but the callback doesn't works. No div is displayed on success and no changes on class happens.

This is my form

<form class="form-inline">
<input type="hidden" id="id_pro" value="1">
<input type="hidden" id="status" value="1">
<button id="submit" type="button" class="btn btn-link">
<span id="check" class="glyphicon glyphicon-check" title="my tytle"></span>
</button>
</form>
<span class="error" style="display:none"> Please Enter Valid Data</span>
<span class="success" style="display:none"> Form Submitted Success</span>

This is the js part

$(function() {
 $("#submit").click(function() {
   var id_pro = $("#id_pro").val();
   var status = $("#status").val();
   var dataString = 'id_pro='+ id_pro + '&status=' + status;

   if(id_pro=='' || status=='')
   {
      $('.success').fadeOut(200).hide();
      $('.error').fadeOut(200).show();
   }
   else
   {
      $.ajax({
      type: "POST",
      url: "myphppage.php",
      data: dataString,
      datatype: 'json',
      success: function(data)
      {
          if(data.result=='1')
          {
            $('.success').fadeIn(200).show();
            $('.error').fadeOut(200).hide();
            $("#check").attr('class', data.check);
          }
       }
      });
    }
return false;
});
});

And this is the php part

<? 

    if($_POST)
    {
        $id_pro=$_POST['id_pro'];
        $status=$_POST['status'];
        if($status==0){$status=1;}else{$status=0;}
        if ($mysqli->query("UPDATE mytable SET online=".$status." WHERE id=".$id_pro." ") === TRUE) {
            header("Content-type: application/json");
            $data = array('check'=>'new_class','check_text'=>'new text','result'=>'1');
            print json_encode($data);   
        }
        else
        {
            header("Content-type: application/json");
            $data = array('result'=>'0');
            print json_encode($data);   
        }
        $result->close();
    }else { }
?>

Any idea? Thank you in advance

5
  • 1
    console.log(data); Commented Dec 30, 2014 at 20:13
  • datatype typo here. Commented Dec 30, 2014 at 20:13
  • consol.log(data): code{"check":"new_class","check_text":"new text","result":"1"}code. What is strange is an error 500: POST myphppage.php 500 Internal Server Error Commented Dec 30, 2014 at 20:20
  • what do you mean with "datatype typo here"? Thanks! Commented Dec 30, 2014 at 20:21
  • datatype -> dataType, though, since your php is returning the appropriate contentType header, it's not really needed anyway. Commented Dec 30, 2014 at 21:00

1 Answer 1

1

error 500 means error in php and in your php don't see defined $mysqli and $result i think here is your problem.

better PHP looks like this but must define connect to DB

<?php
header("Content-type: application/json");
$data = array('result'=>'0');

if ($_SERVER['REQUEST_METHOD'] == 'post' )
{
    $id_pro = $_POST['id_pro'];
    $status = ($_POST['status'] == 0) ? 1 : 0; // if($status==0){$status=1;}else{$status=0;}

    // define $mysqli
    if ($mysqli->query("UPDATE mytable SET online=".$status." WHERE id=".$id_pro." ") === TRUE) {
        $data = array('check'=>'new_class','check_text'=>'new text','result'=>'1');
    }
    // $result->close(); // ????
}

print json_encode($data);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.