By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,805 Members | 1,202 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,805 IT Pros & Developers. It's quick & easy.

Returning an array from javascript to php variable

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
["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 discussion thread is closed

Replies have been disabled for this discussion.