Custom php function and how to call it dynamically

Hi I’m just new using YII,

can someone help me with regards to custom function. My objective is to call my function checkAvailability(param… , …) when the user clicks the save button, the function checks the events requirements schedule in the database based on the date and timestart and timeend that user inputs.

Since I really don’t know how in yiiframework I’ve used jquery and php hereunder;

in my: baseurl/js/script.js

/* dov: date of visit, tov: time of visit (start), tov_end: time of visit (end)*/

$(document).ready(function() {

	$("#save").click(function() {


	if ($("#dov").val() == "" || $("#tov").val() == "" || $("#tov_end").val() == "")


		{


			alert("Please complete the form");


		}


		


		else


		{


			$.post('../inc/validate.php', { dov : $("#dov").val(), tov : $("#tov").val(),


                                tov_end : $("#dov_end").val()}, function(data) {


				document.getElementById("myDiv").innerHTML = data;


                            });


		}


 });

in my: baseurl/inc/validate.php

<?php

$dov = $_REQUEST[‘dov’];

$tov = $_REQUEST[‘tov’];

$tov_end = $_REQUEST[‘tov_end’];

function checkAvailability($dov,$tov,$tov_end){

sql = mysql_query(&quot;sql to check from data if it has conflicts&quot;);


... ;


return &#036;availability;

}

if(isset($_REQUEST[‘dov’]){

?>

<script> alert(<?php echo $dov; ?>); </script>

<?php } ?>

My problem is it that the $.post seems not posting to validate.php, does anyone knows the answer? can you recommend were to put my checkAvailability() php functions and how to call it.

Thanks and GOD Bless.

Normal practice is to turn your function being available as/from controller/action method and make ajax call to this controller/action then.

http://www.yiiframew…1/en/basics.mvc

If you’re new to yii it’s highly recommendable to start with yii-guide and yii blog example: http://www.yiiframework.com/tutorials/

It’s fairly easy to use your own JS and PHP libraries in Yii, but it looks like you are trying to reinvent the wheel by writing your own libraries, then stuffing it into Yii. You should really check out the MVC approach mentioned by yugene.

Once you understand MVC, what you want to do is create an ajax submit form and do something like this:

view.php


<?php $form=$this->beginWidget('CActiveForm', array(

        'id'=>'your-form-id',

        'enableAjaxValidation'=>true,

        'clientOptions'=> array('validateOnSubmit'=>true),

)); ?>

Then inside your controller file (this is where view.php will submit to) you will control what actions to take to determine which function to call. In your case, the form above will submit to the controller/actionSubmit and the controller will have actionSubmit() function that will call a validate function on your model.

controller.php


public function actionSubmit() {

  if (Yii::app()->request->isAjaxRequest) {

    $model = new Model();

    $model->attributes = $_POST['Form'];

    if( $model->validate() ) {

      // success

    } else {

      // error

    }

  }

}

Your model will have the logic that validates the form (in view.php)

model.php


public function validate() {

  // some other validation code if necessary

  $valid_flag = $this->checkAvailability();

  // maybe some other checks?

  return $valid_flag;

}


private function checkAvailability(){

 // $dov,$tov,$tov_end would be attributes of your model

  sql = mysql_query("sql to check from data if it has conflicts");

  ... ;

  return $availability;

}

Thanks yugene B)