简体   繁体   中英

Insert values from database in inputbox

How can I change this code to insert values from the database in some inputboxes on the webpage? I also need to add some dynamic text on the page if no results where found.

Do I only need to make som variables instead of the for loop and then insert something like this for every inputbox: $("input").val(j[i].optionValue);

$(function(){
   $("select#ctlJob").change(function(){
   $.getJSON("select.php",{id: $(this).val()}, function(j){
   var options = '';

   for (var i = 0; i < j.length; i++) {
        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
   }

   $("#ctlPerson").html(options);
   $('#ctlPerson option:first').attr('selected', 'selected');
   })
 })         
})

Like this:

<div>
<label>number</label>
<input type="text" id="ctlJob"  />
<input type="submit" value="Get info from table and insert in fields" name="submit" />
</div>

<div>
<label>field1</label>
<input type="text"name="field1" maxlength="5" class="input" />
</div>

<div>
<label>Field2</label>
<input type="text"name="field2" maxlength="5" class="input" />
</div>


<label>&nbsp;</label>
<input type="submit" value="Send" name="submit" />

the Database is in the server and Jquery/Javascript is in the client side. So the only way you can do that is:

1- When generating the html using a .php, retrieve the database info and mix it with your HTML.

2- Trigger a event of Jquery when your page loads, then do a AJAX petition to a .php that retrieves and returns the data you need, then insert it into your HTML through Jquery.

$.post('get_db_info.php', function(data) {
   $("#input_where_data_goes").val(data);
});

If you need to retrieve multiple values in one go, returning them in JSON format is usually the best.

If you want to delete something from the DB when you delete it in your HTML it's more or less the same: when you delete it from the HTML call a AJAX petition to a php that deletes the info from the DB, passing a ID as a variable to the PHP.

$.post('delete_db_info.php', {deleted_id: deleted_id},function(data) {
   alert("DATA: "+deleted_id+" DELETED!");
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM