0

I'm a super beginner in php, and I'm using a MVC php framework called Yii. I can't seem to find any articles that explain how to get values of html elements with PHP. Everywhere I look it's all about how to get values from form fields after a POST in some other view. Is there anyway to get field values and send them to a controller in PHP and just come back to the original view.

In .Net MVC I just use jquery to get form fields and do an ajax call. It's not sensitive data so I'm not worried about security. I like ajax because I don't do any page post back, I just send my data over and remain on the same page I was on.

Is there any way to do MVC AJAX kind of thing with PHP? Read html element values and send them to a controller for data manipulation?

tereško
  • 58,060
  • 25
  • 98
  • 150
EKet
  • 7,272
  • 15
  • 52
  • 72
  • you say you know how to do that in .net mvc, so its not much different in yii, tell me where exactly are you stuck – bool.dev Feb 28 '12 at 06:23

1 Answers1

1

It works the same way. Yii comes bundled with jquery, so you

just use jquery to get form fields and do an ajax call

to some controller function, do whatever you want with it, and return a response, with php's echo.

If you already know some jquery, then the client-side shouldn't be much different from .net mvc.

Edit:
To add a <script> to the generated html see registerScript.

To create urls use the createUrl function.

To add ajax options to html tags code looks similar to:

echo CHtml::checkBox('mybox',false,
  array(// array for htmloptions, we also pass ajax options in here
    'class'=>'checkBoxes_class',
    'ajax'=>array(// this is ajax options for jquery's ajax
      'type'=>'POST',
      'url'=>Yii::app->createUrl('xyz',array('clickedboxid'=>'mybox')), // here you passed clickedboxid as a get variable
      'beforeSend'=>'function(){}',
      'success'=>'',
      // etc etc
    )
  )
);

Every html tag generator helper function takes htmlOptions array, where we can also pass ajax options.

While reading these values in the controller:

public function actionSomeAction($id){
   // $id is mybox
   echo "Hello"; // this is returned as response to the client
}

Hope this is enough for you to get started.

bool.dev
  • 17,508
  • 5
  • 69
  • 93
  • So just like before I send my data to a controller via URL with Ajax and it's all the same?! Sounds awesome, how come no one talks about that...I saw many articles after googling that suggested `$_POST['field_name']`, I guess I'm not good at googling. – EKet Feb 28 '12 at 17:38
  • umm..whenever you send data to server, you send through GET or POST variables, right? so $_POST has the post variables, and $_GET has get variables, incase you didn't know. just use the jquery ajax method. in yii there is a CHTML class, which has a function built on top of jquery's ajax, so you can use that. generally when specifying html tags in yii, we can also pass ajax options, which are taken by this ajax method of the chtml class – bool.dev Feb 28 '12 at 17:51
  • ok cool. I know GET/POST, my point was there was a lot of focus using $_POST['field'] method vs the Ajax method. I started thinking maybe ajax and php just don't mix well or something since there was no focus on it. thanks for the CHTML tip, didn't know about that. I'll give that a shot tonight. – EKet Feb 28 '12 at 18:02
  • yes you send through url, but yii again comes in with a helper function to generate urls, createUrl. – bool.dev Feb 28 '12 at 18:02
  • they mix very well don't worry!! – bool.dev Feb 28 '12 at 18:08
  • imho i have given enough tips/hints for you to continue with ajax and yii, see my updated answer. happy coding. – bool.dev Feb 28 '12 at 18:34
  • 1
    you da man. Thanks for all your effort. I'll give this a go tonight, is it weird that I'm excited to try it? I heart MVC. – EKet Feb 28 '12 at 22:54