我如何在php上运行ajax编码器


How can i run ajax on php codeigniter

我试图在我的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) ;    
    }

怎么了? ?

你需要把你的代码放在一个文档准备处理程序-

$(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调用期间使用它们尤其糟糕。

change this

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

你好,你可以看看下面的链接,它会对你有用的

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

谢谢你