I’m new to PHP and MySQL and I am working on an Add to Blacklist page for a Car Rental web based system.
This page entails that I get the data of the Company from the Company Table then if I choose to, I can add it to the Blacklist Table.
My method of thinking was that I can use a list box to get the name of the company in the Company Table then either click a button (Get Details) beside the list box to auto-fill the Text Fields with the Company details. I have labelled them below on the page.
or
(Also I was thinking maybe when the user clicks the company name in the list box it would then auto-fill the Fields with the Company details.)
Then after I get the details to fill the Text Fields I can then add them to the Blacklist Table.
So far I have the page retrieving the Name from the Company table but I can’t get it to fill the Text Fields.
My code is :
//HTML
<body>
<script>
function companyList()
{
var sel = document.getElementbyId("getCompanyList");
var result;
result = sel.options[sel.selectedIndex].value;
var companyDetails = result.split(', ');
document.getElementbyId("companyID").value = companyDetails[0];
document.getElementbyId("companyName").value = companyDetails[1];
document.getElementbyId("Address").value = companyDetails[2];
document.getElementbyId("currentBal").value = companyDetails[3];
document.getElementbyId("amountDue").value = companyDetails[4];
document.getElementbyId("phoneNo").value = companyDetails[5];
}
</script>
<div id="main"> <!--DIV FOR MAIN BODY OF PAGE-->
<form method="POST" >
<legend align ="center">Add to Blacklist</legend> <!--HEADING TO PAGE-->
<div id="image" align="center">
<img src="logo.png" style="width:550px;height:400px"><br>
<!--ALIGNS AND PUTS LOGO-->
label>Choose a Company
<?php include "blackbox.php";?>
</div> <!--CLOSING OF LOGO DIV-->
</form>
</div> <!--CLOSING OF MAIN BODY DIV-->
<div id="Container">
<div id ="Input" align= "center">
<label>Company ID:
<input type = "text" name="companyID" id="companyID" placeholder = "Company ID" disabled required />
</label>
<label>Company Name:
<input type = "text" name="companyName" id="companyName" placeholder = "Company Name" disabled required /></label>
<label>Company Address:
<input type = "text" name="Address" id="Address" placeholder = "Address" disabled required /></label>
<label>Current Balance:
<input type = "int" name="currentBal" id="currentBal" placeholder="Current Balance" disabled required /></label>
<label>Amount Due:
<input type = "text" name="amountDue" id="amountDue" placeholder = "Amount Due" disabled required /></label>
<label>Phone Number:
<input type = "int" name="phoneNo" id="phoneNo" placeholder = "Phone Number" disabled required /></label>
<br><br>
<p>
<input type="submit" value="Add to Blacklist"/>
<input type = "reset" value="Clear"/>
<br>
<br>
</div>
</p>
</form>
</div>
</body>
</html>
//PHP
<?php
include "db.inc.php"; //DATABASE CONNECTION
$sql = "SELECT CompanyID, CompanyName, Address, CurrentBalance, AmountDue, PhoneNum FROM Company";
if(!$result = mysql_query($sql,$con))
{
die('Error in querying the database' . mysql_error());
}
echo "<br><select name = 'getCompanyList' id = 'getCompanyList' onclick = 'companyList()'>";
while ($row = mysql_fetch_array($result))
{
$id = $row['CompanyID'];
$cname = $row['CompanyName'];
$address = $row['Address'];
$currBal = $row['CurrentBalance'];
$amount = $row['AmountDue'];
$phone = $row['PhoneNum'];
$allText = "$id,$cname,$address,$currBal,AmountDue,PhoneNum";
echo "<option value = '$allText'>$cname</option>";
}
echo "</select>";
mysql_close($con);
?>
What I want to do is:
- Use list box to get names of company’s from company table (this I have achieved)
- After picking the company, use the selected value in the list box to get all the details for that company and display them in the input boxes that I have laid out.
Is this feasible? Can you help please?