使用php动态创建表单元素
这个简短的程序假设是查找表X的列名,并创建一个表单,其中至少包含一个文本字段和一个包含表中所有列名称的select元素。有了这些信息,用户可以在此表上执行搜索,并进一步指定要搜索的列。我希望用户能够添加更多带有匹配select元素的文本字段,以防他想要优化搜索 当用户按下按钮时,如何动态添加这些额外字段使用php动态创建表单元素,php,javascript,xhtml,Php,Javascript,Xhtml,这个简短的程序假设是查找表X的列名,并创建一个表单,其中至少包含一个文本字段和一个包含表中所有列名称的select元素。有了这些信息,用户可以在此表上执行搜索,并进一步指定要搜索的列。我希望用户能够添加更多带有匹配select元素的文本字段,以防他想要优化搜索 当用户按下按钮时,如何动态添加这些额外字段 <?php $table_name = "tablename"; mysql_connect("localhost", "root", ""); $query = "SELECT COLU
<?php
$table_name = "tablename";
mysql_connect("localhost", "root", "");
$query = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '$table_name'";
$result = mysql_query($query);
$column_names="";
while($row = mysql_fetch_array($result)){
$column_names == "" ? $column_names .= $row["COLUMN_NAME"] : $column_names .= "," . $row["COLUMN_NAME"] ;
}
$column_names = explode(",", $column_names);
?>
<html>
<head>
<title>SQLSEARCH</title>
</head>
<body>
<form action="index.php" method="post">
<?php
echo "Search: <input tpe=\"text\" name=\"advtext[]\" /> in ";
echo "<select name=\"advselect[]\">";
foreach($column_names as $value)
echo "<option>" . $value . "</option>";
echo "</select>";
?>
<input type="hidden" name="searchsent" value="1" />
<input type="submit" name="searchbutton" value="Search" />
</form>
<input type="button" name="addattributes" value="Add Search Attributes" / onclick="AddSelect();">
</body>
</html>
您可以对表单使用innerHTML属性。否则,使用Ajax功能添加额外的文本框以动态添加
您可以对表单使用innerHTML属性。否则,使用Ajax功能添加额外的文本框以动态添加
每次用户按下按钮时,此函数都会添加一个输入元素和一个选择元素
function AddSelect(){
var newInput = document.createElement('input');
newInput.type='text';
newInput.name = 'advtext[]';
<?php
foreach($column_names as $value => $i){
echo "\tvar newOption" . $value . "=document.createElement('option')" . "\n";
echo "\tnewOption" . $value . ".value='" . $i . "';" . "\n";
echo "\tnewOption" . $value . ".innerHTML='" . $i . "';" . "\n\n";
}
?>
var newSelect = document.createElement('select');
newSelect.name = 'advselect[]';
<?php
foreach($column_names as $value => $i){
echo "\tnewSelect.appendChild(newOption" . $value . ")" . "\n";
}
?>
var SubmitButton = document.forms.myform.searchbutton;
document.forms.myform.insertBefore(newInput, SubmitButton);
document.forms.myform.insertBefore(document.createTextNode(" in "), SubmitButton);
document.forms.myform.insertBefore(newSelect, SubmitButton);
}
函数AddSelect(){
var newInput=document.createElement('input');
newInput.type='text';
newInput.name='advtext[]';
每次用户按下按钮时,此函数都会添加一个输入元素和一个选择元素
function AddSelect(){
var newInput = document.createElement('input');
newInput.type='text';
newInput.name = 'advtext[]';
<?php
foreach($column_names as $value => $i){
echo "\tvar newOption" . $value . "=document.createElement('option')" . "\n";
echo "\tnewOption" . $value . ".value='" . $i . "';" . "\n";
echo "\tnewOption" . $value . ".innerHTML='" . $i . "';" . "\n\n";
}
?>
var newSelect = document.createElement('select');
newSelect.name = 'advselect[]';
<?php
foreach($column_names as $value => $i){
echo "\tnewSelect.appendChild(newOption" . $value . ")" . "\n";
}
?>
var SubmitButton = document.forms.myform.searchbutton;
document.forms.myform.insertBefore(newInput, SubmitButton);
document.forms.myform.insertBefore(document.createTextNode(" in "), SubmitButton);
document.forms.myform.insertBefore(newSelect, SubmitButton);
}
函数AddSelect(){
var newInput=document.createElement('input');
newInput.type='text';
newInput.name='advtext[]';