fishbaitfood Posted August 15, 2011 Share Posted August 15, 2011 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; } } } ?> Quote Link to comment Share on other sites More sharing options...
fishbaitfood Posted August 15, 2011 Author Share Posted August 15, 2011 As far as I know, the problem is with the jQuery code. I just need to know how I can get all the different php functions, instead of 'output'. And as simple as just putting the function name, doesn't work. Quote Link to comment Share on other sites More sharing options...
fishbaitfood Posted August 15, 2011 Author Share Posted August 15, 2011 Anyone? I can't find anything about it the way I want to do it. And it's probably really easy to accomplish. Thanks. Quote Link to comment Share on other sites More sharing options...
nogray Posted August 15, 2011 Share Posted August 15, 2011 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. Quote Link to comment Share on other sites More sharing options...
fishbaitfood Posted August 16, 2011 Author Share Posted August 16, 2011 Hey, Thanks for the tip! But could you please give me a clear example of the javascript workaround using json and a parse function? Can't find it for use in my context. EDIT: I think I've found something! Again, thanks for the tip! Quote Link to comment 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.