473,405 Members | 2,310 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,405 software developers and data experts.

Returning an array from javascript to php variable

Hello Everyone,
I'm trying to read a dynamic table into an array and then return it
back to the php array so that I can register that for the session. I'm
not too sure if i'm going in the right direction though. Javascript
follows.

function get_ticket_cart(){
var tamt = document.getElementById('tamt');
var tcells = tamt.cells;
var total = tcells[tcells.length-1];
return tcells;
}

And I'm trying to do this on onClick of the form. Php code follows

$tickets = array();
<input type="submit" value="Pay Dues" onclick="<? $tickets ?> =
get_ticket_cart();" >

$_SESSION['tickets'] = $tickets;
session_register('tickets');

I could be wrong in trying to catch the return value. But the problem
is this needs to be done on the click event.

Any suggestions are appreciated.

Thanks
saki

Jan 4 '06 #1
3 2093
Srinivas Aki wrote:
I'm trying to read a dynamic table into an array and then return it
back to the php array so that I can register that for the session. I'm
not too sure if i'm going in the right direction though. Javascript
follows.

function get_ticket_cart(){
var tamt = document.getElementById('tamt');
var tcells = tamt.cells;
var total = tcells[tcells.length-1];
return tcells;
}

And I'm trying to do this on onClick of the form. Php code follows

$tickets = array();
<input type="submit" value="Pay Dues" onclick="<? $tickets ?> =
get_ticket_cart();" >

$_SESSION['tickets'] = $tickets;
session_register('tickets');

I could be wrong in trying to catch the return value. But the problem
is this needs to be done on the click event.


I doubt you have understood how PHP works. At first you need to understand
that _server-side_ PHP code is parsed _before_ it gets to the client.

<URL:http://php.net/manual/>

Second, provided that short_open_tag=on in php.ini -- which it should not
be, see <URL:http://php.net/ini.core> --, the above code would generate
nothing but

<input type="submit" value="Pay Dues" onclick=" = get_ticket_cart();" >

`$tickets' would refer to the PHP array, and using it in this way results in
the empty string. (RTSL -- Read The Source, Luke; the source the _client_
gets.) Ignoring that the generated event listener code is of course
syntactically incorrect JS/ECMAScript code.

If you want to submit data to the server and so fill the PHP array, you will
have to do a HTTP request addressing a PHP script that will fill it; that
requires at least a URL. An (X)HTML form could be used; it would result in
displaying the server's response unless the server responds with HTTP
status code 204 (No Content). But then you should use the `onsubmit'
intrinsic event handler of the `form' element instead. Or, as an
alternative to forms, you could submit the data another way, including
"AJAX".
HTH

PointedEars
Jan 5 '06 #2
Srinivas Aki wrote:
Hello Everyone,
I'm trying to read a dynamic table into an array and then return it
back to the php array so that I can register that for the session. I'm
not too sure if i'm going in the right direction though. Javascript
follows.

function get_ticket_cart(){
var tamt = document.getElementById('tamt');
var tcells = tamt.cells;
var total = tcells[tcells.length-1];
return tcells;
}

And I'm trying to do this on onClick of the form. Php code follows

$tickets = array();
<input type="submit" value="Pay Dues" onclick="<? $tickets ?> =
get_ticket_cart();" >

$_SESSION['tickets'] = $tickets;
session_register('tickets');

I could be wrong in trying to catch the return value. But the problem
is this needs to be done on the click event.

Any suggestions are appreciated.

Thanks
saki


Well, since the table is dynamically generated, there obviously is a
time when you're in possession of the data that populates it. Why not
just add this data to a hidden field at time the same time as you're
generating a new row? Personally, I like to just keep the data in an
array, then redraw the whole table every time it changes. Makes it
easier when you need to remove an item.

Example:

var tickets = Array();

function add_ticket(id) {
tickets.push(id);
refresh_table();
save_to_hidden();
}

function refresh_table() {
var html = '';
for(var i = 0; i < tickets.length; i++) {
html += '<html here>';
}
table.innerHTML = html;
}

function save_to_hidden() {
hidden.value = tickets.join(',', tickets);
}

Jan 5 '06 #3
["Followup-To:" header set to comp.lang.javascript.]
On 2006-01-04, Srinivas Aki <sr**********@gmail.com> wrote:
Hello Everyone,
I'm trying to read a dynamic table into an array and then return it
back to the php array so that I can register that for the session. I'm
not too sure if i'm going in the right direction though. Javascript
follows.

function get_ticket_cart(){
var tamt = document.getElementById('tamt');
var tcells = tamt.cells;
var total = tcells[tcells.length-1];
return tcells;
}

And I'm trying to do this on onClick of the form. Php code follows


$tickets = array();
<input type="submit" value="Pay Dues"
onclick="<? $tickets ?> = get_ticket_cart();" >

$_SESSION['tickets'] = $tickets;
session_register('tickets');

I could be wrong in trying to catch the return value. But the problem
is this needs to be done on the click event.

Any suggestions are appreciated.


client side javascript does not run synchronously with server side PHP.
your PHP has syntax errors.

how are you communicating with the browser? regular form submission or
something else?

Bye.
Jasen
Jan 5 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Krackers | last post by:
How do you write a function which returns a reference to an array. I can only get a function to return a copy of the array itself. I've had a look at some other threads in this group an the return...
1
by: john | last post by:
Relatively new to C coding, so any help would greatly be appreciated. I'm having problems try to return my string array from my parsing function. When I do a printf I am getting the correct value...
13
by: Jordan Tiona | last post by:
Is this not allowed in C++? I try in MSVC, but I always get an error, and the function no longer shows up in my class view.
3
by: Srinivas Aki | last post by:
Hello Everyone, I'm trying to read a dynamic table into an array and then return it back to the php array so that I can register that for the session. I'm not too sure if i'm going in the right...
45
by: VK | last post by:
(see the post by ASM in the original thread; can be seen at <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b> as an option) As that is not in relevance to...
4
by: noddy | last post by:
I have a mysql table called Users The userID field values start at 100 and increment up from there I am trying to transfer values from this table into a javascript array Here is what the code...
1
by: bsprogs | last post by:
I am currnetly programming a file hosting website in PHP and I am slowly integrating AJAX into the website. Here is my problem: The user uploads the file. The server processes the file and...
8
by: darren | last post by:
Hi everybody, have a quick look at this code: ===== ===== int main(void) { string msg; makeString(msg); cout << "back in main, result = " << msg << endl;
5
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.