473,503 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1467
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
14000
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...
7
3827
by: Dr John Stockton | last post by:
What are the best ways of returning multiple results from a subroutine ? I've been using ... return } which is inelegant. I'm used to Pascal's procedure X(const A, B : integer; var C, D :...
1
3308
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
4764
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
2097
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
4761
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
2318
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
2857
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
2188
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
2665
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
7202
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
7280
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
7330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
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
7460
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
5578
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,...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.