【问题标题】:PHP\HTML dynamic dropdown listing with auto filed population from PostgreSQL databasePHP\HTML 动态下拉列表,带有来自 PostgreSQL 数据库的自动归档填充
【发布时间】:2012-10-11 20:02:07
【问题描述】:

我已经更新了这个脚本。脚本要求的动态下拉部分已通过本论坛优秀成员的贡献实现。现在我正在努力让表单上的一些字段在从下拉列表中选择项目时自动填充。例如,如果我从下拉列表中选择 pipeno 和 wallthickiness,我希望在记录中记录相应的 DJoint、pipeno2、Heatno1 和 heatno 2 元组,以便从数据库中自动填充表单中的各个字段 我希望自动填充的字段是我在 php scipt 中回显的字段,但我认为我的脚本不太正确,因为我在脚本的最后一行不断收到此错误。我将非常感谢您对此问题的一些贡献和帮助

解析错误:语法错误,文件意外结束 C:\Users\imasha\Documents\My Web Sites\Personal Site1\dropdown2.php 在第 63 行

这是我更新的脚本

        <?php
         // Php Code to connect to postgresqldatabase
         include ("connection.php");

        $query = 'select * from fieldtally order by pipeno asc'; 
        $result = pg_query($db_handle,$query); 
        while ($row = pg_fetch_row($result))
        {

       // Creates Arrays to use in dropdowns
            $pipeno_array[] = $row[0];
            $wallthickness_array[] = $row[1];
        } 

        // Declare dropdown functions for the HTML form
        function dropdown($field_name, $num){

        // Creates the Dropdown
            echo "<select name=\"".$field_name."\" id=\"".$field_name.$num."\"\n";
            echo "<option value=\"\"> --- Select --- </option>\n";
            // Chooses which array to use for Dropdown options
            global $pipeno_array, $wallthickness_array;
            $name_array = ($field_name == 'pipeno') ? $pipeno_array : $wallthickness_array;
            // Creates the Dropdown options based off the array above
            foreach($name_array as $k){
                echo "<option value=\"$k\">$k</option> \n"; }
            // Ends the Dropdown
             echo "</select>\n"; 
             if($field_name == 'pipeno'){
                $wallthickness = 'wallthickness';
                echo $formValue->DJNo($DJno);
                echo $formValue->Heatno1($pipeweight);
                echo $formValue->pipeno2($pipeno2);
                echo $formValue->Heatno2($pipegrade);
        }
        ?>

        <html>
        <head><title>UG Pipeline Fiedl Data Capture</title>
        </head>
        <body>
        <p></p>
        <form action="<?php echo $PHP_SELF; ?>" method="post">
        <table width="600" cellpadding= "10" cellspacing="1" border="2">
        <tr align="center" valign="top">
        <td align="center" colspan="1" rowspan="1" bgcolor="#64b1ff">
        <h3>Input Field Tally Information</h3>
        Select Pipe No:<?php dropdown('pipeno', 1); ?>  Select Wall Thickness:<?php dropdown('wallthickness', 1); ?><br /><br />

     DJ No:<input type="text" name="jointno" value="<?php $DJno = "jointno"; echo $formValue->DJNo($DJno);?>"> 

    HeatNo1: <input type="text" name="pipeweight" value="<?php $pipeweight = "pipeweight"; echo $formValue->Heatno1($pipeweight); ?>"><br><br>

    pipeno2: <input type="text" name="loadtally" value="<?php $pipeno2 = "loadtally"; echo $formValue->pipeno2($pipeno2);?>"> 

    HeatNo2: <input type="text" name="pipeweight" value="<?php $pipeweight = "pipegrade"; echo $formValue->Heatno2($pipegrade); ?>"><br><br>

    Input Serial No: <input type="text" name="serialno"><br><br> 

    Input measured Length: <input type="text" name="measuredlength"><br><br>

    <input type="Submit" name="submit" value="Submit">

    </td></tr></table>
    </form>

<p>
<form action="fieldbend.php" method="post">
<table width="600" cellpadding="10" cellspacing="1" border="2">
<tr align="center" valign="top">
<td align="center" colspan="1" rowspan="1" bgcolor="#ff9d9d">
<h3>Input Field Bend Information</h3>
Select Pipe No:<?php dropdown('pipeno', 2); ?>  Select Wall Thickness:<?php dropdown('wallthickness', 2); ?><br /><br />
Input Measured Distance: <input type="text" name="measureddistance"><br /><br /> Input Bend Angle: <input type="text" name="benddegree"><br><br>
Input Bend Type: <input type="text" name="bendtype"><br><br>
<input type="Submit" name="submit" value="Submit">
</td></tr></table>
</form></P>

<p>
<form action="apptally.php" method="post">
<table width="600" cellpadding="10" cellspacing="0" border="2">
<tr align="center" valign="top">
<td align="center" colspan="1" rowspan="1" bgcolor="#66CC66">
<h3>Input App. Tally Information</h3>
 Input Type: <input type="text" name="type">   Input Serial No: <input type="text" name="serialno"><br><br>
Input Reference ID: <input type="text" name="referenceid"><br><br>
<input type="Submit" name="submit" value="Submit">
</td></tr></table>
</form></P>
     </body>
    </html>

【问题讨论】:

  • Shade,由于您的原始问题已得到解答,并且这是一个新问题,您应该将其作为一个新问题进行,而不是通过编辑您的第一个问题。首先,大多数用户不会看到您的问题,因为它有答案。其次,有了新的问题/答案,您和帮助解决您的问题的人都将获得代表点数,而您都不会通过回答此编辑获得代表点数。一旦您将其作为新问题发布,我将很乐意再次提供帮助。

标签: php html postgresql gis


【解决方案1】:

我注意到您的代码中存在一些问题。

Mauricio 指出的第一个问题是您的 &lt;options&gt;&lt;/options&gt; 不在您的 &lt;select name="education"&gt;&lt;/select&gt; 标签内。还有为什么你选择的名字是education,而选项是$row['pipeno'],而你却得到$_POST['pipeno']?

第二个问题是当您回显&lt;options&gt;&lt;/options&gt; 时。

//<option> value="<?=$row["pipeno"]; ?>"><?=$row["pipeno"]; ?></option>
        print ("<option> $row[0]</option>");

您正在注释掉 &lt;option&gt; 开始标记,value 在 &lt;option&gt; 标记之外,因为您已经关闭了它,看起来您使用 &lt;option value="&lt;?=$row["pipeno"]; ?&gt;"&gt;&lt;?=$row["pipeno"]; ?&gt;&lt;/option&gt; 和 print ("&lt;option&gt; $row[0]&lt;/option&gt;"); 重复每个选项两次/p>

编辑 这已更新以匹配您在问题中编辑的代码

再次查看后,我认为您的问题是如何创建下拉列表。试着把它放在顶部,作为一个函数,然后你可以稍后在需要的地方调用它。然后,您可以指定下拉列表是针对pipeno 还是wallthickness,然后只需指定下拉列表的#,这样它就可以创建一个唯一的id。

尝试将代码更改为-

<?php
 $PGHOST = "localhost:25376";
 $PGDATABASE = "Pipeline";
 $PGUSER = "postgres";
 $PGPASSWORD = "Casa2009";
 $PGPORT = 5432;
 $db_handle = pg_connect("dbname=$PGDATABASE user=$PGUSER password=$PGPASSWORD");
 if ($db_handle) {
 echo 'Connection attempt succeeded.';
 } else {
 echo 'Connection attempt failed.';
 }

$query = 'select * from fieldtally order by pipeno asc '; 
$result = pg_query($db_handle,$query); 
while ($row = pg_fetch_row($result))
{
    // Creates Arrays to use in dropdowns
    $pipeno_array[] = $row[0];
    $wallthickness_array[] = $row[1];
} 

 // This function creates dropdowns that can be used in your forms
function dropdown($field_name, $num){
    // Creates the Dropdown
    echo "<select name=\"".$field_name."\" id=\"".$field_name.$num."\"\n";
    echo "<option value=\"\"> --- Select --- </option>\n";
    // Chooses which array to use for Dropdown options
    global $pipeno_array, $wallthickness_array;
    $name_array = ($field_name == 'pipeno') ? $pipeno_array : $wallthickness_array;
    // Creates the Dropdown options based off the array above
    foreach($name_array as $k){
        echo "<option value=\"$k\">$k</option>\n"; }
    // Ends the Dropdown
    echo "</select>\n";
}
?>

<html>
<head><title>UG Pipeline Fiedl Data Capture</title>
</head>
<body>
<p></p>
<form action="<?php echo $PHP_SELF; ?>" method="post">
<table width="600" cellpadding= "10" cellspacing="1" border="2">
<tr align="center" valign="top">
<td align="center" colspan="1" rowspan="1" bgcolor="#64b1ff">
<h3>Input Field Tally Information</h3>
Select Pipe No:<?php dropdown('pipeno', 1); ?>  Select Wall Thickness:<?php dropdown('wallthickness', 1); ?><br /><br />
Input Joint No: <input type="text" name="jointno"> Input measured Length: <input type="text" name="measuredlength"><br><br>
Input Serial No: <input type="text" name="serialno"><br><br> 
<input type="Submit" name="submit" value="Submit">
</td></tr></table></form> 

<p>
<form action="fieldbend.php" method="post">
<table width="600" cellpadding="10" cellspacing="1" border="2">
<tr align="center" valign="top">
<td align="center" colspan="1" rowspan="1" bgcolor="#ff9d9d">
<h3>Input Field Bend Information</h3>
Select Pipe No:<?php dropdown('pipeno', 2); ?>  Select Wall Thickness:<?php dropdown('wallthickness', 2); ?><br /><br />
Input Measured Distance: <input type="text" name="measureddistance">  Input Bend Angle: <input type="text" name="benddegree"><br><br>
Input Bend Type: <input type="text" name="bendtype"><br><br>
<input type="Submit" name="submit" value="Submit">
</td></tr></table>
</form></P>

<p>
<form action="apptally.php" method="post">
<table width="600" cellpadding="10" cellspacing="0" border="2">
<tr align="center" valign="top">
<td align="center" colspan="1" rowspan="1" bgcolor="#66CC66">
<h3>Input App. Tally Information</h3>
 Input Type: <input type="text" name="type">   Input Serial No: <input type="text" name="serialno"><br><br>
Input Reference ID: <input type="text" name="referenceid"><br><br>
<input type="Submit" name="submit" value="Submit">
</td></tr></table>
</form></P>

</body>
</html>

这是我的示例fieldtally 表 -

这是表单的样子 -

注意:我使用 select size="3" 来显示该图像的多行,但在上面的代码中将其保留为标准的 1 行。

【讨论】:

  • 我试过这个但我得到这个错误 - 注意:未定义的索引:第 38 行 C:\Users\iegbulefu\Documents\My Web Sites\Personal Site1\dropdown2.php 中的 pipeno。它完全从表单中删除 wallthickness 字段。这是这一行 - echo "\n";
  • 抱歉,我已更新代码以修复错误。 Undefined index: pipeno 是因为您使用的是pg_fetch_row(),所以我应该使用$row[0] 而不是$row['pipeno']。 wallthickness 字段未显示,因为我在 &lt;/select&gt; 结束标记之前遗漏了结束 ?&gt; 标记。进行这些更改,如果它修复了错误,请告诉我。
  • 嗨,肖恩,我已经进行了必要的修改,但它的行为仍然相同。
  • 嗨,肖恩,我已经进行了必要的修改,但它的行为仍然相同。壁厚和 Pipeno 字段都应该是下拉列表,并且在同一个表单上,它也应该在提交选项卡上方,因为用户需要滚动这两个字段,选择 pipeno 和相应的壁厚,然后输入数据输入'jointno','measured length'和serialno,然后按提交选项卡以将数据推送到数据库中。当我将 Wallthickness 字段移动到 php 结束标记下方的位置时,它显示在表单之外。
  • 那么您还有更多需要纠正的地方。 (1) 你在&lt;form&gt; 之前有&lt;/form&gt; - &lt;p&gt;&lt;/form&gt;&lt;/P&gt;&lt;form action="fieldtally.php" method="post"&gt;,(2) '壁厚应该是一个下拉菜单',但你将它作为一个文本框发布 - Select Wall Thickness: &lt;input type="text" name="wallthickness"&gt;。 (3) '它应该在提交标签上方',但你没有在表单或其他字段('jointno','measured length','serialno')中显示'提交标签',所以我没有'不知道如何帮助您移动它们。另外,我发现您从之前在fieldtally 中发布的行创建下拉列表很有趣。
【解决方案2】:

它在这里:

Select Pipe No: <select name="education"> </select>  Select Wall Thickness: <input type="text" name="wallthickness"><br><br>

您在同一行中打开标签选择和关闭,并且在放置元素之后。要解决这个问题,请更正以下行:

变化:

Select Pipe No: <select name="education"> </select>  Select Wall Thickness: <input type="text" name="wallthickness"><br><br>

到:

Select Wall Thickness: <input type="text" name="wallthickness"><br><br>
Select Pipe No: <select name="education"> 

并在关闭正文之前关闭选择标签,如下所示:

</select>
</body>

在这里,您将元素放入标签选择中。试试看

【讨论】:

  • 您好 Mauricio,感谢您的建议,我试过了,但效果不大,无法解决问题。
猜你喜欢
  • 2023-04-09
  • 1970-01-01
  • 2010-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多