3

Okay prior to my previous question I have tried a bit of modification to my code but somehow I am still lagging.

This is jquery I have created

$(document).ready(function()
    {
        var array_ids = [];
        $('.add').click(function()
        {
            array_ids.push($(this).parent().siblings('.row_id').html().trim());
            alert(array_ids);    
        });

        $('.show').click(function(e)
        {
            //e.preventDefault();
            var jsonString = JSON.stringify(array_ids);
            $.ajax(
            {
               method: 'POST',
               url: 'addsale.php',
               data: {data : jsonString},
               cache: false,
               dataType: "json",
               success: function()
               {
                 console.log(data.reply);
                alert(data.reply);
               } 
            });
        });
    });

And addsale.php

if(isset($_POST['push'])) //tried it commenting also!
{
$data = array();
$data = json_decode(stripslashes($_POST['data']));
foreach($data as $d){
 echo $d;
 }
 }

Can anyone tell me what is missing to access the array and get the html from addsale.php to current page?

3
  • 1
    Use $_POST['data'] Commented Oct 19, 2016 at 3:07
  • $data = $_POST['data']; this thing ?? Commented Oct 19, 2016 at 3:08
  • Your $.ajax() success callback uses a variable data that isn't defined (you probably want it declared as success: function(data) { ...). Commented Oct 19, 2016 at 3:15

4 Answers 4

1
$(document).ready(function()
{
    var array_ids = [];
    $('.add').click(function()
    {
        array_ids.push($(this).parent().siblings('.row_id').html().trim());
        alert(array_ids);    
    });

    $('.show').click(function(e)
    {
        //e.preventDefault();
        //prefer parse function
        var jsonString = JSON.stringify(array_ids);
        $.ajax(
        {
           method: 'POST',
           url: 'addsale.php',
           data: {"data" : jsonString},
           cache: false,
           dataType: "json",
           //e is the response text from your PHP code
           success: function(e)
           {
             //I don't know why this code
             //console.log(data.reply);
            //alert(data.reply);
           } 
        });
    });
});

in your PHP code try

if(isset($_POST['data'])) //tried it commenting also!
{
   $data = array();
   $data = json_decode(stripslashes($_POST['data']));
   foreach($data as $d){
       echo $d;
   }
 }
Sign up to request clarification or add additional context in comments.

3 Comments

JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 3 of the JSON data
Whenever I click add it saves like this 1 then again click 1,2 then again click 1,2,3
It's ok stringify() for array to Json. In case you want Json {"key":"value"} you can use parse(). It's preferable using json for your comunication from PHP to Javascript or vice et versa.
0

echo with foreach will not good handling for json response ! You should loop that json after response to ajax success function ...

addsale.php

if(isset($_POST['data'])) //tried it commenting also!
{
   $data = array();
   $data = json_decode(stripslashes($_POST['data']));
   echo json_encode($data);
 }

access json resposne in ajax

$.ajax(
        {
           method: 'POST',
           url: 'addsale.php',
           data: {"data" : jsonString},
           cache: false,
           dataType: "json",
           success: function(d)
           {
             //I don't know why this code
             console.log(d);
           } 
        });

Comments

0

i think you have everything correct but missing something right data: {data: ... }, you should also add something like below-

 data : {data : jsonString, 'push':'push'},

and in php code you try to decode the an array which is not in json format, your code just need to be like this-

  if(isset($_POST['push'])) //tried it commenting also!
  {
      foreach($_POST['data'] as $d){
         echo $d;
      }
  }

Comments

0

No data argument in your success function. Modify your addsale.php

$(document).ready(function()
{
    var array_ids = [];
    $('.add').click(function()
    {
        array_ids.push($(this).parent().siblings('.row_id').html().trim());
        alert(array_ids);    
    });
    $('.show').click(function(e)
    {
        //e.preventDefault();
        var jsonString = JSON.stringify(array_ids);
        $.ajax(
        {
           method: 'POST',
           url: 'addsale.php',
           data: {data : jsonString},
           cache: false,
           dataType: "json",
           success:function(data)
           {
             console.log(data.reply);
             alert(data.reply);
           } 
        });
    });
});

addsale.php

<?php
if(isset($_POST['data'])) //tried it commenting also!
{
$data=json_decode(stripslashes($_POST['data']));
echo json_encode(['reply'=>$data]);
exit();
}

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.