简体   繁体   中英

How can i run ajax on php codeigniter

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

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 -

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

And add a click handler to the 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. 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.

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

<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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM