Jump to content


Photo

Uncaught TypeError: Cannot read property 'address' of null


  • Please log in to reply
11 replies to this topic

#1 fishbaitfood

fishbaitfood

    Advanced Member

  • Members
  • PipPipPip
  • 97 posts

Posted 02 November 2011 - 06:40 PM

Hello all,

I'm currently finding myself in the situation where my code on localhost works perfectly, but not on my domain.

What my code is about:
- enter a customer name in a textfield
- (for selecting a customer name: autocomplete script which gathers the customer names from a database through a file called "customers.php")
- jquery $.ajax customer name submit, to file "get-customer-data.php" which json_encode outputs additional data of the entered customer
- success in $.ajax to fill other textfields with the json data

And this WORKS on my localhost perfectly!
But on my domain I get the error in this topic title.


So far, I've figured out that the json_encode function won't output anything on my domain.
So I guess $.ajax is receiving that "null" value, right?


This is the php file that outputs json_encode additional data, to be received by $.ajax:

<?php
		
		if (strlen($_POST['customer']) > 0) {
			
			$customer = $_POST['customer'];
		
			$select_customer_data = "SELECT address, city, country FROM customers WHERE customer = '$customer';";
			
			include_once('connection.inc.php');
			$con = mysql_connect(MYSQL_SERVER, MYSQL_USERNAME, MYSQL_PASSWORD) or die ("Connection failed: " . mysql_error());
			$dbname = "mydatabase";
			$select_db = mysql_select_db($dbname, $con) or die ("Can't open database: " . mysql_error());
			
			$get_customer_data = mysql_query($select_customer_data) or die ("Query failed: " . mysql_error());
			$customer_data = mysql_fetch_array($get_customer_data);
			
			$customer_address = $customer_data['address'];
			$customer_city = $customer_data['city'];
			$customer_country = $customer_data['country'];
			
			$output =  array('address' => $customer_address, 'city' => $customer_city, 'country' => $customer_country);
			
			echo json_encode($output);
			
		}
	
?>


And the webpage:

<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<!--[if lt IE 9]
			<script src="http://html5shiv.googlecode.com/svn/divunk/html5.js"></script>
		<![endif]-->
		<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
		<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
		<script type="text/javascript" src="js/autocomplete.js"></script>
		<title></title>

		<script type="text/javascript">
			$(document).ready(function(){
				
				$("#input_customer").autocomplete("customers.php", { minChars:1, width:186 });
				
				$(".btn-submit").click(function() {
					var customer = $("#input_customer").val();
					var dataString = "customer="+customer;
					$.ajax({
						type: "POST",
						url: "get-customer-data.php",
						data: dataString,
						dataType: "json",
						success: function(data) {
							$(".input-address").val(data.address);
							$(".input-city").val(data.city);
							$(".input-country").val(data.country);
						}
					});
				});
				
			});
		</script>
	</head>
	
	<body>
	
		<div class="fact-to">
			<h3 class="fact-to-heading">Customer data</h3>
			<div class="fact-to-contact-box">
				<input type="text" id="input_customer" name="customer" />
				<input type="image" src="images/btn-submit.png" class="btn-submit" title="Get customer data" />
				<input type="text" class="input-address" /><br />
				<input type="text" class="input-city" /><br />
				<input type="text" class="input-country" />
			</div>
		</div>

	</body>
</html>


ps: when using $.ajax, it isn't necessary to use form tags, right? (besides validation)
Don't think that's causing the javascript error.

Why won't the exact same thing work on my domain, when it does on localhost?


#2 trq

trq

    Advanced Member

  • Administrators
  • 30,968 posts
  • LocationSydney, Australia.

Posted 02 November 2011 - 08:30 PM

You really should be setting the responses content type to json before echoing it back.

header('Content-type: application/json');

ps: when using $.ajax, it isn't necessary to use form tags, right?


The $ajax function doesn't care where it's data comes from.

As for your issue. Have you done any debugging? What does a var_dump of $output produce? What does firebug have to say about what is in data ?

http://thorpesystems.com | http://proemframework.org | http://github.com/trq

SmtpCatcher - A very simple mock sendmail useful for testing PHP mail scripts.
OPM - My Linux package manager.


#3 fishbaitfood

fishbaitfood

    Advanced Member

  • Members
  • PipPipPip
  • 97 posts

Posted 03 November 2011 - 06:07 AM

The data is present, but json_encode won't work.
Adding the header information won't help either.


This is what I get when I var_dump $output:

array(3) {
  ["address"]=>
  string(8) "Broadway"
  ["city"]=>
  string(8) "New York"
  ["country"]=>
  string(13) "United States"
}


On localhost json_encode outputs {"address":"Broadway", "city":"New York", "country":"United States"}.
But on my domain I just get a blank page when testing this.


#4 trq

trq

    Advanced Member

  • Administrators
  • 30,968 posts
  • LocationSydney, Australia.

Posted 03 November 2011 - 06:37 AM

Firebug?

http://thorpesystems.com | http://proemframework.org | http://github.com/trq

SmtpCatcher - A very simple mock sendmail useful for testing PHP mail scripts.
OPM - My Linux package manager.


#5 fishbaitfood

fishbaitfood

    Advanced Member

  • Members
  • PipPipPip
  • 97 posts

Posted 03 November 2011 - 06:53 AM

With Firebug I get about the same error.

Posting the customer to "get-customer-data.php" is OK.

But with $.ajax success function, my $(".input-address").val(data.address); seems to be null.

Firebug states:
Data is null.
$(".input-address").val(data.address);


So from what I understand.. The array is present, so there IS data, but when json_encode comes in, $.ajax won't receive the data.


#6 trq

trq

    Advanced Member

  • Administrators
  • 30,968 posts
  • LocationSydney, Australia.

Posted 03 November 2011 - 07:07 AM

What version of php are you using and is your data in UTF-8?

http://thorpesystems.com | http://proemframework.org | http://github.com/trq

SmtpCatcher - A very simple mock sendmail useful for testing PHP mail scripts.
OPM - My Linux package manager.


#7 fishbaitfood

fishbaitfood

    Advanced Member

  • Members
  • PipPipPip
  • 97 posts

Posted 03 November 2011 - 07:21 AM

PHP version: 5.1.2

MySQL Character set: UTF-8 Unicode (utf8)

And my database collation is also set to 'utf8_general_ci'.

#8 trq

trq

    Advanced Member

  • Administrators
  • 30,968 posts
  • LocationSydney, Australia.

Posted 03 November 2011 - 07:24 AM

Sorry, but I really don't see why there would be an issue.

http://thorpesystems.com | http://proemframework.org | http://github.com/trq

SmtpCatcher - A very simple mock sendmail useful for testing PHP mail scripts.
OPM - My Linux package manager.


#9 fishbaitfood

fishbaitfood

    Advanced Member

  • Members
  • PipPipPip
  • 97 posts

Posted 03 November 2011 - 07:30 AM

Exactly.. that's why I'm here.  :D
It works perfectly on localhost.
But somehow, on my domain, json_encode removes my data or something.  :shrug:

#10 fishbaitfood

fishbaitfood

    Advanced Member

  • Members
  • PipPipPip
  • 97 posts

Posted 03 November 2011 - 07:44 AM

echo $output["address"];

Echoing out the different data works fine.

It's just so weird I don't get an output when echoing json_encode($output);

#11 nogray

nogray

    Advanced Member

  • Members
  • PipPipPip
  • 930 posts
  • LocationSan Francisco CA

Posted 03 November 2011 - 03:10 PM

So hosting companies don't provide json_encode() in php (even if they offer php 5.2+). I know it should be installed by default, but that's not the case all the times.

Check if json_encode is avaliable using function_exists()

NoGray.com


#12 fishbaitfood

fishbaitfood

    Advanced Member

  • Members
  • PipPipPip
  • 97 posts

Posted 03 November 2011 - 03:38 PM

Wow, another great, useful function I did not know about! Thanks!

And damnit! It isn't available on my domain.

So I guess I shall contact my domain's hosting service...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com