PHP\HTML脚本,用于从下拉列表中选择项目时自动填充表单字段 [英] PHP\HTML script to auto populate form fields when an item is selected from a dropdown list

查看:124
本文介绍了PHP\HTML脚本,用于从下拉列表中选择项目时自动填充表单字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我创建了一个HTML表单,其中有一个下拉列表,列出了
postgresql数据库中的项目,我希望表单中的某些字段自动填充到
中。从
的下拉菜单中选择一项时的数据库。在表单上还有一些其他字段,用户需要在按下提交按钮
将整个数据推回到数据库之前,填写



当前,我的脚本无法连接到数据库或无法填充
表单上的下拉列表,也无法自动填充
其他指定的表单字段( (下拉列表中的项目
的属性也存储在数据库中),第二个问题
im的另一个问题是,在将数据输入到输入字段中并在
之后单击提交。它在数据库中创建新记录,而不是在数据库中填充所需列的
。有人可以请
看一下我的脚本并帮帮我吗。



这是创建带有
下拉菜单的数据输入表单的脚本




 <?php 
//连接到postgresql数据库$ b的Php代码$ b $ PGHOST =本地主机:25376;
$ PGDATABASE =管道;
$ PGUSER = postgres;
$ PGPASSWORD = Casa2009;
$ PGPORT = 5432;
$ db_handle = pg_connect( dbname = $ PGDATABASE user = $ PGUSER password = $ PGPASSWORD);


//<!-#2->
if(isset($ set _($ _ POST ['submit_1']))){
//将现场数据发布到数据库中的代码
$ pipeno = pg_escape_string($ _POST [’pipeno’]);
$ heatno1 = pg_escape_string($ _POST [’heatno1’]);
$ pipeno2 = pg_escape_string($ _POST [’pipeno2’]);
$ heatno2 = pg_escape_string($ _ POST [’heatno2’]);
$ Djointno = pg_escape_string($ _ POST [’Djointno’]);
$ measuredlength = pg_escape_string($ _ POST [’measuredlength’]);
$ serialno = pg_escape_string($ _ POST [’serialno’]);
$ wthick = pg_escape_string($ _ POST [’wthick’]);
$ remarks = pg_escape_string($ _ POST [’remarks’]);
//<!-#3->
$ query =更新字段设置为wthick = $ wthick,pipeno = $ pipeno,heatno1 = $ heatno1,pipeno2 = $ pipeno2,heatno2 = $ heatno2,Djointno = $ Djointno,measuredlength = $ measuredlength,serialno = $ serialno ,remarks = $ remarks其中pipeno = $ pipeno;
场内插入(wick,pipeno,heatno1,pipeno2,heatno2,Djointno,measuredlength,serialno,remark),
选择$ wthick,$ pipeno,$ heatno1,$ pipeno2,$ heatno2,$ Djointno,$ measuredlength,$ serialno,$ remark,
不存在(从pipetally中选择pipeno,其中pipeno = $ pipeno);

//<!-#4a->
$ result = pg_query($ query);
if(!$ result){
$ errormessage = pg_last_error();
$ message =查询错误:。 $ errormessage;
}
$ message = sprintf(这些值已插入到管道数据库中-%s%s%s%s%s%s%s%s%s%s%s,$ wthick,$ pipeno, $ heatno1,$ pipeno2,$ heatno2,$ Djointno,$ measuredlength,$ serialno,$ remarks);
}
//<!-#2->
if(isset($ set _($ _ POST ['submit_2']))){
//将场弯数据发布到suto数据库中的代码
$ pipeno = pg_escape_string($ _POST ['pipeno']) ;
$ wthick1 = pg_escape_string($ _ POST [’wthick1’]);
$ heatno1 = pg_escape_string($ _POST [’heatno1’]);
$ pipeno2 = pg_escape_string($ _POST [’pipeno2’]);
$ heatno2 = pg_escape_string($ _ POST [’heatno2’]);
$ Djointno = pg_escape_string($ _ POST [’Djointno’]);
$ measureddistance = pg_escape_string($ _ POST [’measureddistance’]);
$ benddegree = pg_escape_string($ _ POST [’benddegree’]);
$ bendtype = pg_escape_string($ _ POST [’bendtype’]);
$ remarks = pg_escape_string($ _ POST [’remarks’]);
//<!-#3->
$ query1 = INSERT INTO fieldbend(pipeno,wthick1,heatno1,pipeno2,heatno2,Djointno,measureddistance,benddegree,bendtype,remarks)VALUES('$ pipeno','$ wthick1','$ heatno1',' $ pipeno2','$ heatno2','$ Djointno','$ measureddistance','$ benddegree','$ bendtype','$ remarks')重复键更新wthick1 ='$ wthick1',heatno1 ='$ heatno1 ',pipeno2 ='$ pipeno2',heatno2 ='$ heatno2',Djointno ='$ Djointno'';

//<!-#4a->
$ result1 = pg_query($ query1);
if(!$ result1){
$ errormessage = pg_last_error();
$ message1 =查询错误:。 $ errormessage;
}
$ message1 = sprintf(这些值已插入管道数据库-%s%s%s%s%s%s%s%s%s%s%s,$ pipeno,$ wthick1,$ heatno1,$ pipeno2,$ heatno2,$ Djointno,$ measureddistance,$ benddegree,$ bendtype,$ remarks);
}
//<!-#2->
if(isset($ set _ $ [POST_'submit_3'])){
//将数据适当地发送到数据库中的代码
$ pipeno = pg_escape_string($ _POST ['pipeno']) ;
$ wthick2 = pg_escape_string($ _ POST [’wthick2’]);
$ tallytype = pg_escape_string($ _POST [’tallytype’]);
$ qty = pg_escape_string($ _POST [’qty’]);
$ serialno = pg_escape_string($ _ POST [’serialno’]);
$ referenceid = pg_escape_string($ _ POST [’referenceid’]);
//<!-#3->
$ query2 =将应用适当地插入(pipeno,wthick2,tallytype,qty,serialno,referenceid)VALUES('$ pipeno','$ wthick2','$ tallytype','$ qty','$ serialno' ,'$ referenceid')在重复键更新上wthick2 ='$ wthick2',pipeno ='$ pipeno';

//<!-#4a->
$ result2 = pg_query($ query2);
if(!$ result2){
$ errormessage = pg_last_error();
$ message2 =查询错误:。 $ errormessage;
}
$ message2 = sprintf(这些值已插入管道数据库-%s%s%s%s%s%s%s,$ pipeno,$ wthick2,$ tallytype,$ qty, $ serialno,$ referenceid);
}
//用于从数据库中提取数据并加载到
形式的代码$ query =‘从pipeno asc的字段顺序中选择pipeno’;
$ result = pg_query($ db_handle,$ query);
while($ row = pg_fetch_row($ result))
{
//创建要在下拉列表中使用的数组
$ pipeno_array [] = $ row [0];

}
//此函数创建将以以下形式使用的下拉列表
function dropdown($ field_name,$ num){
//创建下拉列表
//<!-#5a->
$ c =($ field_name =='pipeno')吗? ’onChange = check(’。$ num。’);’:’’;
echo< select name = \。$ field_name。 \ id = \。$ field_name。$ num。 \ $ c> \n;
echo< option value = \ \> ---选择---< / option> \n;
//选择要用于下拉选项的数组
global $ pipeno_array;
$ name_array =($ field_name =='pipeno')吗? $ pipeno_array:$壁厚;
//基于
foreach($ name_array as $ k){
echo< option value = \ $ k\> $ k< option> \n; }
//结束下拉列表
echo< / select> \n;
}
?>
< html>
< head>
< meta charset = utf-8>
< title> UG管道字段数据捕获< / title>
< / head>
< body>
<元http-equiv = Content-Type content = text / html; charset = utf-8>
< / head>
< body>
< script type = text / javascript src = https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js>< / script>
< script type = text / javascript>

< ;!-#5b->
function check(num){
var pipeno_id =‘#pipeno _’+ num;
var pipeno = $(pipeno_id).val();
if(pipeno!=){
jQuery.ajax({
type: POST,
url: check.php,
数据: 'pipeno ='+ pipeno,
缓存:false,
成功:function(response){
var response_array = JSON.parse(response);
$('#heatno1' ).val(response_array ['heatno1']);
$('#pipeno2')。val(response_array ['pipeno2']);
$('#heatno2')。val(response_array [ 'heatno2']);
$('#Djointno')。val(response_array ['Djointno']);
}
});
}
else {
$(’#heatno1’)。val(’’);
$(’#pipeno2’)。val(’’);
$(’#heatno2’)。val(’’);
$(’#Djointno')。val(’’);}
}
< / script>
< ;!-#4b->
<!-<?php printf($ message);?>->

<!-#6.1->
< form action = method = post>
< table width = 800 cellpadding = 10 cellspacing = 1 border = 2>
< tr align = center valign = top>
< td align = center colspan = 1 rowspan = 1 bgcolor =#00CC00>
< h3>输入字段提示信息< / h3>
选择壁厚:<选择名称= wthick id = wthick>
< ;!-#7.1->
< option value => -选择-< / option>
< option value = 9.80> 9.80< / option>
< option value = 13.50> 13.50< / option>
< option value = 15.90> 15.90< / option>
< /选择>
选择管道号:<?php dropdown(‘pipeno’,1); ?> HeatNo1:<输入类型=文本 name = heatno1 id = heatno1>< br />< br />
PipeNo2:<输入类型= text name = pipeno2 id = pipeno2> HeatNo2:<输入类型=文本 name = heatno2 id = heatno2>联合编号:<输入类型=文本 name = Djointno< br />< br / >
输入的测量长度:<输入类型=文本 name = measuredlength>输入序列号:< input type = text name = serialno>< br>< br>备注:< input type = text name =备注< br>< br>
< ;!-#8.1->
<输入类型=提交 name = submit_1 value =提交>
< ;!-#9.1->
< / td< / tr> / table /< / form>
< ;!-#6.2->
< form action = method = post>
< table width = 800 cellpadding = 10 cellspacing = 1 border = 2>
< tr align = center valign = top>
< td align = center colspan = 1 rowspan = 1 bgcolor =#FF99FF>
< h3>输入字段弯曲信息< / h3>
选择壁厚:<选择名称= wthick1 id = wthick1>
< ;!-#7.2->
< option value => -选择-< / option>
< option value = 9.80> 9.80< / option>
< option value = 13.50> 13.50< / option>
< option value = 15.90> 15.90< / option>
< / select>
< ;!-#10.1->
选择管道号:<?php dropdown(‘pipeno’,2); ?> HeatNo1:<输入类型=文本 name = heatno1 id = heatno1_2> < br>< br>
PipeNo2:<输入类型= text name = pipeno2 id = pipeno2_2> HeatNo2:<输入类型=文本 name = heatno2 id = heatno2_2>联合编号:< input type = text name = Djointno> < br>< br>
输入测量距离:< input type = text name = measureddistance>输入弯曲角度:<输入类型=文本 name =弯曲度> < br>< br>
选择弯曲类型:<选择名称=弯曲类型 id =弯曲类型>
< option value => -选择-< / option>
< option value = Combo> Combo< / option>
< option value = SAG> SAG< / option>
< option value = OB> OB< / option>
< option value = SBRT> SBRT< / option>
< option value = SBLT> SBLT< / option>
< option value = HBLT> HBLT< / option>
< option value = HBRT> HBRT< / option>< p>< / p>
备注:< input type = text name =备注>< br>< / br>

<输入类型=提交 name = submit_2 value =提交>
< ;!-#9.2->
< / td< / tr> / table /< / form>
< ;!-#6.3->

< form action = method = post>
< table width = 800 cellpadding = 10 cellspacing = 1 border = 2>
< tr align = center valign = top>
< td align = center colspan = 1 rowspan = 1 bgcolor =#99FF33>
< h3>输入应用程序。提示信息< / h3>
<!-#11->
选择壁厚:<选择名称= wthick2 id = wthick2>
< ;!-#7.3->
< option value => -选择-< / option>
< option value = 9.80> 9.80< / option>
< option value = 13.50> 13.50< / option>
< option value = 15.90> 15.90< / option>
< / select>
< ;!-#10.2->
选择管道号:<?php dropdown(‘pipeno’,3); ?> < br>< br>输入提示类型:<输入类型=文本 name =提示类型>
输入提示数量:< input type = text name = qty>< br>< / br>输入序列号:< input type = text name = serialno>
RefID:< input type = text name = referenceid>< br>< / br>
< ;!-#8.3->
<输入类型=提交 name = submit_3 value =提交>
< / td< / tr> / table
< / form>

< / body>
< / html>




Php脚本将数据加载到表单字段并从中推回数据
数据库-文件名= check1.php




 <?php 
//连接到postgresql数据库的Php代码
$ PGHOST = localhost:25376;
$ PGDATABASE =管道;
$ PGUSER = postgres;
$ PGPASSWORD = Casa2009;
$ PGPORT = 5432;
$ db_handle = pg_connect( dbname = $ PGDATABASE user = $ PGUSER password = $ PGPASSWORD);

//用于从数据库中提取数据并加载到
形式的代码$ pipeno = pg_escape_string($ _ POST [’pipeno’]);
$ query = SELECT * FROM fieldtally1 WHERE pipeno = $ pipeno;
$ result = pg_query($ db_handle,$ query);
$ row = pg_fetch_row($ result);
$ row_info = array('heatno1'=> $ row [1],'pipeno2'=> $ row [2],'heatno2'=> $ row [3],'jointno'=> ; $ row [4]);
$ row_info = json_encode($ row_info);
print_r($ row_info);
?>




当我点击带有或不带有数据的提交按钮时遇到的错误是
吗?



注意:未定义变量:在C:\Users\iegbulefu\Documents\My
网站\中的注释第27行上的Personal Site1\autopopulate.php



警告:pg_query():查询失败:错误:
或附近的语法错误 GGFF第1行:
... no2 = 67677,Djointno = 8,measuredlength = 80,serialno = 99GGFF,remar ... ^
in C:\Users\iegbulefu\Documents\我的网站30第30行上的个人
Site1\autopopulate.php



解决方案

I通过使用Ajax和JQuery可以做到这一点。您需要做3件事-在文档的 head 头中添加Ajax / JQuery代码,创建php文件供Ajax连接,并添加 onChange id & value 字段的属性。



在您的< head>< / head>中在您的< title>< / title> -



<$ p之后添加此javascript $ p> <脚本类型= text / javascript src = https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js> < / script>
< script type = text / javascript>
function check(){
var pipeno = $(’#pipeno’)。val();
if(pipeno!= pipeno){
jQuery.ajax({
type: POST,
url: check.php,
数据:'pipeno ='+ pipeno,
缓存:false,
成功:function(response){
var response_array = JSON.parse(response);
$('#wallthickness ').val(response_array ['wallthickness']);
$('#jointno')。val(response_array ['jointno']);
$('#measuredlength')。val(response_array ['measuredlength']);
$('#serialno')。val(response_array ['serialno']);}
});
}
else {
$(’#wallthickness’)。val(’’);
$(’#jointno’)。val(’’);
$(’#measuredlength’)。val(’’);
$(’#serialno')。val(’’);}
}
< / script>

然后创建一个名为 check.php 的文件与代码-

 <?php 
//连接到postgresql数据库
的Php代码include( connection.php);
//用于从数据库中提取数据并加载到
形式的代码$ pipeno = pg_escape_string($ _ POST [’pipeno’]);
$ query =选择*从野外在哪里pipeno = $ pipeno;
$ result = pg_query($ db_handle,$ query);
$ row = pg_fetch_row($ result))
$ row_info = array('wallthickness'=> $ row [1],'jointno'=> $ row [2],'measuredlength'= > $ row [3],'serialno'=> $ row [4]);

$ row_info = json_encode($ row_info);
print_r($ row_info);
?>

最后,添加 id &将 value 属性分配给表单字段(不要更改文件,只需更新这些行)

 选择管道编号:<选择名称=管道编号 id =管道编号 onChange = check()><选项值=管道编号> -选择-< / option> 
...(保持数据库连接/如何创建下拉列表,在这里,只需编辑下面的选项)
echo< option value = \ $ pipeno\> $ pipeno< / option>;
...(在此处保持数据库连接/创建下拉菜单的方式,等等。)
< / select>
...
输入联合编号:< input type = text name = jointno id = jointno>
输入壁厚:<输入类型=文本 name = wallthickness id = wallthickness>
输入的测量长度:<输入类型=文本 name = measuredlength id = measuredlength>
输入序列号:< input type = text name = serialno id = serialno>






已更新10/19由于代码更改-



我在您的代码中添加了编号的锚点,并且注释位于末尾。您可以用2个文件来完成全部操作,但是第二个文件 check1.php 只能进行1个数据库查询,否则您的ajax将出错。



autopopulate.php-

 <!-#1-> ; 
<?php
//连接到postgresql数据库的Php代码
$ PGHOST = localhost:25376;
$ PGDATABASE =管道;
$ PGUSER = postgres;
$ PGPASSWORD = Casa2009;
$ PGPORT = 5432;
$ db_handle = pg_connect( dbname = $ PGDATABASE user = $ PGUSER password = $ PGPASSWORD);

//<!-#2->
if(isset($ set _ $ [POST_submit_1'])){
//将数据发布到数据库中的代码
$ pipeno = pg_escape_string($ _POST [’pipeno’]);
$ wallthickness = pg_escape_string($ _ POST [’wallthickness’]);
$ heatno1 = pg_escape_string($ _POST [’heatno1’]);
$ pipeno2 = pg_escape_string($ _POST [’pipeno2’]);
$ heatno2 = pg_escape_string($ _ POST [’heatno2’]);
$ jointno = pg_escape_string($ _ POST [’jointno’]);
$ measuredlength = pg_escape_string($ _ POST [’measuredlength’]);
$ serialno = pg_escape_string($ _ POST [’serialno’]);
$ wthick = pg_escape_string($ _ POST [’wthick’]);

//<!-#3->
$ query = INSERT INTO fieldtally1(pipeno,wallthickness,heatno1,pipeno2,heatno2,jointno,measuredlength,serialno,wthick)VALUES('$ pipeno','$ wallthickness','$ heatno1','$ pipeno2 ','$ heatno2','$ jointno','$ measuredlength','$ serialno','$ wthick')重复键更新wallthickness ='$ wallthickness',heatno1 ='$ heatno1',pipeno2 ='$ pipeno2 ',heatno2 ='$ heatno2',jointno ='$ jointno',measuredlength ='$ measuredlength',serialno ='$ serialno',wthick ='$ wthick';

//<!-#4a->
$ result = pg_query($ query);
if(!$ result){
$ errormessage = pg_last_error();
$ message =查询错误:。 $ errormessage;
}
$ message = sprintf(这些值已插入数据库-%s%s%s%s%s%s%s%s%s%s%s,$ pipeno,$ wallthickness,$ heatno1,$ pipeno2,$ heatno2,$ jointno,$ measuredlength,$ serialno,$ wthick);
}

//代码从数据库中提取数据并加载到
$ query =‘选择pipeno,pipetickness由pipet asc的fieldtally1订单’中;
$ result = pg_query($ db_handle,$ query);
while($ row = pg_fetch_row($ result))
{
//创建要在下拉列表中使用的数组
$ pipeno_array [] = $ row [0];
$ wallthickness_array [] = $ row [1];
}

//此函数创建可在表单中使用的下拉列表
函数dropdown($ field_name,$ num){
//创建下拉列表
//<!-#5a->
$ c =($ field_name =='pipeno')吗? ’onChange = check(’。$ num。’);’:’’;
echo< select name = \。$ field_name。 \ id = \。$ field_name。$ num。 \ $ c> \n;
echo< option value = \ \> ---选择---< / option> \n;
//选择要用于下拉选项的数组
全局$ pipeno_array,$ wallthickness_array;
$ name_array =($ field_name =='pipeno')吗? $ pipeno_array:$ wallthickness_array;
//基于
foreach($ name_array as $ k){
echo< option value = \ $ k\> $ k< option> \n; }
//结束下拉列表
echo< / select> \n;
}

?>
< html>
< head< title> UG流水线字段数据捕获< / title>< / head>
< body>
< script type = text / javascript src = https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js>< / script>
< script type = text / javascript>
< ;!-#5b->
function check(num){
var pipeno_id =‘#pipeno _’+ num;
var pipeno = $(pipeno_id).val();
if(pipeno!=){
jQuery.ajax({
type: POST,
url: check1.php,
数据: 'pipeno ='+ pipeno,
缓存:false,
成功:function(response){
var response_array = JSON.parse(response);
$('#heatno1' ).val(response_array ['heatno1']);
$('#pipeno2')。val(response_array ['pipeno2']);
$('#heatno2')。val(response_array [ 'heatno2']);
$('#jointno')。val(response_array ['jointno']);
//$('#measuredlength').val(response_array['measuredlength'] ); //应该从用户
输入// // $('#serialno')。val(response_array ['serialno']); //也应该从用户
输入}
});
}
else {
$(’#heatno1’)。val(’’);
$(’#pipeno2’)。val(’’);
$(’#heatno2’)。val(’’);
$(’#jointno')。val(’’);}
}
< / script>

< ;!-#4b->
<?php printf($ message);?>
< ;!-#6.1->
< form action = method = post>
< table width = 800 cellpadding = 10 cellspacing = 1 border = 2>
< tr align = center valign = top>
< td align = center colspan = 1 rowspan = 1 bgcolor =#64b1ff>
< h3>输入字段提示信息< / h3>
选择管道厚度:<选择名称= wthick id = wthick>
< ;!-#7.1->
< option value => -选择-< / option>
< option value = 9.8> 9.8< / option>
< option value = 13.5> 13.5< / option>
< option value = 15.9> 15.9< / option>
< /选择>
选择管道号:<?php dropdown(‘pipeno’,1); ?>选择壁厚:<?php dropdown('wallthickness',1); < br />< br />
HeatNo1:<输入类型=文本 name = heatno2 id = heatno1> PipeNo2:<输入类型=文本 name = pipeno2 id = pipeno1> HeatNo2:<输入类型=文本 name = heatno2 id = heatno2>< br />< br />
联合编号:< input type = text name = jointno>输入测量长度:<输入类型=文本 name = measuredlength>输入序列号:< input type = text name = serialno>< br>< br>
< ;!-#8.1->
<输入类型=提交 name = submit_1 value =提交>
< ;!-#9.1->
< / td< / tr> / table /< / form>
< / p>

< ;!-#6.2->
< form action = method = post>
< table width = 800 cellpadding = 10 cellspacing = 1 border = 2>
< tr align = center valign = top>
< td align = center colspan = 1 rowspan = 1 bgcolor =#ff9d9d>
< h3>输入字段弯曲信息< / h3>
选择壁厚:<选择name = wallthickness id = wallthickness>
< ;!-#7.2->
< option value => -选择-< / option>
< option value = 9.8> 9.8< / option>
< option value = 13.5> 13.5< / option>
< option value = 15.9> 15.9< / option>
< / select>
< ;!-#10.1->
选择管道号:<?php dropdown(‘pipeno’,2); ?>选择壁厚:<?php dropdown(’wallthickness’,2); < br />< br />
HeatNo1:<输入类型=文本 name = heatno1 id = heatno1_2> PipeNo2:<输入类型=文本 name = pipeno2 id = pipeno2_2> HeatNo2:<输入类型=文本 name = heatno2 id = heatno2_2>< br />< br />
联合编号:< input type = text name = jointno>输入测量距离:<输入类型=文本 name =测量距离>< br>< br>
输入弯曲角度:<输入类型= text name =弯曲度>输入弯曲类型:<输入类型=文本 name =弯曲类型>< br>< br>
< ;!-#8.2->
<输入类型=提交 name = submit_2 value =提交>
< ;!-#9.2->
< / td< / tr> / table /< / form>
< / p>

<!-#6.3->
< form action = method = post>
< table width = 800 cellpadding = 10 cellspacing = 1 border = 2>
< tr align = center valign = top>
< td align = center colspan = 1 rowspan = 1 bgcolor =#66CC66>
< h3>输入应用程序。提示信息< / h3>
<!-#11->
选择壁厚:<选择名称= wallthickness1 id = wallthickness1>
< ;!-#7.3->
< option value => -选择-< / option>
< option value = 9.8> 9.8< / option>
< option value = 13.5> 13.5< / option>
< option value = 15.9> 15.9< / option>
< / select>
< ;!-#10.2->
选择管道号:<?php dropdown(‘pipeno’,3); ?>选择壁厚:<?php dropdown(壁厚,3); < br />< br />
输入提示类型:< input type = text name = type>输入序列号:< input type = text name = serialno>< br>< br>
输入参考ID:< input type = text name = referenceid>< br>< br>
< ;!-#8.3->
<输入类型=提交 name = submit_3 value =提交>
< / td< / tr> / table
< / form>
< / body>
< / html>

check1.php-

 <?php 
//连接到PostgreSQL数据库的代码Php
$ PGHOST = localhost:25376;
$ PGDATABASE =管道;
$ PGUSER = postgres;
$ PGPASSWORD = Casa2009;
$ PGPORT = 5432;
$ db_handle = pg_connect( dbname = $ PGDATABASE user = $ PGUSER password = $ PGPASSWORD);

//用于从数据库中提取数据并加载到
形式的代码$ pipeno = pg_escape_string($ _ POST [’pipeno’]);
$ query = SELECT * FROM fieldtally1 WHERE pipeno = $ pipeno;
$ result = pg_query($ db_handle,$ query);
$ row = pg_fetch_row($ result);
$ row_info = array('heatno1'=> $ row [1],'pipeno2'=> $ row [2],'heatno2'=> $ row [3],'jointno'=> ; $ row [4]);
$ row_info = json_encode($ row_info);
print_r($ row_info);
?>

这里是问题所在-



1 -将所有<?php?> 脚本移到页面顶部。这将:(1)清理您的代码,(2)能够同时处理所有数据库查询。



2-您要更新的查询 fieldtally1 现在已经完成,您可以从数据库中获取下拉列表。



3-解决第二个问题在输入字段中输入数据后,单击提交。它将在数据库中创建一条新记录,而不是填充数据库中所需的列使用 INSERT INTO ... VALUES ...重复键更新... 。如果数据库中已经存在 pipeno (因为它是主键),它将 UPDATE 而不是 INSERT



4-我建议保存您的错误成功消息(#4a),并在html顶部回显它(#4b)。



5a-您从硬编码的 pipeno 下拉列表更改为使用该功能的动态下拉列表。因此,您丢失了javascript- onChange = check();
5b-现在您正在创建 pipeno & onChange = check(); 动态地,您必须更改您的 check()函数以动态获取ID 。



6- $ PHP_SELF 无效。我认为您正在尝试使用 $ _ SERVER ['PHP_SELF'] ,但这很容易被黑,因此最好使用 action = 。 [6.1,6.2,6.3]



7-在执行< select>< option> 时使用相同的 value = ,但每个发布都需要不同,以便您获得该值。另外,大多数< / option> 的拼写错误为< / optio> 。 [7.1,7.2,7.3]



8-如果您在同一页面上有3个表单,则每个表单都必须使用不同的名称,否则您将无法告诉您单击了哪个提交按钮。 [8.1,8.2,8.3]



9-您的前两个表单缺少结束标记-< / td>< / tr>< ; / table>< / form> 。 [9.1,9.2]



10-在您2& 3种形式,您使用的是在第一种形式中使用的 id 。这是无效的,因为每个 id 必须唯一。 [10.1,10.2]



11-第三种形式使用的是在形式2中使用的 id = wallthickness 参见上面有关 id 唯一性的信息。


I created a HTML form with one drop down listing of items from postgresql database, I wish to have some of the fields in the form to populate automatically from the database when an item is selected from the dropdown menu. I also have some other fields on the form which needs to be filled-in by the users before pressing the submit button to push the entire data back to the database.

Currently my script is not able to connect to the database or populate the drop-down listing on the form and also not able to autopopulate the other specified form fields (which are the attributes of the items in the dropdown list also stored in the database) and the second issue im also having is that after entering data into the input fields and click submit. it creates a new record in the database instead of populating the required columns in the database. Can Someone please take a look at my script and help me out.

Here is the script to create the data entry form with a dropdown

 <?php
//Php Code to connect to postgresqldatabase
$PGHOST = "localhost:25376";
$PGDATABASE = "Pipeline";
$PGUSER = "postgres";
$PGPASSWORD = "Casa2009";
$PGPORT = 5432;
$db_handle = pg_connect("dbname=$PGDATABASE user=$PGUSER password=$PGPASSWORD");


//<!-- #2 -->
 if(isset($_POST['submit_1'])){
 //Code to post fieldtally data to the database
 $pipeno = pg_escape_string( $_POST['pipeno']);
 $heatno1 = pg_escape_string( $_POST['heatno1']);
 $pipeno2 = pg_escape_string( $_POST['pipeno2']);
 $heatno2 = pg_escape_string($_POST['heatno2']);
 $Djointno = pg_escape_string($_POST['Djointno']);
 $measuredlength = pg_escape_string($_POST['measuredlength']);
 $serialno = pg_escape_string($_POST['serialno']);
 $wthick= pg_escape_string($_POST['wthick']);
 $remarks= pg_escape_string($_POST['remarks']); 
 //<!-- #3 -->
$query = "Update fieldtally set  wthick = $wthick, pipeno=$pipeno,heatno1=$heatno1,pipeno2=$pipeno2,heatno2=$heatno2,Djointno=$Djointno,measuredlength=$measuredlength,serialno=$serialno,remarks=$remarks where pipeno = $pipeno;
          INSERT INTO fieldtally(wthick, pipeno,heatno1,pipeno2,heatno2,Djointno,measuredlength,serialno,remarks), 
                Select $wthick, $pipeno,$heatno1,$pipeno2,$heatno2,$Djointno,$measuredlength,$serialno,$remark,
                WHERE NOT EXISTS (select pipeno from fieldtally where pipeno = $pipeno)";

//<!-- #4a -->
 $result = pg_query($query);
 if (!$result) {
 $errormessage = pg_last_error();
 $message = "Error with query: " . $errormessage;
 }
 $message = sprintf ("These values were inserted into the pipeline database - %s %s %s %s %s %s %s %s %s",$wthick,$pipeno,$heatno1,$pipeno2,$heatno2,$Djointno,$measuredlength,$serialno,$remarks);
 }
 //<!-- #2 -->
 if(isset($_POST['submit_2'])){
 //Code to post fieldbend data under suto the database
 $pipeno = pg_escape_string( $_POST['pipeno']);
 $wthick1= pg_escape_string($_POST['wthick1']);
 $heatno1 = pg_escape_string( $_POST['heatno1']);
 $pipeno2 = pg_escape_string( $_POST['pipeno2']);
 $heatno2 = pg_escape_string($_POST['heatno2']);
 $Djointno = pg_escape_string($_POST['Djointno']);
 $measureddistance = pg_escape_string($_POST['measureddistance']);
 $benddegree = pg_escape_string($_POST['benddegree']);
 $bendtype= pg_escape_string($_POST['bendtype']); 
 $remarks= pg_escape_string($_POST['remarks']);
 //<!-- #3 -->
$query1 = "INSERT INTO fieldbend(pipeno,wthick1,heatno1,pipeno2,heatno2,Djointno,measureddistance,benddegree,bendtype,remarks)VALUES ('$pipeno','$wthick1','$heatno1','$pipeno2','$heatno2','$Djointno','$measureddistance','$benddegree','$bendtype','$remarks') ON DUPLICATE KEY UPDATE wthick1='$wthick1',heatno1='$heatno1',pipeno2='$pipeno2',heatno2='$heatno2',Djointno='$Djointno''";

//<!-- #4a -->
 $result1 = pg_query($query1);
 if (!$result1) {
 $errormessage = pg_last_error();
 $message1 = "Error with query: " . $errormessage;
 }
 $message1 = sprintf ("These values were inserted into the pipeline database - %s %s %s %s %s %s %s %s %s %s",$pipeno,$wthick1,$heatno1,$pipeno2,$heatno2,$Djointno,$measureddistance,$benddegree, $bendtype,$remarks);
 }
 //<!-- #2 -->
 if(isset($_POST['submit_3'])){
 //Code to post apptally data under suto the database
 $pipeno = pg_escape_string( $_POST['pipeno']);
 $wthick2= pg_escape_string($_POST['wthick2']);
 $tallytype = pg_escape_string( $_POST['tallytype']);
 $qty = pg_escape_string( $_POST['qty']);
 $serialno = pg_escape_string($_POST['serialno']);
 $referenceid = pg_escape_string($_POST['referenceid']);
 //<!-- #3 -->
$query2 = "INSERT INTO apptally(pipeno,wthick2,tallytype,qty,serialno,referenceid)VALUES ('$pipeno','$wthick2','$tallytype','$qty','$serialno','$referenceid') ON DUPLICATE KEY UPDATE wthick2='$wthick2', pipeno='$pipeno'";

//<!-- #4a -->
 $result2 = pg_query($query2);
 if (!$result2) {
 $errormessage = pg_last_error();
 $message2 = "Error with query: " . $errormessage;
 }
 $message2 = sprintf ("These values were inserted into the Pipeline database - %s %s %s %s %s %s",$pipeno,$wthick2,$tallytype,$qty, $serialno,$referenceid);
 }
 // Code to pull data from the database and load onto the form
 $query = 'select pipeno 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];

 } 
 // This function creates dropdowns that will be used in the forms
 function dropdown($field_name, $num){
     // Creates the Dropdown
     //<!-- #5a -->
     $c = ($field_name == 'pipeno') ? ' onChange="check('.$num.');"' : '';
     echo "<select name=\"".$field_name."\" id=\"".$field_name.$num."\"$c>\n";
     echo "<option value=\"\"> --- Select --- </option>\n";
     // Chooses which array to use for Dropdown options
     global $pipeno_array;
     $name_array = ($field_name == 'pipeno') ? $pipeno_array : $wallthick;
     // 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>
<meta charset="utf-8">
<title>UG Pipeline Field Data Capture</title>
</head>
<body>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     </head>
      <body>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">

<!-- #5b -->
        function check(num){
           var pipeno_id = '#pipeno_'+num;
           var pipeno = $(pipeno_id).val();
           if(pipeno != ""){
             jQuery.ajax({
             type: "POST",
             url: "check.php" , 
             data:'pipeno='+pipeno,
             cache: false,
             success: function(response){
             var response_array = JSON.parse(response);
             $('#heatno1').val(response_array['heatno1']);
             $('#pipeno2').val(response_array['pipeno2']); 
             $('#heatno2').val(response_array['heatno2']);
             $('#Djointno').val(response_array['Djointno']);
             }           
            });
           }
           else{
           $('#heatno1').val('');
           $('#pipeno2').val('');
           $('#heatno2').val('');
           $('#Djointno').val('');}
            }
  </script>
          <!-- #4b -->
 <!--<?php printf($message);?>-->

 <!-- #6.1 -->
<form action="" method="post">
 <table width="800" cellpadding= "10" cellspacing="1" border="2">
 <tr align="center" valign="top">
 <td align="center" colspan="1" rowspan="1" bgcolor="#00CC00">
 <h3>Input Field Tally Information</h3>
      Select Wall Thickness:<select name="wthick" id="wthick">
 <!-- #7.1 -->
  <option value=""> --Select-- </option> 
  <option value="9.80">  9.80  </option>
  <option value="13.50"> 13.50 </option>
  <option value="15.90"> 15.90 </option>
  </Select>           
 Select Pipe No:<?php dropdown('pipeno', 1); ?> HeatNo1: <input type="text" name="heatno1" id="heatno1"><br /><br />  
 PipeNo2: <input type="text" name="pipeno2" id="pipeno2"> HeatNo2: <input type="text" name="heatno2" id="heatno2">Joint No: <input type="text" name="Djointno"><br /><br /> 
  Input measured Length: <input type="text" name="measuredlength"> Input Serial No: <input type="text" name="serialno"><br><br> Remarks: <input type="text" name="remarks"><br><br> 
 <!-- #8.1 -->
 <input type="Submit" name="submit_1" value="Submit">
 <!-- #9.1 -->
</td></tr></table></form>
<!-- #6.2 -->
 <form action="" method="post">
 <table width="800" cellpadding= "10" cellspacing="1" border="2">
 <tr align="center" valign="top">
 <td align="center" colspan="1" rowspan="1" bgcolor="#FF99FF">
 <h3>Input Field Bend Information</h3>
  Select Wall Thickness:<select name="wthick1" id="wthick1">
 <!-- #7.2 -->
     <option value=""> --Select-- </option> 
     <option value="9.80">  9.80  </option>
     <option value="13.50">13.50 </option>
     <option value="15.90"> 15.90 </option>
  </select>
 <!-- #10.1 -->           
 Select Pipe No:<?php dropdown('pipeno', 2); ?>   HeatNo1: <input type="text" name="heatno1" id="heatno1_2"> <br><br>
 PipeNo2: <input type="text" name="pipeno2" id="pipeno2_2"> HeatNo2: <input type="text" name="heatno2" id="heatno2_2"> Joint No: <input type="text" name="Djointno"> <br><br>
 Input Measured Distance: <input type="text" name="measureddistance"> Input Bend Angle: <input type="text" name="benddegree"> <br><br>
Select Bend Type:<select name="bendtype" id="bendtype">
    <option value=""> --Select-- </option> 
    <option value="Combo">Combo</option>
    <option value="SAG">SAG</option>
    <option value="OB">OB</option>
    <option value="SBRT">SBRT</option>
    <option value="SBLT">SBLT</option>
    <option value="HBLT">HBLT</option>
    <option value="HBRT">HBRT</option><p></p> 
  Remarks: <input type="text" name="remarks"><br></br>

  <input type="Submit" name="submit_2" value="Submit"> 
 <!-- #9.2 -->
</td></tr></table></form>
<!-- #6.3 -->

<form action="" method="post">
 <table width="800" cellpadding= "10" cellspacing="1" border="2">
 <tr align="center" valign="top">
 <td align="center" colspan="1" rowspan="1" bgcolor="#99FF33">
 <h3>Input App. Tally Information</h3>
 <!-- #11 -->
       Select Wall Thickness:<select name="wthick2" id="wthick2">
 <!-- #7.3 -->
     <option value=""> --Select-- </option> 
     <option value="9.80">  9.80  </option>
     <option value="13.50"> 13.50 </option>
     <option value="15.90"> 15.90 </option>
  </select>
 <!-- #10.2 -->             
 Select Pipe No:<?php dropdown('pipeno', 3); ?>  <br><br> Input Tally Type: <input type="text" name="tallytype">
 Input Tally Qty: <input type="text" name="qty"><br></br>  Input Serial No: <input type="text" name="serialno"> 
 RefID: <input type="text" name="referenceid"><br></br>
 <!-- #8.3 -->
 <input type="Submit" name="submit_3" value="Submit">
 </td></tr></table>
</form>

</body>
</html>

Php Script to load data onto the form field and push data back from the database - filename = check1.php

<?php
//Php Code to connect to postgresqldatabase
$PGHOST = "localhost:25376";
$PGDATABASE = "Pipeline";
$PGUSER = "postgres";
$PGPASSWORD = "Casa2009";
$PGPORT = 5432;
$db_handle = pg_connect("dbname=$PGDATABASE user=$PGUSER password=$PGPASSWORD");

// Code to pull data from the database and load onto the form  
$pipeno = pg_escape_string($_POST['pipeno']);
$query = "SELECT * FROM fieldtally1 WHERE pipeno = $pipeno ";
$result = pg_query($db_handle,$query); 
$row = pg_fetch_row($result);
$row_info = array('heatno1'=>$row[1],'pipeno2'=>$row[2],'heatno2'=>$row[3],'jointno'=>$row[4]);
$row_info = json_encode($row_info);
print_r($row_info); 
?>

the errors I get when I hit the submit button with or without data is this

Notice: Undefined variable: remark in C:\Users\iegbulefu\Documents\My Web Sites\Personal Site1\autopopulate.php on line 27

Warning: pg_query(): Query failed: ERROR: syntax error at or near "GGFF" LINE 1: ...no2=67677,Djointno=8,measuredlength=80,serialno=99GGFF,remar... ^ in C:\Users\iegbulefu\Documents\My Web Sites\Personal Site1\autopopulate.php on line 30

解决方案

I would do this by using Ajax and JQuery. You would need to do 3 things - Add Ajax/JQuery code in head of document, create php file for Ajax to connect to, and add onChange, id & value attributes to fields.

In your <head></head> add this javascript after your <title></title>-

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
function check(){   
    var pipeno = $('#pipeno').val();
    if(pipeno != "pipeno"){
      jQuery.ajax({
      type: "POST",
      url: "check.php",
      data: 'pipeno='+pipeno,
      cache: false,
      success: function(response){
     var response_array = JSON.parse(response);
     $('#wallthickness').val(response_array['wallthickness']);  
     $('#jointno').val(response_array['jointno']);
     $('#measuredlength').val(response_array['measuredlength']);
     $('#serialno').val(response_array['serialno']);}
    });
    }
    else{
         $('#wallthickness').val('');   
     $('#jointno').val('');
     $('#measuredlength').val('');
     $('#serialno').val('');}
    }
</script>

Then create a file named check.php with code -

<?php
 //Php Code to connect to postgresqldatabase
 include ("connection.php");
 // Code to pull data from the database and load onto the form  
 $pipeno = pg_escape_string($_POST['pipeno']);
 $query = "SELECT * FROM fieldtally WHERE pipeno = $pipeno ";
 $result = pg_query($db_handle,$query); 
 $row = pg_fetch_row($result))
 $row_info = array('wallthickness'=>$row[1],'jointno'=>$row[2],'measuredlength'=>$row[3],'serialno'=>$row[4]);

 $row_info = json_encode($row_info);
 print_r($row_info); 
?>

And finally, add id & value attributes to your form fields (Don't change your file, just update these lines)

Select Pipe No:<select name="pipeno" id="pipeno" onChange="check()"><option value="pipeno"> --Select-- </option> 
...  (keep your database connection / how you create your dropdown, etc here, just edit the option below)
 echo "<option value=\"$pipeno\"> $pipeno</option>";
... (keep your database connection / how you create your dropdown, etc. here)
</select> 
...
Input Joint No: <input type="text" name="jointno" id="jointno">
Input Wall Thickness: <input type="text" name="wallthickness" id="wallthickness">
Input measured Length: <input type="text" name="measuredlength" id="measuredlength">
Input Serial No: <input type="text" name="serialno" id="serialno">


UPDATED 10/19 Due to changed code -

I have put numbered anchors in your code, and the notes are at the end. You can do this all in 2 files, but the second file check1.php, can only have the 1 database query, or you will have errors in your ajax.

autopopulate.php-

<!-- #1 -->
<?php
//Php Code to connect to postgresqldatabase
$PGHOST = "localhost:25376";
$PGDATABASE = "Pipeline";
$PGUSER = "postgres";
$PGPASSWORD = "Casa2009";
$PGPORT = 5432;
$db_handle = pg_connect("dbname=$PGDATABASE user=$PGUSER password=$PGPASSWORD");

//<!-- #2 -->
 if(isset($_POST['submit_1'])){
 //Code to post data to the database
 $pipeno = pg_escape_string( $_POST['pipeno']);
 $wallthickness = pg_escape_string($_POST['wallthickness']);
 $heatno1 = pg_escape_string( $_POST['heatno1']);
 $pipeno2 = pg_escape_string( $_POST['pipeno2']);
 $heatno2 = pg_escape_string($_POST['heatno2']);
 $jointno = pg_escape_string($_POST['jointno']);
 $measuredlength = pg_escape_string($_POST['measuredlength']);
 $serialno = pg_escape_string($_POST['serialno']); 
 $wthick= pg_escape_string($_POST['wthick']);

//<!-- #3 -->
 $query = "INSERT INTO fieldtally1(pipeno,wallthickness,heatno1,pipeno2,heatno2,jointno,measuredlength,serialno,wthick)VALUES ('$pipeno','$wallthickness','$heatno1','$pipeno2','$heatno2','$jointno','$measuredlength','$serialno','$wthick') ON DUPLICATE KEY UPDATE wallthickness='$wallthickness',heatno1='$heatno1',pipeno2='$pipeno2',heatno2='$heatno2',jointno='$jointno',measuredlength='$measuredlength',serialno='$serialno',wthick='$wthick'";

//<!-- #4a -->
 $result = pg_query($query);
 if (!$result) {
 $errormessage = pg_last_error();
 $message = "Error with query: " . $errormessage;
 }
 $message = sprintf ("These values were inserted into the database - %s %s %s %s %s %s %s %s %s",$pipeno,$wallthickness,$heatno1,$pipeno2,$heatno2,$jointno,$measuredlength,$serialno,$wthick);
 }

 // Code to pull data from the database and load onto the form
 $query = 'select pipeno, wallthickness from fieldtally1 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
 //<!-- #5a -->
     $c = ($field_name == 'pipeno') ? ' onChange="check('.$num.');"' : '';
     echo "<select name=\"".$field_name."\" id=\"".$field_name.$num."\"$c>\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 Field Data Capture</title></head>
      <body>
       <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
       <script type="text/javascript">
 <!-- #5b -->
        function check(num){
           var pipeno_id = '#pipeno_'+num;
           var pipeno = $(pipeno_id).val();
           if(pipeno != ""){
             jQuery.ajax({
             type: "POST",
             url: "check1.php",
             data: 'pipeno='+pipeno,
             cache: false,
             success: function(response){
             var response_array = JSON.parse(response);
             $('#heatno1').val(response_array['heatno1']);
             $('#pipeno2').val(response_array['pipeno2']); 
             $('#heatno2').val(response_array['heatno2']);
             $('#jointno').val(response_array['jointno']);
             //$('#measuredlength').val(response_array['measuredlength']); // this should be input from the user
            // $('#serialno').val(response_array['serialno']);  //This should also be input from the user
            }           
            });
           }
           else{
           $('#heatno1').val('');
           $('#pipeno2').val('');
           $('#heatno2').val('');
           $('#jointno').val('');}
            }
  </script> 

 <!-- #4b -->
 <?php printf($message);?>
 <!-- #6.1 -->
 <form action="" method="post">
 <table width="800" 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 Thickness:<select name="wthick" id="wthick">
 <!-- #7.1 -->
  <option value=""> --Select-- </option> 
  <option value="9.8">  9.8  </option>
  <option value="13.5"> 13.5 </option>
  <option value="15.9"> 15.9 </option>
  </Select>           
 Select Pipe No:<?php dropdown('pipeno', 1); ?>  Select Wall Thickness:<?php dropdown('wallthickness', 1); ?><br /><br /> 
 HeatNo1: <input type="text" name="heatno2" id="heatno1"> PipeNo2: <input type="text" name="pipeno2" id="pipeno1"> HeatNo2: <input type="text" name="heatno2" id="heatno2"><br /><br /> 
 Joint No: <input type="text" name="jointno"> Input measured Length: <input type="text" name="measuredlength"> Input Serial No: <input type="text" name="serialno"><br><br> 
 <!-- #8.1 -->
 <input type="Submit" name="submit_1" value="Submit">
 <!-- #9.1 -->
 </td></tr></table></form>
 <p></p>

 <!-- #6.2 -->
 <form action="" method="post">
 <table width="800" 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 Wall Thickness:<select name="wallthickness" id="wallthickness">
 <!-- #7.2 -->
     <option value=""> --Select-- </option> 
     <option value="9.8">  9.8  </option>
     <option value="13.5">13.5 </option>
     <option value="15.9"> 15.9 </option>
  </select>
 <!-- #10.1 -->           
 Select Pipe No:<?php dropdown('pipeno', 2); ?>  Select Wall Thickness:<?php dropdown('wallthickness', 2); ?><br /><br /> 
 HeatNo1: <input type="text" name="heatno1" id="heatno1_2"> PipeNo2: <input type="text" name="pipeno2" id="pipeno2_2"> HeatNo2: <input type="text" name="heatno2" id="heatno2_2"><br /><br /> 
 Joint No: <input type="text" name="jointno"> Input Measured Distance: <input type="text" name="measureddistance"><br><br> 
 Input Bend Angle: <input type="text" name="benddegree"> Input Bend Type: <input type="text" name="bendtype"><br><br>
 <!-- #8.2 -->
 <input type="Submit" name="submit_2" value="Submit">
 <!-- #9.2 -->
 </td></tr></table></form>
 <p></p>

 <!-- #6.3 -->
 <form action="" method="post">
 <table width="800" cellpadding= "10" cellspacing="1" border="2">
 <tr align="center" valign="top">
 <td align="center" colspan="1" rowspan="1" bgcolor="#66CC66">
 <h3>Input App. Tally Information</h3>
 <!-- #11 -->
      Select Wall Thickness:<select name="wallthickness1" id="wallthickness1">
 <!-- #7.3 -->
     <option value=""> --Select-- </option> 
     <option value="9.8">  9.8  </option>
     <option value="13.5"> 13.5 </option>
     <option value="15.9"> 15.9 </option>
  </select>
 <!-- #10.2 -->             
 Select Pipe No:<?php dropdown('pipeno', 3); ?>  Select Wall Thickness:<?php dropdown('wallthickness', 3); ?><br /><br />            
 Input Tally 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>
 <!-- #8.3 -->
 <input type="Submit" name="submit_3" value="Submit">
 </td></tr></table>
 </form>
 </body>
 </html>

check1.php-

<?php
//Php Code to connect to postgresqldatabase
$PGHOST = "localhost:25376";
$PGDATABASE = "Pipeline";
$PGUSER = "postgres";
$PGPASSWORD = "Casa2009";
$PGPORT = 5432;
$db_handle = pg_connect("dbname=$PGDATABASE user=$PGUSER password=$PGPASSWORD");

// Code to pull data from the database and load onto the form  
$pipeno = pg_escape_string($_POST['pipeno']);
$query = "SELECT * FROM fieldtally1 WHERE pipeno = $pipeno ";
$result = pg_query($db_handle,$query); 
$row = pg_fetch_row($result);
$row_info = array('heatno1'=>$row[1],'pipeno2'=>$row[2],'heatno2'=>$row[3],'jointno'=>$row[4]);
$row_info = json_encode($row_info);
print_r($row_info); 
?>

Here are the issues-

1 - Move all <?php ?> scripts to the top of the page. This will: (1) clean up your code, (2) be able to process all database queries at the same time.

2 - Your query to update fieldtally1 is now done before you get your dropdowns from the database.

3 - To fix your second issue after entering data into the input fields and click submit. it creates a new record in the database instead of populating the required columns in the database use INSERT INTO ... VALUES ... ON DUPLICATE KEY UPDATE .... If pipeno is already in the database (since it is a Primary Key) it will UPDATE instead of INSERT.

4 - I recommend saving your error or success message (#4a) and echo it at the top of your html (#4b).

5a - You changed from a hard coded pipeno dropdown, back to the dynamically dropdown using the function. So you are missing the javascript - onChange=check(); 5b - now that you are creating your pipeno & onChange=check(); dynamically, you have to change your check() function to get the id dynamically as well.

6 - $PHP_SELF is not valid. I think you were trying for $_SERVER['PHP_SELF'], but this is easily hacked, so it is better just to use action="". [6.1,6.2,6.3]

7 - When doing your <select><option> you were using the same value="", but each one needs to be different for you to get the value when posting. Also, most </option>'s were misspelled as </optio>. [7.1,7.2,7.3]

8 - If you have 3 forms on the same page, each one has to have a different name, or you will not be able to tell which submit button was clicked. [8.1,8.2,8.3]

9 - Your first two forms were missing closing tags - </td></tr></table></form>. [9.1, 9.2]

10 - In you 2 & 3 forms, you are using id's that are used in the 1st form. This is invalid as every id must be unique. [10.1,10.2]

11 - 3rd form uses id="wallthickness" that was used in form 2. see above about id uniqueness.

这篇关于PHP\HTML脚本,用于从下拉列表中选择项目时自动填充表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆