[PHP] Form and SaveData in 1 files!

June 22, 2012 12:33 am PHP

โดยไฟล์นี้ จะแสดงตัวอย่างในการออกแบบไฟล์ที่ใช้ในการแสดงฟอร์มที่ใช้กรอกข้อมูล และบันทึกข้อมูล โดยใช้ไฟล์ๆ เดียว โดยจัดการทำงานโดยใช้ตัวแปร savedata เป็นการกำหนดค่าในการทำงาน โดยมีดังนี้

 

<?php
#Form Version 1.0 By Kusumoto
#http://kusumoto.co

include("config.php");
$id = $_REQUEST['id'];
if ($id == "") {
echo "Error 403";
exit;
}
$savedata = $_REQUEST['savedata'];
if ($savedata == "true") {
$id = $_REQUEST['id'];
$title = $_REQUEST['title'];
$data = $_REQUEST['data'];
$author = $_REQUEST['author'];
	if ($id == "") {
echo "Error 403";
exit;
}
if ($title == "") {
echo "Error 403";
exit;
}
if ($data == "") {
echo "Error 403";
exit;
}
if ($author== "") {
echo "Error 403";
exit;
}
 
$today_date=date("d-M-Y");
$today_time=date("h:i:s: a");
 
mysql_connect($host,$user,$pass)or die("Error Database");
mysql_select_db($db);
mysql_query(" set NAMES utf8 ");
 
$sql = "INSERT into note_data (subid, title, data, date, author) values ('$id','$title','$data','$today_date $today_time','$author') ";
$result = mysql_db_query($db, $sql);
 
 if (!$result) {
 echo("Error in SQL ".mysql_error() );
 } else {
 echo "Complete! Please close this window.";
 }
} else {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>New Note</title>
<style type="text/css">
<!--
body,td,th {
	font-family: tahoma;
}
-->
</style></head>
<?php
mysql_connect($host,$user,$pass)or die("Error Database");
mysql_select_db($db);
mysql_query(" set NAMES utf8 ");
$sql = "SELECT * From note_subject where id='$id'";
$dbquery = mysql_query($sql);
$result = mysql_fetch_array($dbquery);
if (!$result ) {
echo "Error 404";
exit;
}
 
$subcode = mysql_result($dbquery, 0, 'subcode');
?>
<body>
<p><img src="img/note.png" width="60" height="60" /><strong> New Note</strong></p>
<form id="form1" name="form1" method="post" action="newnote.php?savedata=true">
  <table width="588" border="1">
    <tr>
      <td width="98">Subject Code</td>
      <td width="474"><input name="subcode" type="text" disabled="disabled" id="subcode" value="<?php echo $subcode; ?>" />
      <input name="subcode" type="hidden" id="subcode" value="<?php echo $subcode; ?>" />
      <input name="id" type="hidden" id="id" value="<?php echo $id; ?>" /></td>
    </tr>
    <tr>
      <td>Note Title</td>
      <td><input type="text" name="title" id="title" /></td>
    </tr>
    <tr>
      <td>Note Retail</td>
      <td><textarea name="data" cols="50" rows="10" id="data"></textarea></td>
    </tr>
    <tr>
      <td>Author</td>
      <td><input type="text" name="author" id="author" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="button" id="button" value="Send" /></td>
    </tr>
  </table>
</form>
 
<p>&nbsp; </p>
<hr/>
 <p><em><strong>Kusumoto Network!</strong></em></p>
</body>
</html>
 
<?php } ?>