MySQL Query with PHP and Send Data to JavaScript -
edit:
i have number input id on it. in mysql database, have table ids , list of options separated commas id.
i'll take 1 of database records example: id "ham_and_cheese" , options "mayonnaise, salad cream, chutney, no dressing". person selects 1 on number input id "ham_and_cheese".
then, 1 drop down appear options: "mayonnaise, salad cream, chutney, no dressing" (each own option). if choose 2, 2 dropdowns appear, 3 3 appear, etc.
i new ajax great if me out.
my new code:
... <td><input type='number' min='0' max='5' name='ham_and_cheese_amount' /></td> ... <td id='ham_and_cheese_dressing'></td> ... function changedropdown(name, amount){ //gets id. var newname = name.replace("_amount", ""); //gets div drop down go in. var el2 = document.getelementbyid(newname + "_dressing"); //if number 0 selected in number input. if(amount == 0){ el2.innerhtml = ""; } //if number 1 selected in number input. if(amount == 1){ var html = "<select>"; var id = newname; $.ajax({ url: 'updatedropdown.php', datatype: 'json', data: {'option_id':id}, type: 'get', success: function(r){ for(i = 0; < r.length; i++){ // use r[i].tolowercase().replace(" ", "_") convert "salad cream" id "salad_cream", etc. html += "<option value='" + r[i].tolowercase().replace(" ", "_") + "'>" + r[i] + "</option>"; } html += "</select>"; el2.innerhtml = html; } }); } }
my updatedropdown.php:
<?php include "/home/pi/config.php"; $id = $_get['option_id']; //i know connect works because use same code other queries. $connect = mysqli_connect("localhost", $name, $pass, "items"); if(mysqli_connect_errno()){ return "failed connect products database!"; } $result = mysqli_query($connect, "select options products id='$id' limit 1"); $resultarray = explode(", ", mysqli_fetch_row($result)[0]); return json_encode($resultarray); ?>
ajax how information between back-end , front-end.
$.ajax({ url: "/url/of/php/page/that/echos/json/results", data: "post variables send", type: "post", error: function(xmlhttprequest, textstatus, errorthrown) { alert("an error has occurred making request: " + errorthrown); }, success: function(){ //do stuff here } });
just build php page echos out results of query json_encoded, nothing else sent ui.
Comments
Post a Comment