我怎样才能运行AJAX的PHP codeigniter [英] How can i run ajax on php codeigniter

查看:101
本文介绍了我怎样才能运行AJAX的PHP codeigniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用javascript code对我的codeIgniter项目。但我没有执行它。我尝试过很多方法解决这个问题:我打电话给外部JS code,它没有工作。我把我的视图页面就dind't正常工作。我认为这个问题很简单,但我无法看到它。你怎么看待这个问题?

I'm trying to use javascript code on my codeIgniter project. But I didn't execute it. I tried too many ways for solve it: I called the external js code, it didn't work. 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

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

什么是错?

推荐答案

您需要把你的code在文档准备好处理程序 -

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

我会给输入自己的ID -

I would give the input an id -

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

和添加一个单击处理程序给JavaScript -

And add a click handler to the JavaScript -

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

还有一件事 - 使用的console.log()而不是警报()在code 。警报是不是解决问​​题的好方法,因为它们所造成的code停止,直到确认和AJAX调用过程中使用它们尤其糟糕。

One more thing - use console.log() instead of alert() in your code. 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的PHP codeigniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆