Another Question: this one on combining php and jquery
So, I have another question about my form. I'm pretty good with PHP but very new with jquery. Can I combine the two? In the following code you'll see I populate a select field with data from a database. When the user clicks the Add another course link it puts new fields below the first two (I think this is kinda neat) - but I'm not sure how to combine the php with the jquery so that the value of the select field array is +1 (for processing on the php script) and also so it shows the values from the database and doesn't have to be hard-coded into the select fields created by the jquery.
I tried putting the <script> into php by echoing each line but when I tried that it wouldn't work at all. I just echoed it exactly as it's written down (except for escaping the double quotes in the jquery of course).
-
<?php
include("connection.php");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$('.add').click(function () {
var n = $("div").length;
$('.course1').append($("<div><p><label>Course: </label><select name='course[" +n+ "]'><option value='select course'>Select course</option><option value='course1'>Course 1</option><option value='course 2'>Course 2</option></select></p><p><label>Department: </label><input type='text' name='dept[" +n+ "]'></p></div>"));
})
});
</script>
<style>
body { cursor:pointer; }
span { color:red; }
</style>
</head>
<body>
<?php
$option_block1 = "<select name=\"course[0]\"><option value=\"Select Course\">Select Course</option>";
$sql = "SELECT * FROM catalog";
$results = mysql_query($sql,$connection);
while ($row = mysql_fetch_array($results, MYSQL_ASSOC)) {
$course_title = $row['title'];
$option_block1 .= "<option value='$course_title'>$course_title</option>";
}
$i = 0;
echo "<a href=\"#\" class=\"add\">+ Add A Course</a>
<form class=\"regform\" action=\"regform-process.php\" method=\"post\">
<div class=\"course1\"><p><label>Course: </label>$option_block1
</select></p>
<p><label>Department: </label><input type=\"text\" name=\"dept[" .$i. "]\"></p></div>
<p><input type=\"submit\"></p>
</form>";
?>
</body>
</html>