How To Pass Parameters From Javascript To Php Class

Hello –

I am new to PHP and Yii (coming from Java) and am trying to pass a value from JavaScript to a method in a PHP class on a mouse click event. If someone could provide an example using the default Yii "My Web Application" I would be very grateful.

Thank you.

t0mes

Check the CHtml::ajaxButton() documentation for starters. If you’re more familiar with Javascript and jQuery’s $.ajax methods, you can try making a normal button (using CHtml::button()), setting its ID or class using htmlOptions, and then using Yii::app()->clientScript->registerScript() to attach your onlick responder to the element.

If this sounds like mumbo-jumbo, don’t worry. Just check the documentation and play around with making method calls, and test the JS in the console.

Thanks for the reply. I can’t use a button, because I am grabbing the onClick event when a user clicks on a map. I need to grab that event and pass it to a PHP class. Ideally I would pass some data from the map and my JS function works fine and it has all sorts of data about the map, I just need to pass this to a handler on the server side so I can add a row to the database with the coordinates.

Again, thanks you for any help!

Since JS runs on the client side, you’ll need to initiate a http request to hit the server. You can bind the mouseClick event to an ajax call. Are you using jQuery?

I’ll use whatever it takes! I have no idea how to do this using Yii or PHP. I’ve done this in Java by registering a client script, but I can’t seem to figure out how to do that or how to reference a class in JavaScript.

I Used This Query and worked for me , check it

$ajax=CHtml::ajax(array(‘url’=>URL,‘data’=>array(‘item1’=>ITEM1,…)));

<a onclick=’$ajax’>click</a>