0

I'm having a zii.widgets.jui.CJuiDialog box send a form via ajax to my controller. However, when I test the controller for the presence of the post if(isset($_POST['Item'])). It isn't present and I don't really know why. I'm adapting this post http://www.yiiframework.com/wiki/145/cjuidialog-for-create-new-model/ to fit my own needs.

This is the form within the model:

<div class="form">

<form id="email-item-form" action="/www.mywebsite.ca/item/1/my-first-item" method="post">   <div id="email-item-success" class="label label-success"></div>
<div id="email-item-failure" class="label label-important"></div>

<div class="row">
    <label for="Item_email_item">Email this item to:</label>        <input placeholder="Send to..." name="Item[email_item]" id="Item_email_item" type="text" />        <div class="errorMessage" id="Item_email_item_em_" style="display:none"></div>   </div><!-- row -->
<div class="row">
    <label for="Item_email_item_sender">Email sent from:</label>    
    <input placeholder="Sent from..." name="Item[email_item_sender]" id="Item_email_item_sender" type="text" />        <div class="errorMessage" id="Item_email_item_sender_em_" style="display:none"></div>    </div><!-- row -->

<br />
</form>

Controller

public function actionEmail($id)
{
        if(isset($_POST['Item']))
    {

              // Rest of code
            }
} 

The data is being sent via CHtml::ajax and declaring 'type'=>'post'. The process has been working in terms of sending the item id to the controller and sending back responses to the modal box. The problem has started since I tried to confirm the presence of $_POST['Item'] in order to set $model->attributes=$_POST['Item'];

Any help in solving this would be great thank you.

Update to show CHtml::ajax()

var url = '<?php echo Yii::app()->createUrl("item/email", array("id"=>$model->id)); ?>';

<?php echo CHtml::ajax(array(
        'url'=>'js:url',
        'data'=> "js:$(this).serialize()",
....// Rest of code
Jonnny
  • 4,939
  • 11
  • 63
  • 93

2 Answers2

0

I don't see any inputs or other form data that are name="Item". However, if you change your php code to look for the exact names of the form, it should find them:

public function actionEmail($id)
{
        if(isset($_POST['Item[email_item_sender]']))
    {

              // Rest of code
            }
} 

Edit:

If you want to see all your post data for debugging purposes, you can use this code from PHP: Possible to automatically get all POSTed data?

foreach ($_POST as $key => $value)
echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>";
Community
  • 1
  • 1
Jack Cole
  • 1,528
  • 2
  • 19
  • 41
0

By default jQuery sends AJAX requests by a GET. Try this:

<?php echo CHtml::ajax(array(
    'url'=>'js:url',
    'data'=> "js:$(this).serialize()",
    'type'=>'POST'
    ...
Michiel
  • 2,143
  • 1
  • 21
  • 21