Reputation: 718
I have a drop down list of some categories names, I want to take the name of the selected category by using ajax and send them to the same php page that the ajax script is inside it, here is my new code:
create action code:
public function actionCreate()
{
$model=new News;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['category']))
$category = $_POST['category'];
else
$category = NULL;
if(isset($_POST['News']))
{
$model->attributes=$_POST['News'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model, 'category'=>$category
));
}
form code:
<div class="row">
<?php echo CHtml::label('Choose category to add from',''); ?>
<?php $data = array('celebrities'=>'Celebrities', 'events'=>'Events', 'videos'=>'Videos', 'editorials'=>'Editorials'); ?>
<?php echo CHtml::dropDownList('category', '', $data); ?>
</div>
<script type="text/javascript">
$("#category").change(function(){
var category = $(this).val();
$.ajax({
url:'/tonyward/index.php/news/create',
data:{category:category},
type:'POST',
dataType:'html',
success:function(data) {
console.log(data.category)
},
cache:false
});
});
</script>
<div class="row">
<?php echo $form->labelEx($model,'idItem'); ?>
<?php $data = array('celebrities'=>$category); ?>
<?php echo CHtml::dropDownList('subcategory', '', $data); ?>
<?php echo $form->error($model,'idItem'); ?>
</div>
now how should the data of the second drop down list will changed ?
old code :
<div class="row">
<?php echo CHtml::label('Choose category to add from',''); ?>
<?php $data = array('celebrities'=>'Celebrities', 'events'=>'Events', 'videos'=>'Videos', 'editorials'=>'Editorials'); ?>
<?php echo CHtml::dropDownList('category', '', $data); ?>
</div>
<script type="text/javascript">
$("#category").change(function(){
$("#category").change(function(){
var category = $(this).val();
$.ajax({
url:window.location.href,
type:'GET',
data:{category:category},
dataType:'json',
cache:false,
success:function(data){
},
});
});
</script>
<?php
if(isset($_GET['category'])){
$category = $_GET['category'];
echo "<script>alert('done');</script>";
}
?>
Upvotes: 0
Views: 1359
Reputation: 718
how did I solved my problem?
selectedCategory()
.selectedCategory()
is renderPartial
the html code of the drop down list, that are written in _categoryItems.php.('#category')
.selectedCategory()
, and finally it will load html data (on success) in a specific div.here is the codes:
form and ajax function code:
<div class="row">
<?php echo CHtml::label('Choose category to add from',''); ?>
<?php $data = array('Celebrities'=>'Celebrities', 'Events'=>'Events', 'Videos'=>'Videos', 'Editorials'=>'Editorials'); ?>
<?php echo CHtml::dropDownList('category', '', $data, array(
'empty'=>'select category'
)); ?>
</div>
<script type="text/javascript">
$(function() {
document.getElementById('itemslabel').style.display='none';
});
$("#category").change(function(){
document.getElementById('itemslabel').style.display='block';
var category = $(this).val();
if(category === ''){
document.getElementById('items').style.display='none';
}
else{
$.ajax({
url:'/tonyward/index.php/news/selectedCategory',
data:{category:category},
async:true,
type:'POST',
dataType:'html',
success:function(data) {
document.getElementById('items').style.display='block';
$('#categoryItems').html(data);
},
cache:false
});
}
});
</script>
<div class="row" id="items">
<?php echo $form->labelEx($model,'idItem', array('id'=>'itemslabel')); ?>
<div id="categoryItems"></div>
<?php echo $form->error($model,'idItem'); ?>
</div>
action selectedCategory
code:
public function actionSelectedCategory(){
if(Yii::app()->request->isAjaxRequest){
$category = $_POST['category'];
$this->renderPartial('_categoryItems', array(
'category'=>$category
));
}
}
_categoryItems
code:
<?php $data = CHtml::listData($category::model()->findAll(), 'id', 'thumbtitle'); ?>
<?php echo CHtml::dropDownList('subcategory', '', $data); ?>
Note: I displayed and hid some elements by my js code.
Upvotes: 0
Reputation: 24638
Do you know that your page AJAX call returns the WHOLE PAGE plus <script>alert('done');</script>
? Which is really NOT json
as specified by the dataType
option. Therefore, I expect that there will be a parse error
and there's no .error
handler to fire. And if the call was to be success
ful, you do not render what's is returned ... therefore you wont see the alert.
Suggestions
category
is set only the code within the if
block is returned<script>alert('done');</script>
just use done
==> `echo "done"success
handler include alert( data )
dataType
to text
error
handler just in case.category
in the PHP code is: id
or name
... and many others may have the same issue and would avoid the question.Upvotes: 1
Reputation: 171669
A select
tag has no href
so this.href
isn't going to produce a valid URL for your AJAX.
Upvotes: 1