View: 2864|Reply: 9
|
Sample Web PHP programming
[Copy link]
|
|
Salam semua,
ai baru belajar PHP
so ai nak buat programming web php , untuk my kerjalah
begini, ai kena buat 2 page. yang ai bermasalah adalah sebegini
1) page satu
key in data.
ada 2 field. iaitu field negeri dan keduanya adalah field daerah
lepas tu data ini akan dihantar ke mysql dan disimpan lah
jadi mcm mana ai nak buat kalau, bila dia point ke negeri lepas tu dia pilih negeri selangor,
then field daerah itu hanya akan memaparakan daerah2 yang hanya terdapat di negeri selangor?
---buat masa ni ai tanya page 1 dulu ------- |
|
|
|
|
|
|
|
cara 1...
-lepas select negeri send form....
-kat daerah tu set filter utk daerah dari negeri yg ko pilih...
cara2.. guna ajax
-letak kat select onchange
-cara3. load semua daerah yg ada (hidden)..
guna javascript utk tunjukkan(show) daerah yg ko pilih |
|
|
|
|
|
|
|
n cara no 1
aku anggap table ko mcm n- negeri | daerah
- Perak | Kinta
- Perak | Kuala Kangsar
- Perak | Hilir Perak
Copy the Code- <?php
- //connection to db
- include('db_connect.php'); // rasanya ko faham n
- if($_POST['submit'])
- {
- //n mysql query kalau nk insert data
- }
-
- ?>
- <form method="post" name="form1" id="form1">
- <table width="200" border="1">
- <tr>
- <td>Negeri</td>
- <td>
- <select name="negeri" id="negeri" onchange="form.submit();">
- <option value="">Please Select</option>
- <?
- $sql = "SELECT negeri, daerah FROM table GROUP BY negeri ORDER BY negeri";
- $query = mysql_query($sql);
- while($row = mysql_fetch_assoc($query))
- {
- $selected = $row['negeri'] == $_POST['negeri'] ? 'selected="selected"' : '';
- echo '<option value="'. $row['negeri'] .'" '. $selected .' >'. $row['negeri'] .'</option>';
- }
- ?>
- </select>
- </td>
- </tr>
- <tr>
- <td>Daerah</td>
- <td>
- <select name="daerah" id="daerah">
- <option value=""><?= $_POST['negeri'] ? 'Please Select' : '-' ?></option>
- <?
- $sql = "SELECT daerah FROM table WHERE negeri = '". $_POST['negeri'] ."' ORDER BY daerah";
- $query = mysql_query($sql);
- while($row = mysql_fetch_assoc($query))
- {
- $selected = $row['daerah'] == $_POST['daerah'] ? 'selected="selected"' : '';
- echo '<option value="'. $row['daerah'] .'" '. $selected .'>'. $row['daerah'] .'</option>';
- }
- ?>
- </select>
- </td>
- </tr>
- <tr>
- <td colspan="2"><input type="submit" name="Submit" value="Submit"></td>
- </tr>
- </table>
- </form>
Copy the Code |
|
|
|
|
|
|
|
Ni coding aku guna untuk ajar adek2 aku dulu2...aku da test mmg jalan tp serabut sket ar sbb aku sumbat skali dgn css nye...ni pakai javascript semata-mata...untuk submit nnt kena ada adjustment sket
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Javascript OnChange Sample</title>
<style>
fieldset { width: 350px; padding: 10px; }
fieldset ul { margin: 3px; margin-right: 0px; padding: 0px; list-style-type: none; font-family: arial; font-size: 12px; }
fieldset ul li { float: left; margin: 0px; margin-top: 5px; padding: 0px; line-height: 17px; }
fieldset ul select { float: left; margin-left: 3px; width: 200px; }
fieldset ul label { margin-top: 1px; padding: 0px; float: left; width: 100px; clear: both; }
</style>
<script>
window.onload = function(){
loadSelect();
};
function loadSelect(){
var obj1 = false;
var obj2 = false;
var i = 0;
var listIdx = 0;
var states = new Array("Kedah","Selangor","Perak");
var districts = new Array( new Array("Sg Petani","Gurun","Baling","Alor Star"), // Untuk negeri kedah
new Array("Hulu Langat","Hulu Kelang","Gombak"),
new Array("Hulu Kinta","Grik","Manjung","Parit")
);
if (obj1 = document.getElementById("state")){ // test to check whether object is exist
if (obj1.options.length == 1){
for (i=0; i < states.length; i++){
addItem(obj1,states);
}
}
else{
if (obj1.selectedIndex != 0){
listIdx = obj1.selectedIndex;
if (obj2 = document.getElementById("district")){
// Remove existing options list
if (obj2.options.length > 0){
for (i=obj2.options.length; i > 0; i--){
removeItem(obj2,i);
}
}
// Add new item corresponding to state
for (i=0; i < districts[listIdx-1].length; i++){
addItem(obj2,districts[listIdx-1]);
}
}
}
}
}
return;
};
function addItem(obj,strVal){
var tmp = false;
if (obj){
tmp = document.createElement("option");
tmp.text = strVal;
try{
obj.add(tmp,null); // standards compliant
}
catch(ex){ obj.add(tmp); } // IE only
}
return;
};
function removeItem(obj,index){
if (obj){
obj.remove(index);
}
return;
};
</script>
</head>
<body>
<fieldset>
<legend>Lokasi</legend>
<ul>
<li>
<label for="state">Negeri </label>
<select id="state" name="state" ><option></option></select>
</li>
<li>
<label for="district">Daerah </label>
<select id="district" name="district"><option></option></select>
</li>
</ul>
</fieldset>
</body>
</html>
[ Last edited by digitalgunner at 17-4-2009 02:04 ] |
|
|
|
|
|
|
|
n kalau data tu static... tuan rumah nak data dari database...... |
|
|
|
|
|
|
|
Reply #6 BorderManager's post
Yerp...ni kalo data tu static ar...kalo nak data dari database...bley include skali masa loading java script PHP code tu utk generate states ngn districts nye array cm generate html content dr database...x pun guna kaedah AJAX nk request data2 states & districts...
aku sbenarnye prefer exclude PHP coding inline ngn HTML coding aku...senang nk tukar back-end processing nk pakai jsp ke, php ke, asp ke...ikut kehendak client...ni sample coding aku ni pn yg aku guna utk ajar adek2 aku HTML DOM ngn JS...sbb tu x de database tu...data2 aku request thru AJAX...tp aku exclude kn la AJAX call tu x mo kasik coding ni panjang sgt...kalo contoh yg aku bg ni mengelirukan tuan rumah...sori ek !!! |
|
|
|
|
|
|
|
yu betul la ko buat tu mcm... cara 3 dlm post yg lepas
cara ajax nanti ada masa boleh tempek sini |
|
|
|
|
|
|
|
Reply #8 BorderManager's post
Ada satu cara lagi kalo nk buat...ni cara yg aku slalu pakai ar...buat XML ngn XSL...pastu transformkn tukar terus innerHTML container <select> box aku tu... |
|
|
|
|
|
|
|
XML & XSL n aku tak pandai pun... boleh tolong ajar kat sini... |
|
|
|
|
|
|
| |
|