Professional Documents
Culture Documents
Web Programming ct214H Lab4 php2
Web Programming ct214H Lab4 php2
v Requirement:
1) Create a webpage to add a new song review, as shown in Figure 24.
2) Field “mã bài viết” will be assigned a next value when the form is shown (the
current max value + 1).
3) The field “ngày viết” will also assign the current date as the default value.
4) (Extra) Validate the form data before submitting it to the web server (e.g.,
the fields must not be empty). In addition, if the song name is empty, use the
review title for this field.
2) The default value for “Mã bài viết” will be calculated on the server side:
• Use SQL to select the max value of the review id.
• Compute new review id = max value + 1.
• We should write a function to handle this task (e.g., write a function
named get_ma_bviet()) and save it in a separate PHP file.
• To display the review id in the text field: use the echo statement
<script type="text/javascript">
var d = new Date();
f_add.ngayviet.value = d.getFullYear() + "/"
+ (d.getMonth()+1) + "/" + d.getDate();
</script>
v Hint: Students see the example Title-Manager in Chapter 5.1 to recap how to
create a form for deleting data.
1) This requirement can be fulfilled based on the result of Ex 5, Lab3, with the
following modification:
• For each review, besides the current information as in Ex 5, Lab 4, we add
a new form (child form) which contained a button “Xóa bài viết”. When
the delete button is clicked, the form containing this button will be
submitted to the server to process the request.
Ex 3. (Extra) Modify the adding review page so that the author name and genre of the
song can be selected in a dropdown box. In addition, the post date can also be
selected in a calendar.
v Objective:
1) Using the combobox.
2) Increase the user-friendly of the webpage.
3) Create JS code in HTML page by PHP.
v Hint:
1) Use a dropdown list (<select> tag) for getting reviewer names instead of a
textfield in Ex 1. The structure of the dropdown list for selecting the reviewer
name is as follows:
Tác giả <select name="ma_tgia">
<option value='1'>Nhacvietplus</option>
<option value='2'>Sưu tầm</option>
. . .
<option value='8'>Tâm tình</option>
</select>
The options inside the <select> tag will be selected from table tacgia with
the value property from field ma_tgia, and the tag's content got from field
<select name=”ma_tgia”>
<?php
//tạo các options:
// nối kết CSDL, truy vấn dữ liệu
// với mỗi mẩu tin, xuất ra 1 thẻ option tương ứng
while ($row = mysql_fetch_array($q_result)) {
echo "<option value='", $row["ma_tgia"], "'>";
echo $row["ten_tgia"], "</option>";
}
?>
</select>
We can save the code for generating the options as a function (with 3
parameters: table name, the field used for value property, and the field used
for the content of the option) so that we can reuse it for other dropdown lists
(e.g., the 2nd requirement of this exercise).
2) Similar to the 1st requirement.
3) Use a JS date picker, e.g., the DatePicker (http://jqueryui.com/datepicker/).
This library can be downloaded at http://bit.ly/1rY2kWn (includes 3 JS files
and 1 CSS file). To use this library, we do the following steps:
• Put the following code in the head section of the page to include the CSS
files in the HTML page:
<link rel="stylesheet"
href="jquery-datepick/smoothness.datepick.css">
<script src="jquery-datepick/jquery-1.10.2.js"></script>
<script src="jquery-datepick/jquery.plugin.js"></script>
<script src="jquery-datepick/jquery.datepick.js"></script>
• Attach the datepicker to review the date textfield using the following JS
code written in jQuery syntax (also put it in the head section):
<script>
$(function() {
$("#ngayviet").datepick({dateFormat: 'yyyy/mm/dd'});
});
</script>
Ex 4. (Extra) Add the pagination for the review search page in Ex 5 or Ex 6, Lab 3, so
each page shows at most 5 reviews.
v Hint: See Chapter 4.2 – Advanced PHP, pagination part.