Welcome Guest, Not a member yet? Register   Sign In
Need Quick Ajax Help
#1

[eluser]RMinor[/eluser]
Hi, I have a quick question regarding some AJAX. I have a signup form that asks for a zip code and then upon the user typing one in populates the city and state fields automatically from a database table. The request is being sent okay, but I am getting an undefined key in my model. The actual error in Firebug is undefined indexes for city_name and city_state in my model. What am I doing wrong in that I am getting no data back?

Controller
Code:
public function getCity()
{
$zip_code = $this->input->post('input_zip');
$city = $this->Profile_model->getCity($zip_code);
return $city;
}

public function getState()
{
$zip_code = $this->input->post('input_zip');
$state = $this->Profile_model->getState($zip_code);
return $state;
}

Model
Code:
/**
* Method to retrieve a city name based on a zip code.
* @return string - the name of the city
*/
public function getCity($zip_code)
{
$query = $this->db->query("SELECT city_name FROM city WHERE city_zip = ?", array($zip_code));
$result = $query->row_array();
return $result['city_name'];
}

/**
* Method to retrieve a state based on a zip code.
* @return string - the abbreviation of the state
*/
public function getState($zip_code)
{
$query = $this->db->query("SELECT city_state FROM city WHERE city_zip = ?", array($zip_code));
$result = $query->row_array();
return $result['city_state'];
}

View
Code:
[removed]
$(document).ready(function() {
$("#zip_code").keyup(function() {
  var input_zip = $('#zip_code').val();
  var textlength = input_zip.length;
  if (textlength > 4) {
   // Call the getCity() method
   $.ajax({
    type: 'POST',
    url: '<?php echo base_url(); ?>signup/getCity',
    data: input_zip,
    success: function(output_city) {
     $("#city").val(output_city);
    },
   });
   // Call the getState() method
   $.ajax({
    type: 'POST',
    url: '<?php echo base_url(); ?>signup/getState',
    data: input_zip,
    success: function(output_state) {
     $("#state").val(output_state);
    },
   });
  }
});
});
[removed]
#2

[eluser]InsiteFX[/eluser]
Code:
/**
* Method to retrieve a city name based on a zip code.
* @return string - the name of the city
*/
public function getCity($zip_code)
{
$sql = "SELECT city_name FROM city WHERE city_zip = ?";
$query = $this->db->query($sql, array($zip_code));
$result = $query->row_array();
return $result['city_name'];
}

/**
* Method to retrieve a state based on a zip code.
* @return string - the abbreviation of the state
*/
public function getState($zip_code)
{
$sql = "SELECT city_state FROM city WHERE city_zip = ?";
$query = $this->db->query($sql, array($zip_code));
$result = $query->row_array();
return $result['city_state'];
}
#3

[eluser]RMinor[/eluser]
I see that is a different way to write the query that I already have. I tried it and I still got the exact same result.
#4

[eluser]RMinor[/eluser]
Anybody have any ideas on how to fix this?
#5

[eluser]CroNiX[/eluser]
With ajax, you need to echo out the response. Returning it does nothing as you aren't returning php to a php function.

Also, you are sending the data via POST, so in your controller, you need to get the value that you are going to be searching for via POST.

The javascript...
Code:
data: {zip: input_zip},  //set the zip post variable

Then in your
Code:
public function getCity()
{
$zip_code = $this->input->post('zip', TRUE); //get the zip from post
$sql = "SELECT city_name FROM city WHERE city_zip = ?";
$query = $this->db->query($sql, array($zip_code));
$result = $query->row_array();
echo $result['city_name'];  //echo, not return
}
#6

[eluser]RMinor[/eluser]
[quote author="CroNiX" date="1337569002"]With ajax, you need to echo out the response. Returning it does nothing as you aren't returning php to a php function.

Also, you are sending the data via POST, so in your controller, you need to get the value that you are going to be searching for via POST.

The javascript...
Code:
data: {zip: input_zip},  //set the zip post variable

Then in your
Code:
public function getCity()
{
$zip_code = $this->input->post('zip', TRUE); //get the zip from post
$sql = "SELECT city_name FROM city WHERE city_zip = ?";
$query = $this->db->query($sql, array($zip_code));
$result = $query->row_array();
echo $result['city_name'];  //echo, not return
}
[/quote]


I can't believe I overlooked that. Such a dumb error on my part. Thank you for your reply.




Theme © iAndrew 2016 - Forum software by © MyBB