Just a quick preface, I'm not a web developer. I'm simply doing this as a favor for a friend. My goal is to be done with it as quickly as possible, but still not have the coding be horrendous. With that said, as I was coding this, my spidey senses were going off telling me that what I was doing was really bad programming practice and I was wondering if there are any quick fixes I can make to make it more efficient.
I have a form I need to run, but I wanted to give the user the option to choose the number of fields, so I've embedded a lot of PHP code within the body of my HTML files with if/else statements. The problem with this is that it's preventing my Javascript/CSS code in the header to run properly, so I've had to embed them within the body of the html. The code seems to be running a bit slow and probably not as neat as it should be. The code is kind of long, so I removed certain sections that were irrelevant to the discussion. I left a comment in their place to explain their function. I also need to add another block of PHP code that processes the form and enters them in the MySQl database. Any help is appreciated!
<html>
<head>
<title></title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="jquery.maskedinput-1.3.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#cash-amount").hide();
$("#transfer-amount").hide();
$(".date").mask("9999-99-99");
$("#cash_yes").click(function(){
$("#cash-amount").show();
});
$("#cash_no").click(function(){
$("#cash-amount").hide();
});
$("#trnsfr_yes").click(function(){
$("#transfer-amount").show();
});
$("#trnsfr_no").click(function(){
$("#transfer-amount").hide();
});
});
</script>
</head>
<body>
<?php
if(isset($_POST['bill_submit'])){
?>
<form id="expenses" method="post" action="<?php echo htmlentities($PHP_SELF); ?>">
<?php
if($_POST['CC'] != 0)
{
?>
<table>
<caption>Credit Cards</caption>
<tr>
<th></th>
<th>CC # (last four digits)</th>
<th>Amount</th>
<th>Approval</th>
<th>Date</th>
</tr>
<tbody>
<?php
for ($i = 1; $i <= $_POST['CC']; $i++)
{
?>
<tr>
<td> <?php echo $i; ?> </td>
<td> <input type="text" name="cc_num[]" maxlength="4" size="4" /> </td>
<td> <input type="text" name="cc_amnt[]" size="10"/> </td>
<td> <input type="text" name="cc_app[]" maxlength="10" size="10" /> </td>
<td> <input class="date" type="text" name="cc_date[]" size="10" /> </td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
}
if($_POST['DB'] != 0)
{
?>
<table>
<caption>Direct Bills</caption>
<tr>
<th></th>
<th>DB #</th>
<th>Amount</th>
<th>Date</th>
</tr>
<tbody>
<?php
for ($i = 1; $i <= $_POST['DB']; $i++)
{
?>
<tr>
<td> <?php echo $i; ?> </td>
<td> <input type="text" name="db_num[]" size="10"/> </td>
<td> <input type="text" name="db_amnt[]" size="10"/> </td>
<td> <input class="date" type="text" name="db_date[]" size="10" /> </td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
}
if($_POST['misc'] != 0)
{
?>
<table>
<caption>Miscellaneous</caption>
<tr>
<th></th>
<th>Item</th>
<th>Amount</th>
<th>Date</th>
</tr>
<tbody>
<?php
for ($i = 1; $i <= $_POST['misc']; $i++)
{
?>
<tr>
<td> <?php echo $i; ?> </td>
<td> <input type="text" name="misc_item[]" size="10" /> </td>
<td> <input type="text" name="misc_amnt[]" size="10" /> </td>
<td> <input class="date" type="text" name="misc_date[]" size="10" /> </td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
}
?>
<div id="cash-check">
Are you turning in any checks or cash today? <input type="radio" name="cash" value="Yes" id="cash_yes">Yes <input type="radio" name="cash" value="No" id="cash_no">No</br>
<div id="cash-amount">
Enter Amount: <input type="text" name="cash_amnt" id="cash_amnt" size="10" value="0.00">
</div>
</div>
<div id="transfer">
Would you like to transfer any money to another driver? <input type="radio" name="trnsfr" value="Yes" id="trnsfr_yes">Yes <input type="radio" name="trnsfr" value="No" id="trnsfr_no">No</br>
<div id="transfer-amount">
Enter Driver Number: <input type="text" name="transfer_amnt" id="transfer_amnt" size="10">
Enter Amount: <input type="text" name="transfer_amnt" id="transfer_amnt" size="10" value="0.00">
</div>
</div>
<style type="text/css">
#cash-amount
{
text-indent:50px;
}
#transfer-amount
{
text-indent:50px;
}
</style>
<script type="text/javascript">
$("#cash_yes").click(function(){
$("#cash-amount").show();
});
$("#cash_no").click(function(){
$("#cash-amount").hide();
});
$("#trnsfr_yes").click(function(){
$("#transfer-amount").show();
});
$("#trnsfr_no").click(function(){
$("#transfer-amount").hide();
});
</script>
<input type="submit" value="Submit" name="submit" />
</form>
<?php
}
else
{
?>
<div>
Please indicate below how many credit cards, direct bills, and miscellaneous items you have to enter into the system. Note that you will have to start over if you enter the numbers incorrectly.
</div>
</br>
<fieldset>
<legend>Numer of Items</legend>
<form id="past_record" method="post" action="<?php echo htmlentities($PHP_SELF); ?>">
Credit Cards: <select name="CC">
<!--List of options from 0-20!-->
</select>
Direct Bills: <select name="DB">
<!--List of options from 0-20!-->
</select>
Miscellaneous: <select name="misc">
<!--List of options from 0-20!-->
</select>
<input type="submit" value="Submit" name="bill_submit" />
</form>
</fieldset>
</div>
<?php
}
?>
</body>
</html>