-1

I have created a script that adds items on click into an array.

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

Items are coming from mysql database so I am storing primary keys of items. keys are stored in array like this manner

1,2,3,4,5

Now I want to access this array in php so that I could store in database one by one.

I thought to doing some ajax and this is my code

$('.show').click(function(e)
        {
            //e.preventDefault();

            $.ajax(
            {
               method: 'POST',
               url: 'createsale.php',
               data: {items: array_ids},
               success: function()
               {
                    alert('done'); 
               } 
            });
        });

I get the done alert but couldn't manage to get it stored in database. Can anyone tell how do I insert those items in mysql?

8
  • where is createsale.php ? Commented Oct 19, 2016 at 2:15
  • I couldn't think of how to get the sent array! I though $result = $array_id; would work but nope. Commented Oct 19, 2016 at 2:19
  • Data sent to php through GET/POST are stored in the super globals $_GET and $_POST Commented Oct 19, 2016 at 2:20
  • Do you already have the query / rows where the IDs are stored? Commented Oct 19, 2016 at 2:36
  • Yess! And When ever I click add an id gets stored in array! I do not know to access the array now Commented Oct 19, 2016 at 2:38

2 Answers 2

1

Send the value from Javascript by using Json {"key":value} or array [1,2,3]. While getting them to use them in PHP, you can use json_decode() to convert the Json or array from Javascript to PHP.

If you want your information from PHP to Javascript, Just use the funtion json_encode() that will send a json string.

Ref: json_encode, json_decode

Sign up to request clarification or add additional context in comments.

2 Comments

This answer is correct, json_encode, json_decode are the methods for deal with array/Jason data to and from php into JavaScript/HTML
0

your createsale.php file should has something like this below code to receive array Data & dont forget to escape your data using mysql_real_escape_string before doing any mysql query

    <?php
          if(isset($_POST["items"]) && $_POST["items"] !=""){
            $arr = $_POST["items"];
            //convert JS array to PHP array
            $arr = json_decode($arr,true);
            //then you have your PHP array => new array(1,2,3,...);

            /*  use for/forEach loop
         for($i=0;$i<count($arr);$i++){
            do the task
         }
            */
        }
   ?>

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.