Jump to content

Recommended Posts

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?

 

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 ?

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.

 

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.

 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.