[英]How to insert Bootstrap drop-down values in database
i am using bootstrap for making a user form. 我正在使用引导程序制作用户表单。 Now i need to insert selected drop-down value in database. 现在,我需要在数据库中插入选定的下拉值。 But i am facing two problems. 但是我面临两个问题。
1) When i select any item from drop-down, it jumps to top of my screen in url because of href="#". 1)当我从下拉列表中选择任何项目时,由于href =“#”,它会跳到url中屏幕的顶部。 When i removed "#", it started the page refresh on selection item. 当我删除“#”时,它开始在选择项上刷新页面。
2) How i target my drop-down list to insert any selected value in database using php. 2)我如何针对我的下拉列表,以使用PHP在数据库中插入任何选定的值。
My code is 我的代码是
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">Select City
<span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a href="#">Lahore</a></li>
<li><a href="#">Islamabad</a></li>
<li><a href="#">karachi</a></li>
</ul>
</div>
javascript code JavaScript代码
$(document).ready(function() {
$(".dropdown-menu li a").click(function(){
$(this).parents(".dropdown").find('.btn').html($(this).text() + ' <span class="caret"></span>');
$(this).parents(".dropdown").find('.btn').val($(this).data('value'));
});
For the point 1 : Since you have the #
in the href the page scrolls up (ie navigating to the link target). 对于要点1 :由于href中有#
,因此页面会向上滚动(即导航到链接目标)。 You can prevent this event by adding event.preventDefault()
in your code. 您可以通过在代码中添加event.preventDefault()
来防止此事件。
For Point 2 : Since you said if you are able to get the value to your server side php code then you can proceed further you can make use of AJAX Post method . 对于第2点 :由于您说过如果您能够将值获取到服务器端php代码,则可以继续进行操作,可以使用AJAX Post方法 。 The final code would be 最终的代码是
$(document).ready(function() {
$(".dropdown-menu li a").click(function(e){
e.preventDefault(); //this is the line which prevents the event
$(this).parents(".dropdown").find('.btn').html($(this).text() + ' <span class="caret"></span>');
$(this).parents(".dropdown").find('.btn').val($(this).data('value'));
//this is the code to post the value to server
$.post("YourPHPMethod.php",{name: $(this).text()} ,function(response){
alert("Response from server: "+response);
});
});
You have to post it to a php. 您必须将其发布到php。 I do it with ajax like this. 我用这样的ajax来做。
Look: http://www.codesheet.org/codesheet/wOHDXFju 外观: http : //www.codesheet.org/codesheet/wOHDXFju
function postContent(cv) {
$("#myDiv").html('L O A D I N G ...').show();
var url = "/post.php";
$.post(url, {id: cv} ,function(data) { //contentVar is the variable in post.php
$("#myDiv").html(data).show();
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.