如何使用隐藏输入发送选择的值 [英] how to send the value of the select with the hidden input
本文介绍了如何使用隐藏输入发送选择的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在尝试通过隐藏输入发送所选选项的值时被卡住,以便在 mysql 表中插入值:
I'm being stuck while trying to send the value of the selected option by the hidden input, in order to insert the values in the mysql table:
<form action="interface.php" method="post">
VLAN: <select name="vlan" onchange="document.getElementById('vlan_id').value=this.options[this.options.selectedIndex].value;">
<option value="0">Выберите оборудование</option>
<?php
$res = mysql_query('select `vlan_id`,`number` from `vlan`');
while($row = mysql_fetch_assoc($res)){
printf("<option value='%s'>%s</option>", $row['vlan_id'], $row['number']);
}
?>
</select>
<input type="hidden" id="vlan_id name="vlan_id" value="vlan_id" />
界面.php
$val3 = $_POST['vlan'];
$val4 = $_POST['vlan_id'];
$result_insert = mysql_query ("INSERT INTO `ADRESSA`.`interface` ( `vlan`,`vlan_id`) VALUES ('$val3','$val4');")
or die ("<b>Query failed:</b> " . mysql_error());
最好的问候!
推荐答案
您在隐藏 id attr 上缺少引用,请尝试更改
you are missing a quote on hidden id attr try to change
<input type="hidden" id="vlan_id name="vlan_id" value="vlan_id" />
到
<input type="hidden" id="vlan_id" name="vlan_id" value="vlan_id" />
注意 :- mysql_*
已被弃用,使用 mysqli_*
或 PDO
Note :- mysql_*
has been deprecated use mysqli_*
or PDO
还可以通过转义字符串来防止 sql 注入,例如:-
Also prevent sql injection by escaping strings like:-
$val3 = mysql_real_escape_string($_POST['vlan']);
$val4 = mysql_real_escape_string($_POST['vlan_id']);
对于获取值和 id 更改:-
For get value and id change:-
onchange="document.getElementById('vlan_id').value=this.options[this.options.selectedIndex].text;"
你会得到:- Array ( [vlan] => 1 [vlan_id] => ss )
也会根据值改变查询
you will get :- Array ( [vlan] => 1 [vlan_id] => ss )
also change query according values
这篇关于如何使用隐藏输入发送选择的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文