简体   繁体   中英

Echo Field Value In Text Field

I wonder whether someone may be able to help me please.

Through some guidance I received earlier from this site I have been able to put together the script, below that lists date information with an associated radio button allowing each record to be selected.

<?php

mysql_connect("hostname", "username", "password")or
die(mysql_error());
mysql_select_db("database");
?>
<html><head> 
<title>Location</title>
<style type="text/css">
<!--
.style3 {
    font-size: 18px;
    font-family: Calibri;
}
-->
</style>
</head>
<form name="addimages">
  <p>
    <label>
    <div align="center" class="style3"> Location Name <br>
      <br>
      <input name="locationname" type="text" value="<?php echo $locationname;?>" id="locationname" size="80">
    </div>
    </label>
  </p> 
  <p>&nbsp;</p>
  <p>
    <?php

$result = mysql_query("SELECT userdetails.userid, finds.dateoftrip, finds.userid, finds.locationid, detectinglocations.locationid, detectinglocations.locationname,  finds.findname, finds.finddescription FROM userdetails, finds, detectinglocations WHERE finds.userid=userdetails.userid AND finds.locationid=detectinglocations.locationid AND finds.userid = 1 ORDER BY dateoftrip DESC");
if (mysql_num_rows($result) == 0)
// table is empty
  echo 'There are currently no finds recorded for this location.';
 else
{

  while (list($userid, $dateoftrip, $locationname) =
    mysql_fetch_row($result))

  {  
      echo"<table>\n"; 
      echo"<tr>\n"
    .
     "<td><input type='radio' name='radio' dateoftrip value='{$userid}' /></td>\n"
    ."<td><small>{$dateoftrip}</small><td>\n"
    ."</tr>\n";
  }
  echo"<tr><td colspan=\"3\">";
  echo"<br>";
  echo"</td></tr>";
  echo'</table>';
}
?>
</p>
</form>

What I'm now trying to do is add the 'locationname' value to the appropriate text field.

I've added value="<?php echo $locationname;?>" to the textfield but it remains blank.

I think that the problem lies here:

while (list($userid, $dateoftrip, $locationname) = mysql_fetch_row($result)) which I use to get the information for the list of dates, whilst normally I would use this:
while ($row = mysql_fetch_array($result)) { $locationname = $row['locationname']; to fetch the value for a text field.

Could someone perhaps tell me please is there a way that I can combine the two statements to bring together the two pieces of information I need ie the table containing the list of dates whilst 'echoing' the 'locationname' value into the text field.

Many thanks

UPDATED CODE

<?php
mysql_connect("hostname", "username", "password")or
die(mysql_error());
mysql_select_db("database");

$locationname=''; 
$result = mysql_query("SELECT userdetails.userid, finds.dateoftrip, finds.userid, finds.locationid, detectinglocations.locationid, detectinglocations.locationname,  finds.findname, finds.finddescription FROM userdetails, finds, detectinglocations WHERE finds.userid=userdetails.userid AND finds.locationid=detectinglocations.locationid AND finds.userid = 1 ORDER BY dateoftrip DESC");
if (mysql_num_rows($result) == 0){
// table is empty
  $return = 'There are currently no finds recorded for this location.'; 
  }else{     
  $return="<table>\n";     
  while (list($userid, $dateoftrip, $locationname) = mysql_fetch_row($result)){ 

$return .="<tr>\n". 
"<td><input type='radio' name='radio' dateoftrip value='{$userid}' /></td>\n" 
."<td><small>{$dateoftrip}</small><td>\n" 
."</tr>\n"; 
} 
$return .="<tr><td colspan=\"3\"><br>"; 
} 
?>
<form name="addfinds">

      <input name="locationname" type="text" value="<?php echo $locationname;?>" id="locationname" size="80">   
  <p>&nbsp;</p> 
  <p> 
    <?php echo $return;?> 
</p> 
</form> 

您试图在变量被分配之前对其进行回显,移动您的 while 循环以将其包含在其中。

Basically your attempting to use it before setting it, if your had error_reporting on you would see the error. What you need toto is instead of using echos within your while loop, you should build the output and move the code above the input text. Something like this:

<?php
...
$locationname='';
$result = mysql_query("SELECT ...");
if (mysql_num_rows($result) == 0){
    $return = 'There are currently no finds recorded for this location.';
}else{
    $return="<table>\n";
    while (list($userid, $dateoftrip, $locationname) = mysql_fetch_row($result)){

        $return .="<tr>\n".
        "<td><input type='radio' name='radio' dateoftrip value='{$userid}' /></td>\n"
        ."<td><small>{$dateoftrip}</small><td>\n"
        ."</tr>\n";
    }
    $return .="<tr><td colspan=\"3\"><br>";
    ...
    ...
}

?>
<html><head> 
<title>Location</title>
...
...
      <input name="locationname" type="text" value="<?php echo $locationname;?>" id="locationname" size="80">
... 
  <p>&nbsp;</p>
  <p>
    <?php echo $return;?>
</p>
</form>

I guess, you should change while loop like this:

while($row = mysql_fetch_row($result)) {
  list($userid, $dateoftrip, $locationname) = $row;

  // table
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM