选择下拉菜单项时执行mysql select语句

时间:2015-08-28 19:00:58

标签: php html mysql

我正在开发一个Web应用程序,我遇到了问题。 我有两个下拉列表,一个是从我的数据库动态填充,而另一个是静态HTML。

当用户从动态下拉列表中选择一个项目并从静态html下拉列表中选择一个项目,然后单击表单上的提交时,我希望动态选择的列表项打印出整个选定的行db。

这是我的代码示例

 XmlReader reader = XmlReader.Create("c:\\example\\extract");
 XElement el = XElement.Load(reader);
 reader.Close();

 var items = el.Elements("values").Elements("values").DescendantNodes();

 items = from item in el.Elements("values").Elements("values").Descendants()
 where item.Attribute("name").Value == "name"
 select item.FirstNode;

 foreach(XNode node in items) {
     Console.WriteLine(node.ToString());
 }

1 个答案:

答案 0 :(得分:0)

也许这就是你想要的,尽管你必须使用JQuery

<强> HTML

<form id="when_user_clicks_submit_button">
<input class="data_to_send" type="text" value="MySQL Selection data"></input>   
<input type="submit" value="Submit"></input>
</form>

JavaScript |用于Ajax请求的JQuery:

document.getElementById('when_user_clicks_submit_button').onsubmit = function() {
$.ajax({
type: "POST",
url: "php_code_script.php",//php script location
data: { "data_from_form": data_to_send }, //data to send in JSON format
success: function(data) { //callback if the script execution is successful
$('<p>' + data + '</p>').appendTo('body');//this returns the selected information from the MySQL database and inserts it somewhere, make sure to echo or print the selected data inside the php script.
}
)}
};

<强> PHP

    if($_POST['data_from_form']) {
    if ($contactc =="Yes"){
$selectsector = $_POST['data_from_form']; //gets the sended data from the form page to use for selection in the MySQL database

    $result = mysqli_query("SELECT * FROM company_details where comid = '$selectsector'");

    while ($row = mysqli_fetch_array($result)){
    echo $row["fname"]." ".$row["comname"]."<br/>";
    }
    }
    }

也许这就是你想要的,你基本上发送你想用来从数据库中选择的信息,然后php脚本处理它并从数据库中回应所选信息,然后数据返回到表单页面它变为DOM元素(HTML标记)的值。这只是为了举例说明你可能想要什么,我没有考虑安全性或任何事情(我对此还不太了解)。注意我还将mysql更改为mysqli,使其与更新版本的PHP更兼容,因为较新版本或将来不再支持mysql。