【问题标题】:How can i run ajax on php codeigniter我如何在 php codeigniter 上运行 ajax
【发布时间】:2014-07-18 12:04:42
【问题描述】:

我正在尝试在我的 codeIgniter 项目中使用 javascript 代码。但我没有执行。我尝试了太多方法来解决它:我调用了外部js代码,它不起作用。我把它放在我的视图页面中它也不起作用。我认为问题很简单,但我看不到。您如何看待这个问题?

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

public function verifyUser()
    {
        $userName =  $_POST['customerID'];
        $phone =  $_POST['phone'];
        $address = $_POST['address'];
        if($userName=='' && $phone=11){

            $status = array("STATUS"=>"true");  
        }
        echo json_encode ($status) ;    
    }

怎么了??

【问题讨论】:

  • 您是否尝试在控制器中使用简单的return json_encode()?
  • 在你的 ajax 调用中删除你的alert(I'm Here)。这不是正确的地方

标签: javascript php jquery ajax codeigniter


【解决方案1】:

您需要将代码放入文档就绪处理程序中 -

$(document).ready(function() {
    // your code here
    function makeAjaxCall....
    // EDIT: adding the click handler
    $('#formSubmit').click(function()....
});

另外,不要这样做 -

<input onclick="javascript:makeAjaxCall();" value="Submit">

我会给输入一个 id -

<input name="submit" id="formSubmit" value="Submit">

并向 JavaScript 添加一个点击处理程序 -

$('#formSumbit').click(function(e) {
    e.preventDefault(); // keeps the button from acting normally
    makeAjaxCall(); // calls the function
});

还有一件事 - 在您的代码中使用 console.log() 而不是 alert()。警报不是一种很好的故障排除方法,因为它们会导致代码在得到确认之前停止,并且在 AJAX 调用期间使用它们尤其糟糕。

【讨论】:

  • 不需要准备好文档,因为在按钮点击时会调用函数
  • @rajeshkakawat 可能不需要它,但它会确保该功能可用,并且在文档准备好时正确绑定 jQuery。
【解决方案2】:

改变这个

<input type="button" onclick="javascript:makeAjaxCall();" value="Submit">

到

<input type="button" onclick="makeAjaxCall();" value="Submit">

已编辑

$userName =  $_POST['customerID'];// check this
$phone =  $_POST['phone'];
$address = $_POST['address'];
if($userName=='' && $phone=11){

    $status = array("STATUS"=>"true");
}
echo json_encode ($status) ;

还可以在表单中添加id

<form name="addCust" id="addCust" method="post" action="insert">

【讨论】:

  • 对不起,我复制到这里时出错了。在我的项目中,此代码如您所说。但是效果不太好
  • 从 ajax 中删除警报,它破坏了 ajax 功能
【解决方案3】:

您好,您可以在这里查看下面的链接,它对您很有用

    http://www.ccode4u.com/how-jquery-ajax-works-in-codeigniter.html

谢谢你:)

【讨论】:

    猜你喜欢
    • 2012-08-11
    • 1970-01-01
    • 2013-12-15
    • 2012-04-12
    • 1970-01-01
    • 2012-01-24
    • 1970-01-01
    • 2012-09-04
    • 1970-01-01
    相关资源
    最近更新 更多