javascript / PHP interaction

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

javascript / PHP interaction

Post by Unipus »

Maybe it's just late in the day and I'm not thinking straight anymore, but I'm having some real trouble figuring out how to do this:

I have an input text field. I want to make that input field so that onChange it updates a PHP array. But I'm having some trouble getting javascript to be able to pass information to PHP, since it renders in the opposite order. I'm trying to make fish jump upstream here. But I know that they can, somehow. God bless those salmon.

Alternately, how could I capture the value of ALL input text fields in a variable-length form onSubmit and write those values over those currently in an array? I had a system that I thought would work, but, well... it doesn't.
timj
Forum Newbie
Posts: 14
Joined: Tue Aug 26, 2003 8:00 am
Location: Belgium

Post by timj »

To pass the JavaScript value to PhP without bothering the user too much, I suggest the most common way:
store your JavaScript value in a hidden input tag (<input type="hidden">) and then submit the form that contains this hidden input tag to your server. The php script that executes when the form with the hidden tag is submitted, should update the PHP array as you requested.

As for the second part of your question, I'm not sure what you mean with "variable length" form. Do you mean that your form has a number of text fields that's not always the same (eg the user can add text fields)? If that's what you mean, use the $_POST array and treat is as an array (for recent versions of PHP only) or check out the manual if you have an old version that doesn't know $_POST. It would help a lot if the names of your text fields are somewhat similar.

Another way is to use the method from above. Use JavaScript to concatenate all the text values in a certain (but fixed) order and put that in the hidden input tag. When you submit this value (1 long string), you can make php take the string apart again. This isn't very convenient, but it should work.
Post Reply