简体   繁体   English

在输入框中从数据库插入值

[英]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); 我是否只需要使som变量而不是for循环,然后为每个输入框插入类似的内容:$(“ 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. 数据库位于服务器中,而Jquery / Javascript位于客户端中。 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. 1-使用.php生成html时,检索数据库信息并将其与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. 2-在页面加载时触发Jquery事件,然后对.php进行AJAX请求,以检索并返回所需的数据,然后通过Jquery将其插入HTML。

$.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. 如果您需要一次检索多个值,通常最好以JSON格式返回它们。

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. 如果您想在数据库中用HTML删除内容时从数据库中删除某些内容,则大致相同:当您从HTML中删除内容时,请调用AJAX请求到php,该php从数据库中删除信息,并将ID传递为PHP的变量。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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