Skip to main content
Known Participant
December 10, 2013
Question

Sending javascript array to php

  • December 10, 2013
  • 1 reply
  • 824 views

I am trying to send a javascript array to a php for validation.  Here is my code:

var elements = new Array();

        var values = new Array();

        var count = 0;

        var i = 0;

        $("input").each(function(){

            i++;

            var ID = $(this).attr("id");

            elements = ID;

            count = count+1;

        });

        count = 0;

        i = 0;

        $("input").each(function(){

            // Assign each slot to i's form element

            i++;

            var value = $(this).attr("value");

            values = value;

            count = count+1;

        });

$.ajax({ url: 'process.php',

                data: { submit : "1", elements : elements, values : values },

on process.php:

$values=json_decode($_POST['values']);

$elements = json_decode( $_POST["elements"] );

But when these values are echoed out, they come out to be empty.

This topic has been closed for replies.

1 reply

Known Participant
December 10, 2013

I've also tried another way, which also doesn't work:

$elements = $_POST['elements'];

$.ajax({ url: 'process.php',

                    data: "submit=1&elements=" + JSON.Stringify($elements) ,

BTW, elements is as defined: <input type="text" name="elements[]"

on process.php:

$elements = $_POST['elements'];

This also does not put out anything.  I don't think this works since the form is never submitted. 

$('#submit').click(function(){

intercepts it, and so there is not $_POST.