php – 表单需要在codeigniter中将数据发布到控制器
发布时间:2020-12-13 22:25:11 所属栏目:PHP教程 来源:网络整理
导读:我有一张表格, forminput id="amount" type="text" placeholder="Give amount" name="myamount"/button class="btn btn-lg" type="submit" id="btn1" FirstBTN/button button class="btn btn-lg" type="submit" id="btn2"SecondBTN/button/form 哪个需要将数
我有一张表格,
<form> <input id="amount" type="text" placeholder="Give amount" name="myamount"/> <button class="btn btn-lg" type="submit" id="btn1" >FirstBTN</button> <button class="btn btn-lg" type="submit" id="btn2">SecondBTN</button> </form> 哪个需要将数据发布到控制器内的函数.我正在尝试制作两个按钮,将数据发布到两个不同的控制器功能.这是我在html文件(view)中编写的jquery代码: <script type="text/javascript"> $('#btn1').click(function() { $.ajax({ url: 'helloworld/firstmoney',type: 'POST',data: {'submit' : true,myamount: $("#amount").val()},success: function (result) { } }); }); </script> <script type="text/javascript"> $('#btn2').click(function() { $.ajax({ url: 'helloworld/secondmoney',success: function (result) { } }); }); </script> 这是控制器,但我的控制器中没有任何值. public function firstmoney(){ $this->load->helper('form'); $Money = $this->input->post("myamount"); $data = array( 'Money' => $Money ); $this->load->view("page1",$data); } public function secondmoney(){ $this->load->helper('form'); $Money = $this->input->post("myamount"); $data = array( 'Money' => $Money ); $this->load->view("page2",$data); } 我的控制器名称是helloworld,我需要以$money分配值,但我不能让它工作.我是初学程序员,所以请告诉我应该遵循的步骤. 解决方法
首先,你的AJAX看起来应该是这样的.
$('#btn2').click(function() { $.ajax({ url: 'helloworld/secondmoney',method: 'POST',//not type data: { myamount: $("#amount").val() }//no need for submit variable (although you can use it) since this will be submitted on click; no quotes over variables }) .done(function(result)) { //do your stuff on response }); }); 第二:不需要ajax,你应该使用经典形式和post方法,因为你在控制器/方法中进行重定向,其中数据被发布到.AJAX需要JSON格式化响应,在这种情况下不是这样.因此更改视图以具有方法和动作的形式,更改控制器/方法以将JSON字符串返回到AJAX. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |