简体   繁体   English

下拉选择而不单击提交按钮

[英]dropdown selection without clicking the submit button

I don't know where to start, if there is any similar question like mine I would be happy to get some idea! 我不知道从哪里开始,如果有像我这样的类似问题,我会很高兴得到一些想法! Basically the question says it all, Here is what I would like: 基本上问题就是这一切,这就是我想要的:

I have a simple dropdown menu. 我有一个简单的下拉菜单。 When a selection is made in that drop down menu, a php mysql query is ran where the database will be updated with that value. 当在该下拉菜单中进行选择时,将运行php mysql查询,其中将使用该值更新数据库。 I have all the pieces, all I need is the code that would be able to kick it all off. 我拥有所有的部分,我需要的是能够全力以赴的代码。

For instance when you hit submit on a form you would typically type out: 例如,当您在表单上点击提交时,通常会输入:

if (isset($_POST['submit']))
{

//grab information and insert into db

}

How would I do this for a drop down selection without having to click the submit button. 如何在不必单击提交按钮的情况下进行下拉选择。

Make an ajax call on drop down change like: 在下拉变化上进行ajax调用,如:

$('#dropdown').change(function(){
    // ajax call
});

Use jquery/javascript onchange event to perform any type of action on the drop down list. 使用jquery / javascript onchange事件在下拉列表中执行任何类型的操作。 use as follows: 用法如下:

$("#select_id").on('change',function(){
 $.post('code url',{paramenters to be stored},function(data){
 alert('inserted');
})
});

you should be looking for something like this 你应该找这样的东西

<form id="testform">
<select id="yourselect" name="yourselect" onChange="updateDb()">
 <option value="somevalue">Please select</option>
 <option value="somevalue1">Something</option>
</select>
</form>

you Javascript function will look like this 你的Javascript函数会是这样的

function updateDb() {
// I am using jquery for ajax
 $.post("yourserverhandle.php", $("#testform").serialize());
}

and this is how your "yourserverhandle.php" looks like 这就是你的“yourserverhandle.php”的样子

<?php
$query = "update yourtable set something='".mysql_escape_string($_POST["yourselect"])."' where id='something'";
.... mysql connect, execute
?>

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

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