我有一个弹出菜单,里面有一个表单和一个文本框。我想检索该值并将其提交给我的 Controller 。提交后,我希望关闭弹出窗口并返回到其他页面。我尝试过使用 AJAX,但我可能做错了什么。
<form onsubmit="return(testing());" method="POST">
<input type="text" name="vip_text_box" id="vip" value="<?php echo $total_amount ?>"> <br>
<input type="submit" name="Redeem" value="Redeem">
</form>
现在是我的 JavaScript
function testing() {
$test = document.getElementById("vip").value;
var url = base_url + '/index.php/home/redeeming_form_value';
$.ajax({
type : 'POST',
url : url,
data : {'myvalue':test},
success: function(data){
alert(data);
}
});
};
在我的 Controller 中
function redeeming_form_value() {
$amount = $this->input->post('myvalue');
return $amount;
在 AJAQ 中我取得了成功,并且想要提醒我的数据(值)以确保它甚至可以工作,但它什么也没做。当我单击“提交”时,我的弹出 View 就会消失。
最佳答案
尝试将 dataType: "json"添加到您的 Ajax
dataType: "json",
然后在您的 Controller 中使用 json_encode 返回您的数据
$arr = array('amount' => $amount);
return json_encode($arr);
测试响应是否成功
console.log(data.amount);
关于javascript - 将表单数据发送到 Controller (Codeigniter),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41111560/