문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top