我编写了php代码,用于将记录插入MySQL数据库。数据已成功插入。所以,我希望在插入数据库后清除文本字段记录。这是我的代码。
<div class="form">
<h1>Application Form</h1>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<pre><div class="app">
First Name : <input type="text" size="48" align="middle" name="firstname" value="<?php echo $firstname;?>">
<span class="error" style="background-color:#FF0000">* <?php echo $firstnameErr;?></span>
Last Name : <input type="text" size="48" align="middle" name="lastname" value="<?php echo $lastname;?>">
<span class="error" style="background-color:#FF0000">* <?php echo $lastnameErr;?></span>
Company / Organization : <input type="text" size="48" name="company" value="<?php echo $company;?>">
<span class="error"><?php echo $companyErr;?></span>
Home Page : <input type="text" size="48" name="homepage" value="<?php echo $homepage;?>">
<span class="error"><?php echo $homepageErr;?></span>
Contact No : <input type="text" size="48" name="contactno" value="<?php echo $contactno;?>"><span class="error"><?php echo $contactnoErr;?></span>
Address Line 01 : <input type="text" size="48" name="addressline1" value="<?php echo $addressline1;?>">
<span class="error" style="background-color:#FF0000">* <?php echo $addressline1Err;?></span>
Address Line 02 : <input type="text" size="48" name="addressline2" value="<?php echo $addressline2;?>">
<span class="error" style="background-color:#FF0000">* <?php echo $addressline2Err;?></span>
City : <input type="text" size="48" name="city" value="<?php echo $city;?>">
<span class="error" style="background-color:#FF0000">* <?php echo $cityErr;?></span>
Postal Code : <input type="text" size="48" name="postalcode" value="<?php echo $postalcode;?>">
<span class="error" style="background-color:#FF0000">* <?php echo $postalcodeErr;?></span>
Country : <input type="text" size="48" name="country" value="<?php echo $country;?>">
<span class="error" style="background-color:#FF0000">* <?php echo $countryErr;?></span>
Email : <input type="text" size="48" name="email" value="<?php echo $email;?>">
<span class="error" style="background-color:#FF0000">* <?php echo $emailErr;?></span>
Fill the below section if you are submitting abstract only. Otherwise ignore it. <br />
Abstract Details : <textarea name="abstractdetails" rows="5" cols="40"><?php echo $abstractdetails;?></textarea>
<p>Click the button to display an alert box:</p>
<button>Save</button>
<input type="reset" name="submit" value="Cancel" align="middle">
<br><br></div>
</form>
</table>
<?php
try {
$db_user = 'root';
$db_pass = 'xxxxx';
$db = new PDO( 'mysql:host=localhost;dbname=abc', $db_user, $db_pass );
$form = $_POST;
$sql = "INSERT INTO app (
firstname, lastname, company, homepage, contactno, addressline1, addressline2, city,
postalcode, country, email, abstractdetails )
VALUES (
:firstname, :lastname, :company, :homepage, :contactno, :addressline1, :addressline2,
:city, :postalcode, :country, :email, :abstractdetails )";
$query = $db->prepare( $sql );
$query->execute( array( ':firstname'=>$firstname, ':lastname'=>$lastname,
':company'=>$company, ':homepage'=>$homepage, ':contactno'=>$contactno,
':addressline1'=>$addressline1, ':addressline2'=>$addressline2, ':city'=>$city,
':postalcode'=>$postalcode, ':country'=>$country, ':email'=>$email,
':abstractdetails'=>$abstractdetails ));
if($query->rowCount() > 0){
echo "Record Inserted Successfully !!";
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
答案 0 :(得分:1)
您正在为输入字段value = "<?php echo $lastname;?>"
指定值,如果您这样做,很明显表单数据将重新填充。如果您想简单地阻止这种情况,请从您的所有字段中删除这些属性。
答案 1 :(得分:0)
如果您将PHP放在页面顶部,则可以在成功时将所有变量重置为空白。
<? php
try {
$db_user = 'root';
$db_pass = 'xxxxx';
$db = new PDO('mysql:host=localhost;dbname=abc', $db_user, $db_pass);
$form = $_POST;
$sql = "INSERT INTO app (
firstname, lastname, company, homepage, contactno, addressline1, addressline2, city, postalcode, country, email, abstractdetails) VALUES (:firstname, :lastname, :company, :homepage, :contactno, :addressline1, :addressline2, :city, :postalcode, :country, :email, :abstractdetails)";
$query = $db - > prepare($sql);
$query - > execute(array(':firstname' = > $firstname, ':lastname' = > $lastname, ':company' = > $company, ':homepage' = > $homepage, ':contactno' = > $contactno, ':addressline1' = > $addressline1, ':addressline2' = > $addressline2, ':city' = > $city, ':postalcode' = > $postalcode, ':country' = > $country, ':email' = > $email, ':abstractdetails' = > $abstractdetails));
if ($query - > rowCount() > 0) {
echo "Record Inserted Successfully !!";
$firstname = ""; $lastname = ""; // ...
}
} catch (PDOException $e) {
echo $e - > getMessage();
} ?>
<div class="form">
< h1>Application Form
< /h1>
<p><span class="error">* required field.</span > < /p> <form method="post" action="<?php echo htmlspecialchars($_SERVER[" PHP_SELF"]);?>"> <pre><div class="app"> First Name : <input type="text" size="48" align="middle" name="firstname" value="<?php echo $firstname;?>"> <span class="error" style="background-color:#FF0000">* <?php echo $firstnameErr;
?></span > Last Name: < input type = "text"
size = "48"
align = "middle"
name = "lastname"
value = "<?php echo $lastname;?>" > < span class = "error"
style = "background-color:#FF0000" > * <? php echo $lastnameErr; ?> < /span> Company /
Organization: < input type = "text"
size = "48"
name = "company"
value = "<?php echo $company;?>" > < span class = "error" > <? php echo $companyErr; ?> < /span> Home Page : <input type="text" size="48" name="homepage" value="<?php echo $homepage;?>"> <span class="error"><?php echo $homepageErr;
?></span > Contact No: < input type = "text"
size = "48"
name = "contactno"
value = "<?php echo $contactno;?>" > < span class = "error" > <? php echo $contactnoErr; ?> < /span> Address Line 01 : <input type="text" size="48" name="addressline1" value="<?php echo $addressline1;?>"> <span class="error" style="background-color:#FF0000">* <?php echo $addressline1Err;
?></span > Address Line 02: < input type = "text"
size = "48"
name = "addressline2"
value = "<?php echo $addressline2;?>" > < span class = "error"
style = "background-color:#FF0000" > * <? php echo $addressline2Err; ?> < /span> City : <input type="text" size="48" name="city" value="<?php echo $city;?>"> <span class="error" style="background-color:#FF0000">* <?php echo $cityErr;
?></span > Postal Code: < input type = "text"
size = "48"
name = "postalcode"
value = "<?php echo $postalcode;?>" > < span class = "error"
style = "background-color:#FF0000" > * <? php echo $postalcodeErr; ?> < /span> Country : <input type="text" size="48" name="country" value="<?php echo $country;?>"> <span class="error" style="background-color:#FF0000">* <?php echo $countryErr;
?></span > Email: < input type = "text"
size = "48"
name = "email"
value = "<?php echo $email;?>" > < span class = "error"
style = "background-color:#FF0000" > * <? php echo $emailErr; ?> < /span> Fill the below section if you are submitting abstract only. Otherwise ignore it. <br / > Abstract Details: < textarea name = "abstractdetails"
rows = "5"
cols = "40" > <? php echo $abstractdetails; ?> < /textarea> <p>Click the button to display an alert box:</p > < button > Save < /button> <input type="reset" name="submit" value="Cancel" align="middle"> <br><br></div > < /form> </table >
答案 2 :(得分:-1)
如果您将JavaScript与ajax + jquery + php结合使用,则可以直接粘贴此代码
<form method="" action="target.php" id="add_form" autocomplete="off">
<div class="col-md-12 mx-auto">
<div class="form-group">
<label for="car-name">ADD / CREATE ITEM</label>
<input type="text" id="car-name" name="car_name" class="form-control" >
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary form-control" value="Add item">
</div>
</div>
</form>
//如果您位于.html或.php页面,则可以将其放入<script></script>
标记中
$(document).ready(function(){
$("#add_form").submit(function(evt) {
evt.preventDefault();
//sending data information
var postData = $(this).serialize(); //alert(postData);
//sending url
var url = $(this).attr('action'); //alert(url);
// sending data
$.post(url, postData, function(php_table_data){
$("#car_result").html(php_table_data);
window.location.reload();
});
});
});
** .php代码**
if(isset($_POST['car_name'])) {
$car_name = $_POST['car_name'];
if(!empty($car_name)) {
$query = "INSERT INTO tableName(Columnname) VALUES('$car_name') ";
$update_car_name = mysqli_query($connection, $query);
if(!$update_car_name) {
die("QUERY FAILED");
}
} else {
echo "<script>alert('this field cannot be empty')</script>";
}
} else {
header("Location: index.php");
}