473,386 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to pass a javascript array to php file

82
Hi,
Expand|Select|Wrap|Line Numbers
  1. <?
  2. #cd.php
  3. echo "<input type='text' name='text1' id='text1' onchange=\"return_value()"\ /><br></br>";
  4. ?>
  5.  
Javascript file
Expand|Select|Wrap|Line Numbers
  1. var xmlHttp;
  2.  function return_value() {
  3.  var x=document.getElementById('text1').value;
  4.  xmlHttp = GetXmlHttpObject();
  5.  if(xmlHttp == null) {
  6.  alert("Your browser does not support ajax");
  7.  return;
  8.  }
  9.  var url="ab.php";
  10.  url=url+"?x="+x;
  11.  xmlHttp.open("GET",url,true);
  12.   xmlHttp.send(null);
  13.  xmlHttp.onreadystatechange = StateChanged;
  14.  function StateChanged() {
  15.  if(xmlHttp.readyState == 4) {
  16.  var y=new Array();
  17. y= xmlHttp.responseText;
  18.  }
  19.  }
the file ab.php returns an array to the javascript file, how can i pass this array 'y' to the php cd.php file
Jan 20 '09 #1
12 8970
Dormilich
8,658 Expert Mod 8TB
I guess you have to use a serializer for that. JSON and WDDX are the ones I've heard of (both work in Javascript and PHP)

in the javascript code line 16 is unnecessary, your array (y) will be replaced by the return string (responseText).
Jan 20 '09 #2
sarega
82
ok...can you just give an example of how to serialize it??
Jan 20 '09 #3
Dormilich
8,658 Expert Mod 8TB
so far I've only done WDDX. JSON might be more suitable for you. explanations and example you can find here:
Jan 20 '09 #4
sarega
82
ok will try that now...thank you
Jan 20 '09 #5
sarega
82
for serializing at the javascript and deserializing at the php side, can both the javascript n php be in different files??
Jan 20 '09 #6
Dormilich
8,658 Expert Mod 8TB
@sarega
yes...???

PHP is run on the server, JS in the browser, that's two different locations. so why would you have them in the same file? (besides the fact that serialization originates from transfering complex data between system)
Jan 20 '09 #7
acoder
16,027 Expert Mod 8TB
Instead of trying to pass an array, pass something that the server-side script will recognise, e.g. "val=1&val=2&val=3" (rather like how checked checkbox values are passed).
Jan 21 '09 #8
sarega
82
I cannot reload the page, if I did then the values that are entered in the text boxes will be lost...
Jan 22 '09 #9
Dormilich
8,658 Expert Mod 8TB
@sarega
that's just a matter of correctly setting the text boxes' default values.
Jan 22 '09 #10
sarega
82
But the value in the text box will not be the same every time the form is submitted for the user
Jan 22 '09 #11
Dormilich
8,658 Expert Mod 8TB
I don't see the problem there.
Jan 22 '09 #12
acoder
16,027 Expert Mod 8TB
Yes, no problem there. Either use Ajax, or use the new submitted values to set the text box values.
Jan 22 '09 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Nath | last post by:
Please help!? I am new to writing html, javascript, pretty new to MySQL but quite proficient at writing Perl and i'm a quick learner. I am building a database driven website and i am a little...
5
by: Seeker | last post by:
Newbie question here... I have a form with some radio buttons. To verify that at least one of the buttons was chosen I use the following code ("f" is my form object) : var btnChosen; for...
4
by: Sunny | last post by:
Hi, I am at present working on a jsp page which has a single text field. Upon entering a value in the field, i am trigerring an onChangeEvent() which calls a method of javascript. I am also...
2
by: Augusto Cesar | last post by:
Hello people. How can I Pass ASP Array variable to Javascript; I´m to trying this: <script language="JavaScript"> var x = new Array(10);
10
by: Sean Dockery | last post by:
I have the following HTML file that I've been using for testing... <html> <head> <script type="text/javascript"> <!-- function handleWindowLoad() { var items = ; for (var i = 0; i < 11; i++)...
2
by: André | last post by:
Hi, I have to pass a lot of variables from vb.net to javascript using client callback. the problem is that there are single variables, but also arrays with differents indexes. I have two...
1
by: thomas_okken | last post by:
I'm working on a little C# DLL that will let me capture audio from within Internet Explorer. The idea is that the C# object, once it is recording, will periodically invoke a JavaScript callback,...
4
by: IRC | last post by:
hey, i am pretty new on javascript as well as PHP, Hey, anyone can you help me, how to pass the javascript array value to php page......... i want to retrieve the values which are arrayed on...
1
by: helraizer1 | last post by:
Hi all, I have a photography page on my website, I have pictures on the page (obviously) but the file names are stored and loaded in Javascript, in an array. var pic = new...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.