我如何添加下拉列表选定值到数据库?
你好我是PHP和ajax的新手。选择下拉列表后,单独的div功能将出现,我必须插入这两个值。我的意思是选择的选项,也是在单独的div输入数据。我的英语不好。请帮助我的朋友。 预先感谢您我如何添加下拉列表选定值到数据库?
<p><label>Affiliate Type</label></p>
<p><select class="inp authInput" id="id_affiliate_type" name="affiliate_type">
<option value="" selected="selected" disabled="disabled" style="display: none;">Select</option>
<option value="I">Individual</option>
<option value="O">Organisation</option>
<div id="errorselect"></div>
</select></p>
<b button type="submit" class="btn btn-primary" onclick="return sendaccountData();" id="updateaccount" name="updateaccount">Update Account Information</b></button>
如何将此存储使用AJAX和PHP的phpmyadmin数据库..
这是非常简单的。您可以进行ajax调用,并在调用成功时获取该值并将其插入该DIV。
您将需要1个PHP文件,只是为了检查数据库,并给予输出,例如:在你的页面select_org_type.php
<?php
if(!empty($_GET['affiliate_type'])){
//use mysqli commands to connect to database and fetch result and store in $result.
header();//This is very important, it will set your output as pure JSON object and javascript will find it easy to integrate
echo json_encode($result);
}
现在,这里:select_org_type.php
例,例如你正在使用jQuery(我建议很容易实现ajax调用)
<script type="text/javascript">
var affiliate_type = $('#id_affiliate_type').val();
$('#updateaccount').on('click',function(){
$.get('select_org_type.php',{affiliate_type:affiliate_type},function(data){
$('#errorselect').text(data['column-name']);
});
});
</script>
这是我使ajax调用来填充任何选择选项或将结果赋予任何div。
您需要实现一个JavaScript侦听器,以在选择选项时作出反应。然后在这个监听器中,您需要发送一个ajax请求到您的服务器,并将所选的选项值作为post/get变量。在你的服务器上,你需要实现相应的php文件来处理ajax请求。这个php文件必须得到post/get变量(选中的选项值)并将其插入到你的SQL数据库中。这是一般的想法。
假设你正在使用jQuery,听者必须是一个JavaScript文件在您的网页,应该是这样的:
// react when the select list changes
$('#id_affiliate_type').on('change', function(e) {
// get the selected option value
var selected = $(this).find(':selected').text(); // or val(), need to confirm this...
// send the ajax request to your server
$.ajax({
// url of your php script which insert data into database
url: 'your_php_file.php',
type: 'get' // or 'post' if you prefer
// variables passed to the script
data: {selected : selected},
// callback, request is successful
success: function(result) {
console.log('success ' + result);
// do whatever you need to do after the call
}
// callback, something wrong happened
error: function(result) {
console.log('error ' + result);
// do whatever you need to do after the call
}
// process completed (success or error)
complete: function(result) {
}
}
}
在你的PHP文件(your_php_file.php),您可以访问“选择”变量通过呼吁:
$selected = $_GET['selected'] // (or $_POST)
...然后用它来根据需要修改您的数据库。
这是一般的想法,上面的代码只是一般的设计,不是'按原样'使用,而是作为一种灵感。
您可以在SO和其他地方找到很多关于在选择列表上实现on('change')侦听器的示例,通过变量向服务器发送ajax请求并从这些变量更新PHP中的数据库。
好运
先生,我有两个选项,像“组织”和“个人”,onclicking组织“公司”将出现,并输入公司输入框中的数据,我必须在数据库中更新它。我已经完成了事件调用,正如你在代码中提到的那样。我已经做了验证,以便用户不应该继续而不选择该选项。但事情是当我选择INDIVIDUAL选项,并点击更新功能,它不允许我在phpmyadmin中更新 –
我们需要看到更多的代码(服务器和客户端)和更精确的错误消息来帮助你。 – Flo
'$(文件)。在( '准备好',函数(){\t \t \t \t \t \t \t $( “#id_affiliate_type”)。变化(函数(){\t \t \t \t \t \t \t VAR selected_option = $( '#id_affiliate_type')。val(); if(selected_option ==='O'){$('#id_affiliate_type2')。attr('pk','I')。show(); } if(selected_option ('#id_affiliate_type2')。attr('pk','O')。show(); }})$('#id_affiliate_type')。on('change',function( ){ if(this.value =='O') \t \t \t \t \t \t \t \t // .....................^....... \t \t \t \t \t \t \t \t { \t \t \t \t \t \t \t \t \t $(“#detailcompany”)。show(); \t \t \t \t \t \t \t \t} \t \t \t \t \t \t \t \t其他 \t \t \t \t \t \t \t \t { \t \t \t \t \t \t \t \t \t $(“#detailcompany”)。hide(); \t \t \t \t \t \t \t \t} \t \t \t \t \t \t \t \t}); ' –
file1.php
<html>
<select class="opt">
<option value="option1">option1</option>
<option value="option2">option2</option>
</select>
<div id="msg">
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(".opt").change(function(){
var data1 = $(".opt").val();
$.ajax({
type:"GET",
cache:false,
url:"file2.php",
data : { foo:data1}, // multiple data sent using ajax
success: function (html) {
$('#msg').html(html);
}
});
});
</script>
</html>
file2.php
<?php
$data=$_GET['foo'];
if($data=="option1")
{
echo "selected value is option1"; // add sql operation here if option 1
}
else if($data=="option2")
{
echo "selected value is option2"; // add sql operation here if option2
}
?>
您可以根据下拉file2中选择显示的数据。php,
先生,我也正在使网络聊天盒电子商务的客户支持,你可以建议我的想法..? –
你可以尝试一下自己,如果需要纠错,我可以帮你 – vipz
你好先生,是否有可能没有json ..?在整个代码中,我已经使用ajax和php ..你说我已经创建了单独的php文件,我已经编写代码来连接mysql并插入查询来插入其他文件的数据, –
是的,Ashish也可以没有json,你可以直接在任何DIV(Dom元素)中插入html代码。你的输出应该是html,不要把header作为application/json。 –