簡體   English   中英

我如何在PHP Codeigniter上運行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調用期間使用警報尤其糟糕。

改變這個

<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

謝謝 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM