在此表单中,当您单击表格中的记录时,数据将显示在左侧的表单上。当我单击“插入”按钮时,数据将存储在数据库中,并从表单中消失,就像没有记录一样。有没有办法让记录保持在我点击“插入”按钮后。
这是我插入的php代码。
<?php
if(isset($_POST['submit'])){
$ID = $_POST['ID'];
$Name = $_POST['Name'];
$Location = $_POST['Location'];
$Date = $_POST['Date'];
$Category = $_POST['Category'];
$LabourSupplier = $_POST['LabourSupplier'];
$BasicSalary = $_POST['BasicSalary'];
$Allowance1 = $_POST['Allowance1'];
$InTime = $_POST['InTime'];
$OutTime = $_POST['OutTime'];
$Day = $_POST['Day'];
$DayRate = $_POST['DayRate'];
$Salary = $_POST['Salary'];
$OtHours = $_POST['OtHours'];
$OTrate = $_POST['OTrate'];
$OTAmount = $_POST['OTAmount'];
$Allowance2 = $_POST['Allowance2'];
$TotalSalary = $_POST['TotalSalary'];
$Advance = $_POST['Advance'];
$SalaryTo = $_POST['SalaryTo'];
$Mobile = $_POST['Mobile'];
$Home = $_POST['Home'];
//Insert Query of SQL
$query = mysql_query("INSERT INTO attendance(ID,Name,Location, Date,Category,LabourSupplier,BasicSalary,Allowance1,InTime,OutTime,Day,DayRate,Salary,OTHours,OTrate,OTAmount,Allowance2,TotalSalary,Advance,SalaryToHand, Mobile, Home) VALUES ('$ID','$Name','$Location' ,'$Date', '$Category','$LabourSupplier','$BasicSalary','$Allowance1','$InTime','$OutTime','$Day','$DayRate','$Salary','$OtHours','$OTrate','$OTAmount','$Allowance2','$TotalSalary','$Advance','$SalaryTo','$Mobile','$Home')",$connection);
}
?>
这是表单的html代码。
<form id="details" action="" method="POST">
<fieldset> ID:
<input class="input" type="text" id="id" name="ID" value="" />
</fieldset>
<fieldset> Name:
<input class="input" type="text" id="name" name="Name" value="" />
</fieldset>
<fieldset> Location:
<input class="input" type="text" id="location" name="Location" value="" />
</fieldset>
<fieldset> Date:
<input class="input" type="text" id="date" name="Date" value="" />
</fieldset>
<fieldset> Category:
<input class="input" type="text" id="category" name="Category" value="" />
</fieldset>
<fieldset> Labour Supplier:
<input class="input" type="text" id="labsupp" name="LabourSupplier" value="" />
</fieldset>
<fieldset> Basic Salary:
<input class="input" type="text" id="bsalary" name="BasicSalary" value="" />
</fieldset>
<fieldset> Allowance I:
<input class="input" type="text" id="all1" name="Allowance1" value="" />
</fieldset>
<fieldset> In-Time:
<input class="input" type="text" name="InTime" id="intime" value="" />
</fieldset>
<fieldset>Out-Time:
<input class="input" type="text" name="OutTime" id="outtime" value="" />
</fieldset>
<fieldset> Day:
<input class="input" type="text" id="day" name="Day" value="" />
</fieldset>
<fieldset> Day Rate:
<input class="input" type="text" id="dayrate" name="DayRate" value="" />
</fieldset>
<fieldset>Salary:
<input class="input" type="text" id="salary" name="Salary" value="" />
</fieldset>
<fieldset>OT Hours:
<input class="input" type="text" id="othours" name="OtHours" value="" />
</fieldset>
<fieldset>OT Rate:
<input class="input" type="text" id="otrate" name="OTrate" value="" />
</fieldset>
<fieldset> OT Amount:
<input class="input" type="text" id="otamount" name="OTAmount" value="" />
</fieldset>
<fieldset> Allownace II:
<input class="input" type="text" id="allowance2" name="Allowance2" value="" />
</fieldset>
<fieldset> Total Salary:
<input class="input" type="text" id="totalsalary" name="TotalSalary" value="" />
</fieldset>
<fieldset> Advance:
<input class="input" type="text" id="advance" name="Advance" value="" />
</fieldset>
<fieldset> Salary To Hand:
<input class="input" type="text" id="salaryto" name="SalaryTo" value="" />
</fieldset>
<fieldset> Mobile:
<input class="input" type="text" id="mobile" name="Mobile" value="" />
</fieldset>
<fieldset> Home:
<input class="input" type="text" id="home" name="Home" value="" />
</fieldset> <br>
<fieldset >
<button name="submit" type="submit" id="submit">Insert</button>
<button name="update" type="submit" id="update">Update</button>
<button name="delete" type="delete" id="delete">Delete</button>
<button onclick="win();" name="Back" type="back" id="details-back">Back</button>
</fieldset>
</form>
答案 0 :(得分:0)
您最好的选择是使用ajax,它基本上允许您在不重新加载页面的情况下发布数据(这就是清除表单字段的方式)。有关详细信息,请参见此处:https://api.jquery.com/jquery.post/,但一个简单的示例如下所示:
$.post("myscript.php", function(data) {
alert("record added!");
});
或者,当您发布数据时,您只需在每个表单字段中检查它,然后再将其放入。例如,使用您的ID字段,您可以执行以下操作:
<input class="input" type="text" id="id" name="ID" value="<?php isset($_POST['ID']) ? $_POST['ID'] : false; ?>" />
如果您对表单进行任何类型的错误检查,您可能需要执行此类操作,因此如果他们输入的内容无效并且无法插入,则您希望保留所有字段数据而不是清除它。 / p>