如何使用jquery ajax post方法[Codeigniter]将textarea数据保存到会话?

时间:2017-02-09 04:55:25

标签: javascript php jquery ajax codeigniter

我想在Codeigniter smarty中使用jquery ajax post方法将textarea保存到Controller会话。我无法将数据保存到会话中。我怎样才能做到这一点?请指导我完整的描述编码示例。

  

$这 - >会话而> set_userdata()

这是我的观点

<div class="col-md-9" id="label">
    <form method="post" id="programmingQ1" name="programmingQ1" action="{base_url('home/question2')}" enctype="multipart/form-data">
      <div class="form_block">   
        {foreach $question_data as $qdata}  
            <p class="question">
                {$qdata->Question|nl2br}
            </p>
        {/foreach}

            <label for="text">Fill in the answer :</label>
            <br>
            <textarea name="answer1"  id="text" maxlength="2000" class="form-control" rows="20">{if isset($value)}{$value['answer1']}{/if}</textarea>
          <input type="button" id="mybutton" name="next" class="btn btn-default nextbutton" id="btn_submit" value="Next">
      </div>
    </form>
</div>

这是我的ajax

<script type="text/javascript">
$('input#mybutton').click(function() {
var text = $('textarea#text').val();

alert(text);


$.ajax({
  url : "saveq1",
  type: "POST",
  data : text,
  success: function(data)
  {
      alert(data);
  },

});

});
</script>

这是我的控制器

public function saveq1()
{
    $data=array();

    if($_SERVER['REQUEST_METHOD']=='POST')
    {
        $this->load->library('session');
        $this->session->set_userdata('answer1',$this->input->post());
        print_r($this->session->userdata('answer1'));
    }
    $data['value']=$this->session->userdata('question1');
    $this->smarty->view('question1.tpl',$data);
}

1 个答案:

答案 0 :(得分:0)

<?php session_start();?>

<div class="col-md-9" id="label">
    <form method="post" id="programmingQ1" name="programmingQ1" action="{base_url('home/question2')}" enctype="multipart/form-data">
      <div class="form_block">   
        {foreach $question_data as $qdata}  
            <p class="question">
                {$qdata->Question|nl2br}
            </p>
        {/foreach}

            <label for="text">Fill in the answer :</label>
            <br>
            <textarea name="answer1"  id="text" maxlength="2000" class="form-control" rows="20">{if isset($value)}{$value['answer1']}{/if}</textarea>
          <input type="button" id="mybutton" onclick="setTxtAreaDataToSession();" name="next" class="btn btn-default nextbutton" id="btn_submit" value="Next">
      </div>
    </form>
</div>



<script type="text/javascript">
   function setTxtAreaDataToSession(){
       var text = $('#text').val();

       '<?php $this->session->set_userdata(?>'+text+'<?php)?>';  // setting in session.      

   }
</script>