0

私はCodeigniterのコツをつかみ始めていますが、どうやらここで何かが欠けているようです。

ajaxフォームの送信後にデータをユーザーに返す方法がわかりません。これが私のコードの関連部分で、中に質問があります:

意見:

if (confirm("Are you sure?"))
    {
        var form_data = $('form').serialize();

        $.post("<?php echo site_url();?>" + "/products/update_multiple", (form_data),
// this is what I don't understand: how to populate the variable result with data returned from the model/controller?
           function(result) {
                    $('#myDiv').html(result);
            }
            )
    }

コントローラー

function update_multiple()
{

 $this->load->model('products_model');
 $this->products_model->updateMultiple();
// What goes here, to grab data from the model and send it back to the ajax caller?
}

モデル

function updateMultiple()
{
// I have a drop down list to select the field to update. The values of the drop down list look like this: "TableName-FieldName". Hence the explode.

    $table_field = explode("-",$this->input->post('field'));
    $table = $table_field[0];
    $field = $table_field[1];

    $data = $this->input->post('data');

    $rows = explode("\n", $data);

    foreach($rows as $key => $row)
    {
        $values = explode(" ", $row);
        $arr[$key]["code"] = $values[0];
        $arr[$key][$field] = $values[1];
    }


   if ($this->db->update_batch($table, $arr, 'code'))
   {
       // Here is the problem. What should I return here, so that it would be sent back to the calling ajax, to populate the result variable?
   }

}
4

1 に答える 1

3

jsonを返す

   if ($this->db->update_batch($table, $arr, 'code'))
  {
   print json_encode(array("status"=>"success","message"=>"Your message here"));
  }

確認関数内のjQuery

$.ajax({
type: "POST",
url: "<?php echo site_url();?>" + "/products/update_multiple",
data:  $('form').serialize(),
    dataType: "json",
    success: function(content) {
    if (content.status == "success") {
       $('#myDiv').html(content.message);
    } 
        }
    });
于 2012-11-13T07:41:00.623 に答える