Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Sending javascript array to php

Explorer ,
Dec 10, 2013 Dec 10, 2013

Copy link to clipboard

Copied

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.

TOPICS
Server side applications

Views

768
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 10, 2013 Dec 10, 2013

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines