javascript - How to pass multiple variables through ajax to codeigniter controller? One variable is via serialize - Stack Overfl

I have created an edit form in which values are to be send in my codeigniter controller via ajax. The v

I have created an edit form in which values are to be send in my codeigniter controller via ajax. The values in the form that are to be updated is passed using serialized function var curr_val = $("#edit_currency").serialize(); but the id of the values to be updated is not included in the serialize method and is just passed to my javascript function via var curr_id = $("#curr_id").val(); The problem is I cannot successfully passed this 2 variable in ajax to be received in my controller function. There is no updating happens. How can I get this done? Thanks a lot. Here are my codes

View:

<?php
     //echo form_open('/display/student_update');
     foreach($curr_values as $row){


      echo"<input type='hidden' name='curr_id' id='curr_id'  value=".$row->id.">";
      ?>
      <form method="post" action="" id="edit_currency">
     <div class="row">
          <div class="span4"><strong><?php echo $lbl_currency_name;?></strong> </div>
     </div>
     <div class="row">
          <div class="span4"><strong><?php echo"<input type='text' name='currency[pretty_name]' id='pretty_name'  value=".$row->pretty_name.">"; ?></strong> </div>
     </div>
     <div class="row">
          <div class="span4"><strong><?php echo $lbl_currency_code; ?></strong> </div>
     </div>
     <div class="row">
          <div class="span4"><strong><?php echo form_input($currency_code,$row->currency_code);?></strong> </div>
     </div>

    ?>

      <script type="text/javascript">
 $(function(){

 $("#currency_save").click(function(){
 var curr_id = $("#curr_id").val();
 var curr_val = $("#edit_currency").serialize();

 alert(curr_id);
 alert(curr_val);

 $.ajax({
        type: "POST",
        url: "<?php echo base_url();?>currencies/update_currencies",
        dataType:'json',
        data: {'curr_values':curr_val,'curr_id':curr_id},
        success: function(data)
        {
          if(data.notify=="Success"){
            console.log(data.notify);
          }
          else{
           console.log(data.notify);
          }


        }
    });

    $("html, body").animate({ scrollTop: 0 }, 600);
    return false;
    });
    });
   </script>

Controller:

function update_currencies(){

 $curr_val=$this->input->post('curr_values');
 $curr_id = $this->input->post('curr_id');

 $query = $this->course_booking_model->update_currencies($curr_id,$curr_val);

 if($query){
  $notification = "Success";
  }
 else{
 $notification = "Failed";
 }

  echo json_encode(array('notify'=>$notification));
 }

I have created an edit form in which values are to be send in my codeigniter controller via ajax. The values in the form that are to be updated is passed using serialized function var curr_val = $("#edit_currency").serialize(); but the id of the values to be updated is not included in the serialize method and is just passed to my javascript function via var curr_id = $("#curr_id").val(); The problem is I cannot successfully passed this 2 variable in ajax to be received in my controller function. There is no updating happens. How can I get this done? Thanks a lot. Here are my codes

View:

<?php
     //echo form_open('/display/student_update');
     foreach($curr_values as $row){


      echo"<input type='hidden' name='curr_id' id='curr_id'  value=".$row->id.">";
      ?>
      <form method="post" action="" id="edit_currency">
     <div class="row">
          <div class="span4"><strong><?php echo $lbl_currency_name;?></strong> </div>
     </div>
     <div class="row">
          <div class="span4"><strong><?php echo"<input type='text' name='currency[pretty_name]' id='pretty_name'  value=".$row->pretty_name.">"; ?></strong> </div>
     </div>
     <div class="row">
          <div class="span4"><strong><?php echo $lbl_currency_code; ?></strong> </div>
     </div>
     <div class="row">
          <div class="span4"><strong><?php echo form_input($currency_code,$row->currency_code);?></strong> </div>
     </div>

    ?>

      <script type="text/javascript">
 $(function(){

 $("#currency_save").click(function(){
 var curr_id = $("#curr_id").val();
 var curr_val = $("#edit_currency").serialize();

 alert(curr_id);
 alert(curr_val);

 $.ajax({
        type: "POST",
        url: "<?php echo base_url();?>currencies/update_currencies",
        dataType:'json',
        data: {'curr_values':curr_val,'curr_id':curr_id},
        success: function(data)
        {
          if(data.notify=="Success"){
            console.log(data.notify);
          }
          else{
           console.log(data.notify);
          }


        }
    });

    $("html, body").animate({ scrollTop: 0 }, 600);
    return false;
    });
    });
   </script>

Controller:

function update_currencies(){

 $curr_val=$this->input->post('curr_values');
 $curr_id = $this->input->post('curr_id');

 $query = $this->course_booking_model->update_currencies($curr_id,$curr_val);

 if($query){
  $notification = "Success";
  }
 else{
 $notification = "Failed";
 }

  echo json_encode(array('notify'=>$notification));
 }
Share Improve this question asked Aug 18, 2013 at 23:17 EliEli 1,2765 gold badges32 silver badges63 bronze badges 1
  • What is the response you get from server, when you see it in the Developer tools? Cannot pass means your CI function is not executed at all or executed but you miss the values? – Nish Commented Aug 19, 2013 at 6:11
Add a ment  | 

1 Answer 1

Reset to default 2
 $.ajax({
        type: "POST",
        url: "<?php echo base_url();?>currencies/update_currencies",
        data: curr_values + '&curr_id=' + curr_id,
        success: function(data)
        {
          if(data.notify=="Success"){
            console.log(data.notify);
          }
          else{
           console.log(data.notify);
          }


        }
    });

You converted the curr values to query string so you just need to concat the curr id as query string as well.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745654693a4638473.html

相关推荐

  • AlignRAG:浙江大学提出的可泛化推理对齐框架,助力 RAG 系统解决推理失配问题

    近年来,检索增强生成(Retrieval-Augmented Generation, RAG)成为知识驱动文本生成的核心范式。然而,现有的 RAG 系统在推理过程中常常出现“推理失配”问题,即模型的推理路径与检索到的证据不一致,导致生成内容

    1小时前
    00
  • OWASP TOP10

    什么是OWASP?它的全称是 Open Web Application Security Project(开放式 Web 应用程序 安全 项目)TOP 10OWASP Top 10的意思就是10项最严重的Web 应用程序安全风险列表 ,它总

    1小时前
    00
  • 如何快速判断 Flutter 库是否需要适配鸿蒙?纯 Dart 库无需适配!

    在鸿蒙开发中,选择合适的 Flutter 库至关重要。纯 Dart 库因其跨平台特性,无需适配即可直接使用。但对于新手来说,如何判断一个库是否为纯 Dart 库呢?本文将为你提供清晰的判断方法和实用技巧。一、检查 pubspec.yaml

    50分钟前
    00
  • MongoDB “升级项目” 大型连续剧(2)

    上期写的是非必要不升级,想到这个事情,有一些事情的仔细琢磨琢磨,为什么数据库升级的事情在很多公司都是一个困扰,从一个技术人的观点,升级是一件好事,功能提升了,性能提升了,开发效率和一些数据库使用的痛点也被解决了,为什么就不愿意升级呢?如果只

    48分钟前
    00
  • CUT&amp;amp;Tag 数据处理和分析教程(7)

    过滤某些项目可能需要对比对质量分数进行更严格的过滤。本文细讨论了bowtie如何分配质量分数,并举例说明。MAPQ(x) = -10 * log10log10(P(x is mapped wrongly)) = -10 * log10(p)

    46分钟前
    10
  • 大模型驱动金融数据应用的实战探索

    近年来,人工智能技术的飞速发展正在重塑全球各行各业的生态格局,金融行业作为数据密集型领域,更是首当其冲。大模型凭借其强大的自然语言处理、逻辑推理和生成能力,逐渐成为金融数据应用的核心驱动力。本文将从行业背景与趋势、核心场景重构、产品能力提升

    35分钟前
    00
  • maxwell遇到的一则问题

    结论和原因maxwell的元数据库里面没有存储全部的schema数据(就是少数据了),导致相关表的DDL校验失败。PS:我这里maxwell的作用只是采集库表修改情况的统计粗粒度指标,因为之前maxwell在运行报错的时候,直接修改了pos

    32分钟前
    00
  • Windows Server20192022 Evaluation评估版未激活导致关机问题

    摘要&#xff1a;在安装Windows Server 20192022后&#xff0c;会出现系统版本为 Evaluation 评估版情况&#xff0c;如提示Windows许可证已到期&#xff0c;就

    30分钟前
    00
  • 人工智能与ai有什么区别

    一、引言:概念之辨的必要性在科技浪潮席卷全球的当下,人工智能(Artificial Intelligence,简称AI)已成为人们耳熟能详的词汇。然而,当我们深入探讨时,会发现“人工智能”与“AI”这两个表述在语义和使用场景上存在微妙差异。

    30分钟前
    00
  • windows新建open ai密钥

    api链接 openai的api需要付费才能使用但好像系统变量不知道为啥用不了打印出来&#xff0c;获取到的是None可以用了

    27分钟前
    00
  • 最后讲一遍:ChatGPT 快速生成国内外研究现状的方法

    在科研工作中,梳理国内外研究现状有助于明确研究方向,发现研究空白,为后续研究提供理论支持与创新思路。本文将详细介绍如何借助 ChatGPT 高效生成国内外研究现状,帮助您在有限时间内构建全面、专业的文献综述框架,提升学术写作效率与质量。St

    26分钟前
    00
  • 子网掩码是怎么“掩”的?用积木教你彻底搞懂!

    子网掩码是怎么“掩”的?用积木教你彻底搞懂!前言肝文不易,点个免费的赞和关注,有错误的地方请指出,看个人主页有惊喜。作者:神的孩子都在歌唱你是不是也曾被“子网掩码”这个术语搞得晕头转向?明明是学网络的第一步,却像是打开了数学世界的大门:2

    23分钟前
    00
  • 雨晨 26200.5516 Windows 11 IoT 企业版 LTSC 2024 轻装版

    简述&#xff1a;以下为YCDISM (雨晨作品自2025年03月25日起通用介绍&#xff0c;若无重大更改不再额外敖述) 全程由最新YCDISM2025脱机装载26100.1742_zh-cn_windows_11_

    20分钟前
    00
  • ​2025 轻松部署 Odoo 18 社区版

    随着 Odoo 18 社区版的发布,越来越多的企业希望借助这款开源 ERP 系统实现数字化转型。本文将深入解析传统部署方式的底层逻辑,并揭示如何通过自动化工具实现零门槛快速部署。一、手工部署 Odoo 18 技术全解 Docker 环境搭建

    19分钟前
    00
  • 用Xshell8配置密钥登陆

    1.首先在服务端查看root.sshauthorized_keys是否存在,这是存储公钥的文件,若不存在需新建此文件 2. 打开Xshell8,选择"新建",选择"新建用户密钥生成向导" 给用户

    16分钟前
    00
  • windows切换系统版本

    powershell 管理员身份打开 输入 irm massgrave.devget | iex 输入数字 对应后面写着 change windows edition新的会话框中选择想要的版本即可 获取windows 密钥 官方提供的

    14分钟前
    00
  • 在Windows上使用MetaMCP的完整指南

    在当今AI助手工具快速发展的时代&#xff0c;如何有效管理各种MCP&#xff08;Model Control Protocol&#xff09;服务成为了一个挑战。MetaMCP应运而生&#xff0c;它是

    11分钟前
    00
  • OpenAI“Agent 圣经”翻车?LangChain 创始人怒怼“全是坑”!

    整理 | Tina当前,AI 领域呈现出一种近乎“追星式”的热情氛围,每当有新的东西发布,便迅速引发广泛关注与高度评价,仿佛技术变革即将一触即发。同时大家情绪也波动剧烈,从“危机论”到“爆发论”频繁切换。OpenAI 最近出的《A Pra

    6分钟前
    00
  • IDEA 2025.1 版震撼发布,建议更新

    好久不见甚是想念,有小伙伴催更了。 各位编程大神们!今天我要给大家带来一个超级劲爆的消息!IntelliJ IDEA 2025.1 终极版震撼发布啦!就像一颗超级炸弹在编程界炸开,这些新特性能让咱们的开发效率直接翻倍,简直爽歪歪

    2分钟前
    00
  • 连Claude 3.5都败下阵来,大语言模型能否定位软件服务的故障根因?

    论文的第一作者是香港中文大学(深圳)数据科学学院三年级博士生徐俊杰龙,指导老师为香港中文大学(深圳)数据科学学院的贺品嘉教授和微软主管研究员何世林博士。贺品嘉老师团队的研究重点是软件工程、LLM for DevOps、大模型安全。大型语言模

    27秒前
    00

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信