使用php和html,我有一個表單用於輸入屬性數據到sql數據庫中,屬性的每個主鍵都有一個名爲'property_id'的主鍵,每當它自動生成通過表單添加新的屬性。我創建了另一個表單,以便不插入新的屬性數據,而是更新現有的屬性數據,下面的代碼就是該表單。基於選擇字段的值自動填充表單字段
<fieldset>
Select Property:<br>
<select required name="id">
<option value"">Select a property</option>
<?php
//Database connection
(It's there but I've removed it for this question)
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = $conn->query("select * FROM property");
while ($row = $result->fetch_assoc()) {
unset($id, $unit, $street, $suburb);
$id = $row['id'];
$unit = $row['unit'];
$street = $row['street'];
$suburb = $row['suburb'];
echo "<option value='$id'>$unit $street $suburb</option>";
}
echo "</select>"; ?>
<br />
Unit:<br>
<input type="number" name="unit" required value="<?php echo $unit;?>">
<br />
Street:<br>
<input type="text" name="street" required value="<?php echo $street;?>">
<br />
Suburb:<br>
<input type="text" name="suburb" required value="<?php echo $suburb;?>">
<br />
Postcode:<br>
<input type="number" name="postcode" required value="<?php echo $postcode;?>">
<br />
State:<br>
<select required name="state">
<option value="">Please Select</option>
<option value="ACT">ACT</option>
<option value="NSW">NSW</option>
<option value="NT">NT</option>
<option value="QLD">QLD</option>
<option value="SA">SA</option>
<option value="TAS">TAS</option>
<option value="VIC">VIC</option>
<option value="WA">WA</option>
</select>
<br />
Employee ID:<br>
<input type="number" name="employee" required value="<?php echo $employee;?>">
<br />
Owner ID:<br>
<select required name="property">
<option value"">Select an owner</option>
<?php
//Database connection
(It's there but I've removed it for this question)
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = $conn->query("select owner_id, first_name, last_name FROM property_owner");
while ($row = $result->fetch_assoc()) {
unset($id, $unit, $street, $suburb);
$id = $row['owner_id'];
$fName = $row['first_name'];
$lName = $row['last_name'];
echo "<option value='$id'>$fName $lName</option>";
}
echo "</select>"; ?>
<br />
Type of Property:<br>
<input type="radio" name="type" value="Apartment" required checked>Apartment<br />
<input type="radio" name="type" value="Town House">Town House<br />
<input type="radio" name="type" value="House">House<br />
<br />
Rent:<br>
<input type="number" name="rent" value="<?php echo $rent;?>"><br />
Bedroom:<br>
<input type="number" name="bedroom" value="<?php echo $bedroom;?>"><br />
Bathroom:<br>
<input type="number" name="bathroom" value="<?php echo $bathroom;?>"><br />
Furnished:<br>
<input name="furnished" type="radio" value="1" checked>Yes<br>
<input name="furnished" type="radio" value="0" checked>No<br>
Property Description:<br>
<textarea name="description" cols="50" rows="15"></textarea>
<br />
<p></p>
<input type="submit" name="SUBMIT" value="Submit">
我一直在關注W3學校的教程,並從其他stackflow問題中抽取一些小部分。我想知道的是,使用頂部的下拉菜單(選擇屬性,然後是動態ID屬性列表),我如何根據sql數據庫中現有的值填充不同的表單域?
XSS(Cross Site Scripting)ALERT !! –