【问题标题】:not able to pass data to controller using Ajax with jquery ,CodeIgniter (PHP framework)无法使用 Ajax 和 jquery 将数据传递给控制器​​,CodeIgniter(PHP 框架)
【发布时间】:2015-01-07 08:23:30
【问题描述】:

使用 AJAX 将数据传递给控制器 这是我编写的将数据传递给控制器​​的脚本,但数据没有传递给控制器​​

这是我要传递的输入数据

    <div class="form-group">
        <table class="table table-striped b-t b-light text-sm">
            <thead>
                <tr>
                <th>ID</th>
                <th>Question</th>
                <th>answer</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($quet as $row) { ?>
                <tr>
                <td ><?php echo $row['id']; ?></td>
                <td>
                <?php echo $row['question']; ?>
                </td>
                <td><input type='text' name='name' required="required" class="form-control" placeholder='Enter Your Answer'></td>
                </tr>
                <?php } ?>  
            </tbody>
        </table>
    </div>
<button class="btn btn-primary nextBtn btn-lg pull-right" id ="next" type="button" >Next</button>

和脚本

<script>
           $(document).ready(function($){
    $("#next").click(function(){

     var array = $("name").val()

     $.ajax({
          type: "POST",
          datatype:"json",
          url: BASE_URL+"/student/add",
          data: 'data='+array,
          contentType:'application/json',
          processData: false,              
          error: function(response) {console.log('ERROR '+Object.keys(response)); },
          success: function(response) {
              console.log(response)

          }});

        return false;
    });  
});
</script>

和学生控制器

function add(){

      if($this->student_model->add($this->input->post()))
        {
        $response['success'] = TRUE;
        }
        else
        {
        $response['success'] = FALSE;
        }
        echo json_encode($response);  
    }

【问题讨论】:

  • 首先,您必须将输入文本框的名称从“名称”更改。然后你必须为它分配一个 id 或一个唯一的类。之后,您可以将 jquery 称为 var array = $("#id").val()
  • @Hoja.M.A 我为输入文本添加了代码
  • id="next" 按钮在哪里,是否在您的代码中..?
  • @HardikRanpariya 是 id ="next" 按钮它在代码中我已经编辑了问题
  • 好的,然后再次检查我的代码,我编辑该代码。

标签: php jquery sql ajax codeigniter


【解决方案1】:

试试这个。您的数据可能格式错误

data: {'data':array}

编辑

 <input type='text' name='answer' id='answer' required="required" class="form-control" placeholder='Enter Your Answer' />
<script>
   $(document).ready(function($){
        $("#next").click(function(){

        var array = $("#answer").val() // see the change name to id, see the html also

         $.ajax({
             type: "POST",
              url: BASE_URL+"/student/add",
              data:{'data':array},
              error: function(response) {console.log('ERROR '+Object.keys(response)); },
              success: function(response) {
                 console.log(response)
              }});
         });  
    });
 </script>

【讨论】:

  • 当你点击下一个按钮时,不要忘记检查浏览器实用程序来监控 ajax 请求
【解决方案2】:

还要检查你的 JS 中是否正确接收了数组

<script>
  $(document).ready(function($){
    $("#next").click(function(){

     var array = $("#id").val()

     $.ajax({
          type: "POST",
          datatype:"json",
          url: BASE_URL+"/student/add",
          data: {
            'data':array
          },
          contentType:'application/json',
          processData: false,              
          error: function(response) {console.log('ERROR '+Object.keys(response)); },
          success: function(response) {
              console.log(response)

          }});

        return false;
    });  
});
</script>

在您的控制器中,您应该将$this-&gt;input-&gt;post() 更改为$this-&gt;input-&gt;post('data')

【讨论】:

    【解决方案3】:

    从您提到的代码中,您需要检查以下几点:

    1) 由于您使用的是contentType:'application/json',,所以使用数据格式为data: {'data':array}

    2) 最后检查urlurl: BASE_URL+"/student/add",是否可以访问。

    希望这会有所帮助:)

    【讨论】:

    • var array = $("name").val() 这里与类的关系是什么?如果有一个类“name”,那么它必须像 $(".name").val() (dot infront)
    • @Kiren,是的,你是对的,我没有注意到,我以为是 $(".name").val() 但它是 $("name").val() .
    • @jenis 输入文本包含在此 div 中 &lt;div class="form-group"&gt; &lt;table class="table table-striped b-t b-light text-sm"&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;ID&lt;/th&gt; &lt;th&gt;Question&lt;/th&gt; &lt;th&gt;answer&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;?php foreach ($quet as $row) { ?&gt; &lt;tr&gt; &lt;td &gt;&lt;?php echo $row['id']; ?&gt;&lt;/td&gt; &lt;td&gt; &lt;?php echo $row['question']; ?&gt; &lt;/td&gt; &lt;td&gt;&lt;input type='text' name='name' required="required" class="form-control" placeholder='Enter Your Answer'&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?php } ?&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/div&gt;
    • @SoftwareDev,请尝试使用 Firebug(Mozilla 插件)控制台,以便您可以调试并找出根本原因。试试这个 ajax url value => url: BASE_URL+"/index.php/student/add",
    【解决方案4】:

    您需要在代码中更改以下内容。

    在脚本中

    <script>
    $(document).ready(function($){
        $("#next").click(function(){
    
         var array = $("input[name]").val();
    
         $.ajax({
    
              type: "POST",
              url: BASE_URL+"/student/add",
              data: {'data':array},            
              error: function(response) {console.log('ERROR '+Object.keys(response)); },
              success: function(response) {
                  console.log(response)
    
              },
              datatype:"json"
         });
    
            return false;
        });  
    });
    </script>
    

    在学生控制器中

    function add(){
    
          if($this->student_model->add($this->input->post('data')))
            {
                $response['success'] = TRUE;
            }
            else
            {
                $response['success'] = FALSE;
            }
            echo json_encode($response);  
        }
    

    希望对你有所帮助。

    【讨论】:

    • var array = $("#name").val() 为此他必须在输入元素中添加 id 属性?
    • @Hardik Ranpariya $("#name").val() 所以我必须将 id 分配给输入字段 id="name"
    • 我已经改变了这个。您现在无需将 id 分配给输入字段 id="name"
    • @Hardik Ranpariya 很好,它没有显示任何错误,但没有提交数据
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多