使用单选按钮在多个选项之间进行选择后预填充表单字段


pre-populating a form field after choosing between many options using a radio button

我已经搜索了整个网络,我似乎找不到解决方案。我基本上是在数据库中搜索个人。例如,我可以使用姓氏,然后如果两个人有相同的姓氏,他们都将在我的页面上打印。我希望能够选择我想要的个人(或我为例),然后点击下一步(一个超链接到一个空白表格的页面),这是我想我选择的个人的信息(和什么是打印在页面上)在预填写的表格。它允许更用户友好的方法,而不是重新输入数据,也避免了公司名称或驾驶执照等拼写错误。我已经把我的代码多次,但这里再次。这是用于查找和打印搜索结果的代码。还没有单选按钮,我正在努力弄清楚。

<div id="results" style="width:750px;height:225px; text-align: center">
<?php
$host = "localhost"; //server
$db = ""; //database name
$user = ""; //databases user name
$pwd = ""; //password
mysql_connect($host, $user, $pwd) or die(mysql_error());
mysql_select_db($db) or die(mysql_error()); 
$searchTerm = trim($_GET['searchname']);
// Check if $searchTerm is empty
if($searchTerm == "")
{
echo "Enter name you are searching for.";
exit();
} 
else 
{
    //$sql = "SELECT * FROM contractor WHERE CONCAT(FIRSTNAME,' ',LASTNAME,' ',    
  ARRIVAL) like '%$searchTerm%' GROUP BY FIRSTNAME";
$sql= "SELECT  * FROM contractor WHERE CONCAT(FIRSTNAME,' ',LASTNAME,' ', ARRIVAL)   
 like '%$searchTerm%'";
    $query = mysql_query($sql);
    $count=mysql_num_rows($query);
//array_unique($count);
    if(($count)>=1)
    {
    $output = "";
    while($row = mysql_fetch_array($query))
    {
    $output .= "First Name: " . $row['FIRSTNAME'] . "<br />";
    $output .= "Last Name: " . $row['LASTNAME'] . "<br />";
    $output .= "Arrival: " . $row['ARRIVAL'] . "<br />";    
    }
    echo $output; 
    //echo array_unique($output);
    }
    else
    echo "There was no matching record for the name " . $searchTerm;
}
?>
</div>  

这是我的表单字段的代码,当然是一个空表单,提交后刚刚提交给数据库。

 <form action="insert_submit.php" method="post" style="margin-left:35px;">
First Name: <input type = "text" name="FIRSTNAME" id="FIRSTNAME" />

Last Name: <input type = "text" name="LASTNAME" id="LASTNAME"/>

Purpose: <input type = "text" name="PURPOSE" id="PURPOSE"/>

<br>
</br>
Company: <input type = "text" name="COMPANY" id="COMPANY" />

DL #: <input type = "text" name="DRIVERL" id="DRIVERL" />

 <br>
</br>
    <input type="radio" name="STATUS" id="STATUS" value="Checked In">Log In
    <br></br>
    <input type="radio" name="STATUS" id="STATUS" value="Checked Out">Log Out
    <br></br>
<input type="submit" value="Submit" >
<br>
</br>
</form> 

所以你的输出行应该是这样的(清理适合你的格式)

$output .= '<a href="'.$_SERVER['PHP_SELF'].'?userid='.$row['UNIQUEID'].'">
      First Name: '.$row['FIRSTNAME'].'<br />
      Last Name: '.$row['LASTNAME'].'<br />
      Arrival: '.$row['ARRIVAL'].</a><br />';    

然后在页面中建立一个条件如果你有UNIQUEID来查找和填写表单比如

Company: <input type = "text" name="COMPANY" id="COMPANY" value="<?PHP echo $row['COMPANY']; ?>" />

编辑以适应您的数据库当然

示例代码格式

if(something){
    do something
}else{
   while(){
   } # end while
}  # easy to see this matches the if/else set of brackets

预填充

的附加信息

最简单的可能是在同一页上。查找进入页面的用户id

if(!isset($_GET['userid'])){ 
     # meaning we do not have a userid from a url that we generated above
     # insert all your code to generate the links
}else{
     # meaning we *have* a userid from a url that we generated above
     # do your database query based on the userid such as
   $sql='select * from mytable where userid=?';
   $result=sqlsrv_query($db,$sql,array($_GET['userid']),array('Scrollable'=>'static'));
     # or use mysqli, pdo or whatever...  just watch for sql injection
   $row=sqlsrv_fetch_array($result,SQLSRV_FETCH_ASSOC));
     # assuming we will not have more than one element returned
   ?>
   First Name: <input type = "text" name="FIRSTNAME" id="FIRSTNAME" value="<?PHP echo $row['firstName']; ?>"/>
    and so forth...  that should get you started

所以基本上你需要的是在第一页列出相同的名字,或者最好是一个名字,比如:

内容,内容,内容,

FirstName、LastNameFirstName、LastName

然后,如果您单击其中任何一个,那么用户将被重定向到另一个页面,该页面在表单中包含所选人员的所有详细信息。如果我没弄错的话。

  1. 我不会在第一页上使用复选框,只是把人的名字做成一个超链接,比如:http://domain.com/userdetails?userid=666

将用户id传递给第二页。第二页上的php代码可以从数据库中提取用户详细信息并填充表单。

为此,您可以以逐行echo的方式生成表单,或者使用DomDocument来查找并填充相关的值字段。

第二页表单生成:选择具有该id的行,然后将其获取到一个名为$array的数组中。

$array = mysql_fetch_array($query); //this query selected the row for the user with the id
echo '<form method="POST">'."'n";
echo '<input name="firstname" type="text" value="'.$array['firstname'].'">'."'n";
echo '<input name="lastname" type="text" value="'.$array['lastname'].'">'."'n";
echo '</form>';

希望你现在明白了。