Web Hosting Talk







View Full Version : PHP Question


rrsnider
01-18-2002, 12:54 PM
Is there a way to do an "on click" type function in PHP. For instance, I set up a table of colors (4x4 with each cell a different color). When the user clicks on one of the colors, I call a function or whatever and pass that color to store as his preference. Seems basic enough.

Thanks in advance
Bob

zupanm
01-18-2002, 12:57 PM
i don't think so, you'd have you to java script for that.

Ahmad
01-18-2002, 01:42 PM
You can do it in conjunction with javascript, make a javascript function that, on the event of clicking on one of the colors, will send the user to a PHP script that stores the user preferences.

If you mean a more sophisticated, customizable portal system, then i have another idea to achieve this.

rrsnider
01-18-2002, 04:40 PM
If my page has javascript and PHP, how do I reference the javascript variable in PHP?

Ahmad
01-18-2002, 05:04 PM
Ops!

That is not possible, at least not in that sense.

PHP is run on the server and its output is sent to the browser, where HTML is rendered and javascript. Not only they run seperatly on, they run on different processors and machines! (typically).

To send information from javascript to a PHP script, you should redirect the user to the wanted PHP script and put the information you want in the address. I don't know how to redirect a user in javascript, i'll suppose there is a (hypothetical) function called redirect in javascript and that you have a variable called color in javascript, and you want to send the information to a php script called save.php, you should do something like this:

redirect( 'save.php?color=' + color );

now save.php should be written to recieve the request with $color variable containing the chosen color.

priyadi
01-18-2002, 05:04 PM
Originally posted by rrsnider
If my page has javascript and PHP, how do I reference the javascript variable in PHP?

PHP and Javascript is not directly connected to each other. PHP run on the server, Javascript run on the web browser. PHP can pass variables to Javascript by creating the javascript code itself. Javascript can pass variables to PHP by using HTML forms.

If you have trouble understanding how things work, I suggest to use PHP without Javascript first.

On your problem you can work around without using Javascript. Use four buttons laid in a table, then use CSS to color the buttons.

311
01-18-2002, 05:08 PM
Originally posted by rrsnider
Is there a way to do an "on click" type function in PHP. For instance, I set up a table of colors (4x4 with each cell a different color). When the user clicks on one of the colors, I call a function or whatever and pass that color to store as his preference. Seems basic enough.

Thanks in advance
Bob

I may be wrong but don't you use cookies to save a users preference or something like that?