3 Replies - 1757 Views - Last Post: 14 April 2011 - 06:54 AM

#1 d.m14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 13-August 08

Passing Javascript array to PHP script

Posted 13 April 2011 - 01:56 PM

Hi,

Iam have sending a javascript array through an ajax request parameter
pars.push(element.id);

//this is the parameter inside the ajax request 

paramater  = 'p_=' + pars;



The 1st vakue is fine and does what I want in my database but the 2nd 3rd etc are causung me trouble
because of commas in the array (1,2,3,4)

How do i strip these commas before posting to my php script?

thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Passing Javascript array to PHP script

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1147
  • View blog
  • Posts: 7,134
  • Joined: 07-September 06

Re: Passing Javascript array to PHP script

Posted 13 April 2011 - 05:27 PM

You need some delimeter when printing an array otherwise you will be unable to recreate the array (after all 1,23,4 is different than 1,2,3,4 and so on).

That said, I would suggest you keep the commas and simply explode the array in PHP to get an array again:

<?php
$list = $_GET['p_'];
$arr = explode(",", $list);
?>


At that point you have an array of the numbers you passed in, now do your queries on each entry of the array

Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#3 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: Passing Javascript array to PHP script

Posted 13 April 2011 - 05:38 PM

For extensibility, I would advise looking into JSON. It's a bit much to explain here, but in a nutshell:
JSON is a data format available in Javascript that can be passed as a query parameter (eg - via AJAX), then converted server-side to a format that the server-side scripting language can interpret. It is has low overhead and is a widely supported data format.

http://www.json.org/js.html

This post has been edited by e_i_pi: 13 April 2011 - 05:39 PM

Was This Post Helpful? 1
  • +
  • -

#4 d.m14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 13-August 08

Re: Passing Javascript array to PHP script

Posted 14 April 2011 - 06:54 AM

View Poste_i_pi, on 13 April 2011 - 05:38 PM, said:

For extensibility, I would advise looking into JSON. It's a bit much to explain here, but in a nutshell:
JSON is a data format available in Javascript that can be passed as a query parameter (eg - via AJAX), then converted server-side to a format that the server-side scripting language can interpret. It is has low overhead and is a widely supported data format.

http://www.json.org/js.html


Thanks for that although I solved my problem server side by using the explode function. Always forget the simple functions when thinking you have a hard problem!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1