[英]How can i run ajax on php codeigniter
I'm trying to use javascript code on my codeIgniter project. 我正在尝试在我的codeIgniter项目上使用javascript代码。 But I didn't execute it.
但是我没有执行。 I tried too many ways for solve it: I called the external js code, it didn't work.
我尝试了多种解决方法:我调用了外部js代码,但没有用。 I put in my view page it dind't work as well.
我在视图页面上放了它,也没有用。 I think the problem is simple but I can't see it.
我认为问题很简单,但我看不到。 What do you think about this problem?
您如何看待这个问题?
view.php view.php
<html>
<?php include("/external/css/style.css"); ?>
<head>
<title>New Customer</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>
function makeAjaxCall(){
$.ajax({
alert("I'm here"); // I cant see that
type: "post",
url: "http://localhost/codeigniter_2/index.php/homepage/verifyUser",
cache: false,
data: $('#addCust').serialize(),
success: function(json){
try{
var obj = jQuery.parseJSON(json);
alert( obj['STATUS']);
}catch(e) {
alert('Exception while request..');
}
},
error: function(){
alert('Error while request..');
}
});
}
</script>
</head>
<body>
<h1>Customer Add</h1><a href="http://localhost/codeigniter_2/index.php">list</a>
<form name="addCust" method="post" action="insert">
<table border="1">
<tr>
<th>Customer Name:</th>
<td><input type="text" id="custom" name="customerName"/></td>
<tr>
<th>Customer Phone:</th>
<td><input type="text" name="phone"/></td>
<tr>
<th>Address:</th>
<td><input type="text" name="address"/></td>
.
.
.
<tr>
<td><input type="button" onclick="javascript:makeAjaxCall();" value="Submit"></td>
<td><input type="reset"></td>
</tr>
</form>
</table>
</body>
</html>
conroller.php conroller.php
public function verifyUser()
{
$userName = $_POST['customerID'];
$phone = $_POST['phone'];
$address = $_POST['address'];
if($userName=='' && $phone=11){
$status = array("STATUS"=>"true");
}
echo json_encode ($status) ;
}
What's wrong?? 怎么了??
You need to put your code in a document ready handler - 您需要将代码放入准备就绪的文档处理程序中-
$(document).ready(function() {
// your code here
function makeAjaxCall....
// EDIT: adding the click handler
$('#formSubmit').click(function()....
});
Also, instead of doing this - 另外,除了这样做,
<input onclick="javascript:makeAjaxCall();" value="Submit">
I would give the input an id - 我会给输入一个ID-
<input name="submit" id="formSubmit" value="Submit">
And add a click handler to the JavaScript - 并将点击处理程序添加到JavaScript中-
$('#formSumbit').click(function(e) {
e.preventDefault(); // keeps the button from acting normally
makeAjaxCall(); // calls the function
});
One more thing - use console.log()
instead of alert()
in your code. 还有一件事-在代码中使用
console.log()
而不是alert()
。 Alerts are not a good method of troubleshooting because they cause the code to halt until acknowledgement and using them during an AJAX call is especially bad. 警报不是解决问题的好方法,因为警报会导致代码暂停,直到得到确认为止,并且在AJAX调用期间使用警报尤其糟糕。
change this 改变这个
<input type="button" onclick="javascript:makeAjaxCall();" value="Submit">
to 至
<input type="button" onclick="makeAjaxCall();" value="Submit">
EDITED 已编辑
$userName = $_POST['customerID'];// check this
$phone = $_POST['phone'];
$address = $_POST['address'];
if($userName=='' && $phone=11){
$status = array("STATUS"=>"true");
}
echo json_encode ($status) ;
also add id
to form 还添加
id
到表单
<form name="addCust" id="addCust" method="post" action="insert">
Hello Here you can check the below link and it will useful for you 您好,您可以在下面查看以下链接,它将对您有用
http://www.ccode4u.com/how-jquery-ajax-works-in-codeigniter.html
Thank you :) 谢谢 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.