gpt4 book ai didi

php - 表单需要将数据发布到 codeigniter 中的 Controller

转载 作者:可可西里 更新时间:2023-10-31 23:46:21 24 4
gpt4 key购买 nike

我有一个表格,

<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>

需要将数据发布到 Controller 内部的函数。我正在尝试制作两个按钮,将数据发布到两个不同的 Controller 功能。这是我用 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',
type: 'POST',
data: {'submit' : true ,myamount: $("#amount").val()},
success: function (result) {

}
});

});

</script>

这是 Controller ,但我没有在我的 Controller 中获得任何值..

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);

}

我的 Controller 名称是 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 方法的经典形式,因为你在数据发布到的 Controller /方法中进行重定向。AJAX 需要 JSON 格式的响应,但在本例中并非如此。因此,无论是将 View 更改为具有方法和操作的形式,还是更改 Controller /方法以将 JSON 字符串返回给 AJAX。

关于php - 表单需要将数据发布到 codeigniter 中的 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34797404/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com