douzhe9075 2010-03-03 05:27
浏览 67
已采纳

使用php动态创建表单元素

This short program is suppose find the column names of a table X, and create a form with at least one text field and one select element that contains all the names of the columns of the table. With that information, the user can perform a search on this table and further specify on which column he would like to do the search. I would like it for the user to be able to add more text fields with matching select elements, just in case he wants to refine his search.

How can I dynamically add those extra fields when ever the user press a button?

<?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>
  • 写回答

2条回答 默认 最新

  • doushaqing7080 2010-03-03 06:42
    关注

    This function adds an input element and a select element, every time the user presses the button.

     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')" . "
    ";
            echo "\tnewOption" . $value . ".value='" . $i . "';" . "
    ";
            echo "\tnewOption" . $value . ".innerHTML='" . $i . "';" . "
    
    ";
        }
    ?>
    
        var newSelect = document.createElement('select');
        newSelect.name = 'advselect[]';
    
    <?php
        foreach($column_names as $value => $i){
            echo "\tnewSelect.appendChild(newOption" . $value . ")" . "
    ";
        }
    ?>
    
        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);
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题