Jump to content

jQuery get multiple outputs from PHP file


fishbaitfood

Recommended Posts

Hi all,

 

I'm using jQuery to retrieve aditional data from a database, when a name is selected from the dropdown box, without page refresh.

Now my problem is, that I don't know how to get multiple 'outputs' generated by the php file, which processes this.

 

So, in short, I want multiple outputs in different form fields.

 

I guess it's something very simple, but I can't get it to work.

 

 

What I have so far, but isn't working:

 

jQuery + html

<script type="text/javascript">
function get(){
	$.post('data.php', { name: form.name.value },
		function(tel){
			$("#tel").val(output);
			$("#email").val(output);
			$("#adres").html(output);
			$("#adres").append(output);
			$("#adres").append(output);
			$("#adres").append(output);
		}
		function(email){
			$("#email").val(output);
		}
		function(street){
			$("#adres").html(output);
		}
		function(postcode){
			$("#adres").append(output);
		}
		function(city){
			$("#adres").append(output);
		}
		function(country){
			$("#adres").append(output);
		}
	);
}
</script>

<form name="form">
<select name="name">
	<option></option>
	<?php
		while($row = mysql_fetch_array($result)){
			echo '<option>'.$row['name'].'</option>';
		}
	?>
</select>

<input id="tel" type="text" name="tel" />
<input id="email" type="text" name="email" />
<textarea id="adres" name="adres"></textarea>

<input type="button" value="Autofill" onClick="get();" />
<input type="submit" name="add" value="Add" />
</form>

 

 

data.php

<?php
include_once('verbinding.inc.php');
$con = mysql_connect(MYSQL_SERVER, MYSQL_USERNAME, MYSQL_PASSWORD) or die ("Verbinding mislukt: " . mysql_error());

$name = $_POST['name'];

mysql_select_db("cre8") or die ("Kon de database niet openen: " . mysql_error());

$nr = mysql_query("SELECT nr FROM clients WHERE name = '$name'");
$nr_num_rows = mysql_num_rows($nr);

$tel = mysql_query("SELECT tel_gsm FROM clients WHERE name = '$name'");
$tel_num_rows = mysql_num_rows($tel);
$email = mysql_query("SELECT email FROM clients WHERE name = '$name'");
$email_num_rows = mysql_num_rows($email);

$street = mysql_query("SELECT adres FROM clients WHERE name = '$name'");
$street_num_rows = mysql_num_rows($street);
$postcode = mysql_query("SELECT postcode FROM clients WHERE name = '$name'");
$postcode_num_rows = mysql_num_rows($postcode);
$city = mysql_query("SELECT city FROM clients WHERE name = '$name'");
$city_num_rows = mysql_num_rows($city);
$country = mysql_query("SELECT country FROM clients WHERE name = '$name'");
$country_num_rows = mysql_num_rows($country);

$btw = mysql_query("SELECT btw FROM clients WHERE name = '$name'");
$btw_num_rows = mysql_num_rows($btw);

function tel(){
	if($name==NULL)
		echo '';
	else
	{
		if($tel_num_rows==0)
			echo '';
		else 
		{
			$tel = mysql_result($tel, 0);
			echo $tel;
		}
	}
}
function email(){
	if($name==NULL)
		echo '';
	else
	{
		if($email_num_rows==0)
			echo '';
		else 
		{
			$email = mysql_result($email, 0);
			echo $email;
		}
	}
}
function street(){
	if($name==NULL)
		echo '';
	else
	{
		if($street_num_rows==0)
			echo '';
		else 
		{
			$street = mysql_result($street, 0);
			echo "$street<br />";
		}
	}
}
function postcode(){
	if($name==NULL)
		echo '';
	else
	{
		if($postcode_num_rows==0)
			echo '';
		else 
		{
			$postcode = mysql_result($postcode, 0);
			echo "$postcode ";
		}
	}
}
function city(){
	if($name==NULL)
		echo '';
	else
	{
		if($city_num_rows==0)
			echo '';
		else 
		{
			$city = mysql_result($city, 0);
			echo "$city<br />";
		}
	}
}
function country(){
	if($name==NULL)
		echo '';
	else
	{
		if($country_num_rows==0)
			echo '';
		else 
		{
			$country = mysql_result($country, 0);
			echo $country;
		}
	}
}
function btw(){
	if($name==NULL)
		echo '';
	else
	{
		if($btw_num_rows==0)
			echo '';
		else 
		{
			$btw = mysql_result($btw, 0);
			echo $btw;
		}
	}
}
?>

 

 

Link to comment
Share on other sites

Instead of using echo in your php, you would create an array with all the outpust - e.g. array('email'=>'my_email@domain.com', 'address'=>'my_adddress');

 

When you finish collecting the data, use json_encode to transfer the array into json and echo the json result.

 

In your javascript function, you would parse the json object (I think jquery has a parse function) and use that javascript object in the function.

Link to comment
Share on other sites

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.