fishbaitfood Posted November 2, 2011 Share Posted November 2, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/250335-uncaught-typeerror-cannot-read-property-address-of-null/ Share on other sites More sharing options...
trq Posted November 3, 2011 Share Posted November 3, 2011 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/250335-uncaught-typeerror-cannot-read-property-address-of-null/#findComment-1284464 Share on other sites More sharing options...
fishbaitfood Posted November 3, 2011 Author Share Posted November 3, 2011 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( "Broadway" ["city"]=> string( "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. Quote Link to comment https://forums.phpfreaks.com/topic/250335-uncaught-typeerror-cannot-read-property-address-of-null/#findComment-1284577 Share on other sites More sharing options...
trq Posted November 3, 2011 Share Posted November 3, 2011 Firebug? Quote Link to comment https://forums.phpfreaks.com/topic/250335-uncaught-typeerror-cannot-read-property-address-of-null/#findComment-1284581 Share on other sites More sharing options...
fishbaitfood Posted November 3, 2011 Author Share Posted November 3, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/250335-uncaught-typeerror-cannot-read-property-address-of-null/#findComment-1284588 Share on other sites More sharing options...
trq Posted November 3, 2011 Share Posted November 3, 2011 What version of php are you using and is your data in UTF-8? Quote Link to comment https://forums.phpfreaks.com/topic/250335-uncaught-typeerror-cannot-read-property-address-of-null/#findComment-1284595 Share on other sites More sharing options...
fishbaitfood Posted November 3, 2011 Author Share Posted November 3, 2011 PHP version: 5.1.2 MySQL Character set: UTF-8 Unicode (utf8) And my database collation is also set to 'utf8_general_ci'. Quote Link to comment https://forums.phpfreaks.com/topic/250335-uncaught-typeerror-cannot-read-property-address-of-null/#findComment-1284600 Share on other sites More sharing options...
trq Posted November 3, 2011 Share Posted November 3, 2011 Sorry, but I really don't see why there would be an issue. Quote Link to comment https://forums.phpfreaks.com/topic/250335-uncaught-typeerror-cannot-read-property-address-of-null/#findComment-1284602 Share on other sites More sharing options...
fishbaitfood Posted November 3, 2011 Author Share Posted November 3, 2011 Exactly.. that's why I'm here. It works perfectly on localhost. But somehow, on my domain, json_encode removes my data or something. Quote Link to comment https://forums.phpfreaks.com/topic/250335-uncaught-typeerror-cannot-read-property-address-of-null/#findComment-1284608 Share on other sites More sharing options...
fishbaitfood Posted November 3, 2011 Author Share Posted November 3, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/250335-uncaught-typeerror-cannot-read-property-address-of-null/#findComment-1284615 Share on other sites More sharing options...
nogray Posted November 3, 2011 Share Posted November 3, 2011 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() Quote Link to comment https://forums.phpfreaks.com/topic/250335-uncaught-typeerror-cannot-read-property-address-of-null/#findComment-1284752 Share on other sites More sharing options...
fishbaitfood Posted November 3, 2011 Author Share Posted November 3, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/250335-uncaught-typeerror-cannot-read-property-address-of-null/#findComment-1284761 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.