2013-05-21 133 views
3

我有一个HTML表单,用于通过两个按钮“保存/提交”和“新建/重置”从MySQL数据库提交和检索数据。 它从MySQL数据库中正确提取数据,但是当我点击New /重置按钮为新的联系人条目无法清除表单文本字段。我的HTML和PHP代码是为下:清除html文本字段

<?php 
//Database Connection file. 
include'connect.php'; 
$sql = mysql_query("SELECT * FROM contact_list WHERE id='1'"); 
While($result = mysql_fetch_assoc($sql)){ 
    $fname = $result['fname']; 
    $lname = $result['lname']; 
    $email = $result['email']; 
    $contact = $result['contact']; 
} 

if(isset($_POST['fname'])&&isset($_POST['lname'])&&isset($_POST['email'])&& 
isset($_POST['contact'])){ 
    $fname = $_POST['fname']; 
    $lname = $_POST['lname']; 
    $email = $_POST['email']; 
    $contact = $_POST['contact']; 

if($sql = mysql_query("INSERT INTO contact_list VALUES ('', '$fname', '$lname', 
'$email', '$contact')")){ 
    echo'Contact Save Successfully.'; 
}else{ 
    echo'Contact not save.'; 
} 
} 
?> 
<html> 
<form action="sample.php" method="POST"> 
    First Name:<input type="text" name="fname" value="<?php if(isset($fname)) 
    {echo $fname;}?>"> 
    Last Name:<input type="text" name="lname" value="<?php if(isset($lname)) 
    {echo $lname;}?>"> 
    Email:<input type="text" name="email" value="<?php if(isset($email)) 
    {echo $email;}?>"> 
    Contact:<input type="text" name="contact" value="<?php if(isset($contact)) 
    {echo $contact;}?>"> 
//Clean all fields of forms for new entry. 
    <input type="reset" value="New"> 
//Save or submit form data into mysql database 
    <input type="submit" value="Save"> 
</form> 
</html> 
+0

容易做到这一点['MySQL'(http://php.net/manual/en/book。 mysql.php)('mysql_ *'函数)扩展名是[*** deprecated ***](http://php.net/manual/en/function.mysql-connect.php)。我建议使用['MySQLi'](http://php.net/manual/en/book.mysqli.php)('mysqli_ *'函数)或['PDO'](http://php.net/手册/ en/book.pdo.php)。 – BlitZ

+3

RESET将重置窗体的原始值,而不是清除它。使用Jquery来做到这一点。 '$(':text')。val('');'http://api.jquery.com/text-selector/ – Waygood

回答

1

您可以通过使用jQuery

<html> 
<head> 
<script type="text/javascript"> 
$(document).ready(function() { 
    $("#btnReset").click(function(){ 
     $("#fname").val(""); 
     $("#lname").val(""); 
     $("#email").val(""); 
     $("#contact").val(""); 
    }); 
}); 

</script> 
</head> 
<form action="sample.php" method="POST"> 
    First Name:<input type="text" name="fname" value="<?php if(isset($fname)) 
    {echo $fname;}?>" id="fname"> 
    Last Name:<input type="text" name="lname" value="<?php if(isset($lname)) 
    {echo $lname;}?>" id="lname"> 
    Email:<input type="text" name="email" value="<?php if(isset($email)) 
    {echo $email;}?>" id="email"> 
    Contact:<input type="text" name="contact" value="<?php if(isset($contact)) 
    {echo $contact;}?>" id="contact"> 
//Clean all fields of forms for new entry. 
    <input type="reset" value="New" id="btnReset"> 
//Save or submit form data into mysql database 
    <input type="submit" value="Save" id="btnSave"> 
</form> 
</html> 
+0

Dear red 20小时前 Muhammad Hisham我使用你的代码,它工作正常,多一点问题是,当我清除文本字段并将新数据放入文本字​​段并点击保存/提交按钮时,此数据提交成功,但仍位于文本字段中。我再次按下新/清除按钮来清除值,有任何建议来处理这个问题。 – khizar067

+0

@ khizar067然后在保存按钮上单击完成相同的工作,在保存数据后复制相同的代码行以清除字段。 –