Hi,
Can anyone please help me to find why the $.post seems not to be working. I have view form for creating a product. I am trying to have an instant validation when the user inputs the id by checking whether this id does not already exist in the table concerned.
Here is my code for the view form:
[font="Microsoft Sans Serif"][color="#FF0000"]
<div class="form">
<?php Yii::app()->getClientScript()->registerScript(‘checkid’,’$("#thisid").change(function()
{
$.post("dupecheck.php", {thisid: $(this).val()}, function(data) {
$("#dupecheck").html(data); });
}
);');
?>
<?php $form=$this->beginWidget(‘CActiveForm’, array(
'id'=>'roomtype-form',
'enableAjaxValidation'=>true,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'id'); ?>
<?php echo $form->textField($model,'id',array('size'=>3,'id'=>'thisid','maxlength'=>3)); ?>
<?php echo $form->error($model,'id'); ?>
</div>
<div id="dupecheck"></div>
<?php echo $form->labelEx($model,'description'); ?>
<?php echo $form->textField($model,'description',array('size'=>60,'maxlength'=>100)); ?>
<?php echo $form->error($model,'description'); ?><br>
<?php echo $form->labelEx($model,'maxnormal'); ?>
<?php echo $form->textField($model,'maxnormal',array('size'=>10,'id'=>'maxnor')); ?>
<?php echo $form->error($model,'maxnormal'); ?>
<?php echo $form->labelEx($model,‘maxadd’); ?>
<?php echo $form->textField($model,'maxadd',array('size'=>10)); ?>
<?php echo $form->error($model,'maxadd'); ?>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
[/color][/font]
Here is dupecheck.php
[font="Microsoft Sans Serif"][color="#FF0000"]
<?php
$thisid = trim($_POST[‘thisid’]);
$test =Piece::model()->find(“id=’$thisid’”);
if(!empty($test)) {
echo sprintf("This id <strong>%s</strong>, exists already.", $thisid);
} //else {
//echo "Good choice!";
//}
?>[/color][/font]
The dupecheck.php is not even called. It seems that $.post does not work. Any help much appreciated. Thanks.