Đầu tiên ta đi xây dựng một form. Ở đây tôi sẽ sử dụng form nhập thông tin khách hàng thêm vào database
Ta tạo form như sau
Ở phần xử lý code php ta xử lý như sau<form action='' method='post'><label>Tên khách hàng</label><input type='text' name='txtname' size='30' value='' /><br/><label>Email</label><input type='text' name='txtemail' size='30' value='' /><br/><label>Địa chỉ</label><input type='text' name='txtaddress' size='30' value='' /><br/><label>Số điện thoại</label><input type='text' name='txtphone' size='30' value='' /><br/><label>Giới tính</label>Nam <input type='radio' name='gender' size='30' value='0' checked="checked" />Nữ <input type='radio' name='gender' size='30' value='1' /><br/><label>Quốc tịch</label><select name='country'><option value="">Chọn quốc gia</option><option value='1'>Việt nam</option><option value='2'>Lào</option><option value='3'>Campuchia</option></select><br /><input type="submit" value="Get info" name="ok" /></form>
if(isset($_POST['ok'])){
$name = $email = $address = $phone = $gender = $country = "";
if($_POST['txtname'] == NULL){
echo "Vui lòng nhập tên khách hàng";
}else{
$name = $_POST['txtname'];
}
if($_POST['txtemail'] == NULL){
echo "Vui lòng nhập địa chỉ email";
}else{
$name = $_POST['txtemail'];
}
if($_POST['txtaddress'] == NULL){
echo "Vui lòng nhập địa chỉ";
}else{
$name = $_POST['txtaddress'];
}
if($_POST['txtphone'] == NULL){
echo "Vui lòng nhập số điện thoại";
}else{
$name = $_POST['txtphone'];
}
if($_POST['country'] == NULL){
echo "Vui lòng chọn quốc gia";
}else{
$name = $_POST['country'];
}
$gender = $_POST['gender'];
if($name && $address && $phone && $gender && $country){
echo $name."<br/>";
echo $address."<br/>";
echo $phone."<br/>";
echo $gender."<br/>";
echo $country."<br/>";
}
}
Xử lý giữ lại giá trị cho form<form action='' method='post'>
<label>Tên khách hàng</label>
<input type='text' name='txtname' size='30' value='<?php if(isset($_POST['txtname']) && $_POST['txtname'] != NULL){ echo $_POST['txtname']; } ?>' /><br/>
<label>Email</label>
<input type='text' name='txtemail' size='30' value='<?php if(isset($_POST['txtemail']) && $_POST['txtemail'] != NULL){ echo $_POST['txtemail']; } ?>' /><br/>
<label>Địa chỉ</label>
<input type='text' name='txtaddress' size='30' value='<?php if(isset($_POST['txtaddress']) && $_POST['txtaddress'] != NULL){ echo $_POST['txtaddress']; } ?>' /><br/>
<label>Số điện thoại</label>
<input type='text' name='txtphone' size='30' value='<?php if(isset($_POST['txtphone']) && $_POST['txtphone'] != NULL){ echo $_POST['txtphone']; } ?>' /><br/>
<label>Giới tính</label>
Nam <input type='radio' name='gender' size='30' value='0'<?php if(isset($_POST['gender']) && $_POST['gender'] == 0) {?> checked="checked"<?php } ?> />
Nữ <input type='radio' name='gender' size='30' value='1' <?php if(isset($_POST['gender']) && $_POST['gender'] == 1) {?> checked="checked"<?php } ?> /><br/>
<label>Quốc tịch</label>
<select name='country'>
<option value="" selected="selected">Chọn quốc gia</option>
<option value='1' <?php if(isset($_POST['country']) && $_POST['country'] == 1) {?> selected="selected" <?php } ?> >Việt nam</option>
<option value='2' <?php if(isset($_POST['country']) && $_POST['country'] == 2) {?> selected="selected" <?php } ?> >Lào</option>
<option value='3' <?php if(isset($_POST['country']) && $_POST['country'] == 3) {?> selected="selected" <?php } ?> >Campuchia</option>
</select>
<br />
<input type="submit" value="Get info" name="ok" />
</form>
Vậy là chúng ta hoàn thành. Các bạn làm lại và chạy thử trên máy tính của mình nhé...học lập trinh php,hoc lap trình php, học lập trình web, hoc lap trinh web , khóa học lập trình web, khoa hoc lap trinh web
0 nhận xét:
Đăng nhận xét