将数据从MySQL数据库加载到下拉列表中的文本框单击
i got this code
<?php
include("konek.php");
mysql_connect('localhost', 'root', '');
mysql_select_db('psc_db');
$sql = "SELECT * FROM komponen ORDER BY komponen_id";
//$query = "SELECT * FROM taxi_services ORDER BY SID";
$result2 = mysql_query($sql); //$conn
if(isset($_POST["loadbtn"]))
{
$id = (integer) $_POST["codes"];
$query = "SELECT nama_komponen, ekuivalen_sks, ekuivalen_jam FROM komponen WHERE komponen_id = '$id' ";
$result = mysql_query($sql);
$details = mysql_fetch_array($result);
$savedName = $details["nama_komponen"];
$savedJam = $details["ekuivalen_sks"];
$savedSks = $details["ekuivalen_jam"];
}
?>
<html>
<head>
</head>
<body>
<div id="upserv">
<b id="caption2">Planning</b>
<br/><br/>
<form name="upServForm" method="post" >
<?php
$dropdown = "<select name='codes'>";
while($row = mysql_fetch_assoc($result2))
{
$dropdown .= "
<option value='{$row['komponen_id']}'>{$row['komponen_id']}</option>";
}
$dropdown .= "
</select>";
?>
Service ID <?php echo $dropdown; ?> <input type="submit" value="Load" name="loadbtn">
<table width="300" border="0">
<tr>
<td>Name</td>
<td><input type="text" name="upName" style="text-align:right" value="<? echo $savedName; ?>" /></td>
</tr>
<tr>
<td>ekuivalen sks</td>
<td><input type="text" name="upCost" style="text-align:right" value="<? echo $savedJam; ?>" /></td>
</tr>
<tr>
<td>ekuivalen jam</td>
<td><input type="text" name="upActive" style="text-align:right" value="<? echo $savedSks; ?>" /></td>
</tr>
</table>
</div>
<br/>
<div id="buttons">
<input type="reset" value="Clear" /> <input type="submit" value="Save" name="updatebtn" />
</div>
</form>
</body>
</html>
and the result is something like this
I still haven't sucessfully display the data when I click the button. I got the example code from another post, and I tried it and above is the result(but I'm still fail). All I want to do is to display the data into the textbox without the load button
Can you help me coding this code please, thank you
</div>
I think short tags is not configured in your server.
you have to replace <?
with <?php
I think that you must stop using PHP short tags( <? ?>
) or in case that you need or are used to use the enable it on the php.ini file on your server.
The thing that happens is that the php handler does not recognize the code inside the value attribute of the input because of the short tag and does not evaluate it. That's all, enable short tags and you can do some stuff like <?=
for <? echo
, nice...
In your php.ini file,
short_open_tag stands for the opening and closing <? ?>
notation.
asp_tags stands for <% %>
notation
I have created this to simulate your problem
<?php
if ($_POST['btnsubmit']){
echo $_POST['codes'];
}
?>
<html>
<body>
<form method="post">
<select name="codes">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<input type="submit" name="btnsubmit" value="send"/>
</form>
</body>
</html>
And works as expected, other thing is that if you want to keep track of the selected option on the select input you have to add a conditional selected clause to the options of the input that prints the selected="selected" attribute on the option based on the previous submitted value.