Hi,
I am having difficulty to have an instant validation carried out immediately after input of a value in a view file.
Here is the code (extract of the view file) that calls for the function (found in the controller file)
View file:
<?php Yii::app()->getClientScript()->registerScript('checkid2','$("#thisid").change(function()
{
alert("actiondupecheck");
$.post("index.php?r=roomtype/dupecheck", {thisid: $("#thisid").val()}, function(data) {
$("#dupecheck").html(data);
});
}
);');
?>
Controller file:
public function actionDupecheck()
{
$thisid = trim($_POST['thisid']);
$test =Piece::model()->find("id='$thisid'");
if(!empty($test)) {
echo sprintf("This id <strong>%s</strong>, exists already.", $thisid);
}
}
I have tried several ways but am completely stuck with this. I thought there was a problem with the syntax with the URL but after replacing 'dupecheck' with 'admin' in the view file, the admin function was called. What I am trying to do with this code, is to check for a duplicate entry in my table. I have been advised to use model validation rules. I will look into this suggestion but I also want to know that is wrong with my code. Thanks