Welcome Guest, Not a member yet? Register   Sign In
Ajax post method not working
#1

[eluser]Unknown[/eluser]
Ajax post not working in my code i don't know what exact problem the original code (controller code) please help me
Code:
class Ajax_post extends CI_Controller {

function __construct()
{
parent::__construct();  
$this->load->helper(array('url'));
}

function index()
{

$this->load->view('ajax_post');
}

function post_action()
{

if(($_POST['username'] == "") || ($_POST['password'] == ""))
{
  $message = "Please fill up blank fields";
  $bg_color = "#FFEBE8";

}elseif(($_POST['username'] != "myusername") || ($_POST['password'] != "mypassword")){
  $message = "Username and password do not match.";
  $bg_color = "#FFEBE8";

}else{
  $message = "Username and password matched.";
  $bg_color = "#FFA";
}

$output = '{ "message": "'.$message.'", "bg_color": "'.$bg_color.'" }';

}
}

?>

This is my view page code

Code:
<div id="form_message"></div>
    &lt;form name="ajax_form" id ="ajax_form" method="post"&gt;

      Username/Email:*&lt;input type="text" name="username" id="username" size="30" /&gt;&lt;br/><br/>
      Password:*&lt;input type="password" name="password" id="password" size="30" /&gt;&lt;br/><br/>
      &lt;input type="submit" value="Submit" name="login_submit" id="login_submit"&gt;

  &lt;/form&gt;
</div>

This is my javascript code (ajax_post.js)

Code:
function getData(){

    var username=$("#username").val();
    var password=$("#password").val();      
    $.ajax({
    type: "POST",
    url: "http://localhost/CodeIgniter/index.php/ajax_post/post_action",
    dataType: "json",
    data: "username="+username+"&password;="+password,
    cache:false,
    success:
      function(data){
          alert(data);
          }
     // $("#form_message").html(data.message).css({'background-color' : data.bg_color}).fadeIn('slow');


});
}
#2

[eluser]joelkallman[/eluser]
You have a semicolon after password in your ajax.

Code:
function getData(){

  var username=$("#username").val();
  var password=$("#password").val();  

  $.ajax({
      type: "POST",
      url: "http://localhost/CodeIgniter/index.php/ajax_post/post_action",
      dataType: "json",
      data: "username="+username+"&password;="+password,
      cache:false,
      success:
        function(data){
            alert(data);
            }
       // $("#form_message").html(data.message).css({'background-color' : data.bg_color}).fadeIn('slow');


  });
}

Try that section without the semicolon. It will be rejected by CodeIgniter's filter.
#3

[eluser]CroNiX[/eluser]
You can also just use a json object for your data portion, and it will create the appropriate query string:

Code:
data: {username: username, password: password}
#4

[eluser]LuckyFella73[/eluser]
Not sure if you just cropped your code - I can't see the part
where you render the output ($output)

You need something like:
Code:
// more code here

$output = '{ "message": "'.$message.'", "bg_color": "'.$bg_color.'" }';
die($output);

To reduce errors it's better to use the json_encode function by the way.

Code:
$response_data = array(
   'message' => $message,
   'bg_color' => $bg_color
  );
  $json_response = json_encode($response_data);
  die($json_response);




Theme © iAndrew 2016 - Forum software by © MyBB