0

js

var sim_list=[];
var simvalue = $('input[name="simnamecbx"]:checked').each(function(){
    var sim_name=this.value.split(" ")[0];
    console.log("simname:",sim_name);
    sim_list.push({
        sim_name
    });
);
console.log(sim_list);
var simulation_list = JSON.stringify(sim_list);     
$.ajax({ url: 'addprogram.php',
     data: {addpgmname:addprogname, addpgmcomp:addprogcomp, addpgmdate:addprogdate, simselvalue:simulation_list},
     type: 'post',
     dataType: 'json',
     success: function(output){
         var a = output;
         console.log(a);
     }
});

php

$simvalue = json_decode($_POST["simselvalue"],true);
foreach($simvalue as $key => $value) {
    print_r($value);
}

Question

When I execute the above php I get the values as

Array
(
    [0] => Array
        (
            [sim_name] => 12
        )

    [1] => Array
        (
            [sim_name] => 13
        )

    [2] => Array
        (
            [sim_name] => 14
        )

)

But I want the result to be like 12, 13, 14 which can be inserted into mysql table. Please explain how to parse this array that I get from javascript.

3
  • $simvalue is an array so simply use foreach loop and insert it Commented May 14, 2015 at 9:13
  • Is there any error on console??? Commented May 14, 2015 at 9:34
  • above output you must be getting on print_r($_POST["simselvalue"]) right ? Commented May 14, 2015 at 10:05

2 Answers 2

1

I think that the problem is that you are pushing an object with sim_name on to your array instead of the actual value. This actually only works in ES6 compatible browsers which makes your error invisible.

The problem is this part in your javascript code:

sim_list.push({
    sim_name
});

In ECMAScript 6 this is the equivalent of the following:

sim_list.push({
    sim_name: sim_name
});

Thus you are pushing an object with the sim_name property set to the number (e.g. 12, 13, 14).

What I think that you want to do is the following:

sim_list.push(sim_name)

This will push only the number onto the array.

All together now:

JS

var sim_list = []

$('input[name="simnamecbx"]:checked').each(function () {
  var sim_name = this.value.split(' ')[0]
  sim_list.push(sim_name)
})

var data = {
  addpgmname: addprogname,
  addpgmcomp: addprogcomp,
  addpgmdate: addprogdate,
  simselvalue: JSON.stringify(sim_list)
}

$.ajax({
  url: 'addprogram.php',
  data: data,
  type: 'post',
  dataType: 'json',
  success: function (output) {
    console.log(output)
  }
})

PHP

$simvalue = json_decode($_POST["simselvalue"]);

print("The selected values was: " . implode($simvalue, ", "));
Sign up to request clarification or add additional context in comments.

Comments

0

Easiest way:

$data = json_decode($_POST["simselvalue"], true) // true = associative array

foreach($data as $key => $value) {
    // database query
}

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.