[英]Cannot pass value to Codeigniter controller using ajax
I am having a trouble while sending a value from an input element to Codeigniter controller by using ajax. 使用ajax从输入元素向Codeigniter控制器发送值时遇到麻烦。
Since I have to use WYSIWYG editor ( summernote ), thus I can just receive the input inside a <script>
. 由于我必须使用所见即所得的编辑器( summernote ),因此我只能在
<script>
内接收输入。 However when I press the submit button, it just reloads the current page rather than the one in the controller. 但是,当我按下“提交”按钮时,它只会重新加载当前页面,而不是控制器中的页面。 Here is my code:
这是我的代码:
PHP view PHP视图
<section id="mainContent">
<form method="post">
<input type="text" id="textbox" class="editor" name="textbox">
<input id="submit_btn" type="button" name="sutmit" value="submit" onclick="myFunction()">
</form>
</section>
<script type="text/javascript">
function myFunction() {
var markupStr = $('#textbox').summernote('code');
alert(markupStr);
$.ajax({
type : 'POST',
url : "<?= site_url().'cintern/save'; ?>",
async : false,
data : {'iDes': markupStr},
success : function(data){
alert("Success!");
}
});
return false;
};
</script>
PHP controller PHP控制器
public function save()
{
$session_data = $this->session->userdata('logged_in');
$data['id'] = $session_data['idlogin'];
$data['role'] = $session_data['role'];
$data['pageTitle'] = 'Success';
$data['iDes'] = $this->input->post('iDes');
$this->load->view('templates/header', $data);
$this->load->view('internship/success', $data);
$this->load->view('templates/footer');
}
Please help! 请帮忙! Thank you in advance.
先感谢您。
You should use onsubmit
of <form>
tag instead. 您应该改为使用
<form>
标记的onsubmit
。
<section id="mainContent">
<form method="post" onsubmit="myFunction()">
<input type="text" id="textbox" class="editor" name="textbox">
<input id="submit_btn" type="button" name="sutmit" value="submit">
</form>
</section>
The reason is, if you handle the onclick
of your <input type="submit">
, you can't intercept request of <form>
tag after submit data. 原因是,如果您处理
<input type="submit">
的onclick
,提交数据后将无法拦截<form>
标记的请求。
I hope this can help you. 希望对您有所帮助。
Since you are using JQuery for the ajax I suggest you use it to capture the submit also. 由于您将Ajax使用JQuery,因此建议您也使用它来捕获提交。
Remove the inline onclick assignment. 删除内联onclick分配。 Give the form an id attribute.
给表单一个id属性。
<section id="mainContent">
<form method="post" id="target">
<input type="text" id="textbox" class="editor" name="textbox">
<input id="submit_btn" type="button" name="sutmit" value="submit">
</form>
</section>
Turn myfunction()
into a 'submit' event handler 将
myfunction()
转换为“提交”事件处理程序
<script type="text/javascript">
$("#target").submit(function (event) {
var markupStr = $('#textbox').summernote('code');
//stop the normal submit from happening
event.preventDefault();
$.ajax({
type: 'POST',
url: "<?= site_url().'cintern/save'; ?>",
async: false,
data: {'iDes': markupStr},
success: function (data) {
//do stuff with data then redirect to 'after_save'
console.log(data.results, data.otherstuff);
window.location.href = "http://example.com/cintern/after_save";
}
});
});
</script>
Controller: 控制器:
public function save()
{
$data = $this->input->post(NULL, TRUE);
// $data is now a semi-sanitized version of $_POST
$_SESSION['iDes'] = $data['iDes'];
//do other stuff with $data
echo json_encode(['results' => "success", 'otherstuff' => $foo]);
}
public function after_save()
{
$session_data = $this->session->userdata('logged_in');
$data['id'] = $session_data['idlogin'];
$data['role'] = $session_data['role'];
$data['pageTitle'] = 'Success';
$data['iDes'] = $session_data['iDes'];
$this->load->view('templates/header', $data);
$this->load->view('internship/success', $data);
$this->load->view('templates/footer');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.