skip to Main Content

I want to send two values to my PHP script that is being called from an AJAX request. Somehow my data is not being passed to the PHP script.

Maybe I am doing something wrong somewhere. Can I have some insight?

    $(function() {

        $(".delbutton").click(function() {
                var viewed_comments = $("#viewed_comments").val();
                var del_id = $(this).attr("id");

                var info = 'id=' + del_id;
                var comments = 'comm=' + viewed_comments;
                var tr = $(this).closest('tr');  

            if (confirm("Are you sure to mark this as viewed?")) {

                $.ajax({
                    type : "POST",
                    url : "update_entry.php", 
                    dataType: "json",
                    data: {info:info, comments:comments },

                    success : function(response) {

                           if(response=="updation success"){
                           console.log('inside');


                           }
                    }
                });  
            }
            return false;
        });
    });

And my PHP where the AJAX request is going,

    $id     =   $_POST['id'];
  $viewed_comments = $_POST['comm'];
  $level_code  = $_SESSION['level_code'];
  $action   =   'view';
  $viewed_date = date("Y-m-d");    
  $viewed_by        =    $_SESSION['session_admin_id'] ;

  if($action    == 'view')
  { 
   $viewed_date = date('Y-m-d h:i:s');  

   $nots = $db->idToField("tbl_table","notes",$id);
   if ($nots == "")
   {
   $date_string = "last viewed on|".$viewed_date."|" ;
   }
   else {
   $date_string = $nots."last viewed on|".$viewed_date."|" ;
   }
      $fnc->update_is_viewed_for("tbl_table",$id, "$viewed_date", $viewed_by);        
    $notes_data = array("notes"=>$date_string,"viewed_comments"=>$viewed_comments);
    $db->query_update("tbl_table", $notes_data, "id=$id");
     }
    if($db->query_update("tbl_table", $notes_data, "id=$id")){
      http_response_code();        
      echo json_encode('updation success');
    }else{
       http_response_code(204);       
    } 

2

Answers


  1. Isn’t it a name thing? You send two POST variables:

    data: {
              info: info,
              comments: comments
            },
    

    but you retrieve them with different names:

        $id     =   $_POST['id'];
      $viewed_comments = $_POST['comm'];
    

    What do you get if you var_dump($_POST);?

    Login or Signup to reply.
  2. Use seriliaze form values it will solve your data missing problem, change #frm to your form id

     $(document).ready(function(){
        $('#frm').submit(function(event){
        event.preventDefault();
        var formValues = $(this).serialize();
            $.ajax({
            url:"update_entry.php",
            method:"POST",
            data:formValues,
            dataType:"JSON",
                success:function(data){
                   if (data == 'updation success') {
                    console.log('success');
                   }
                }
            });
        });
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search