php 表单 - onsubmit 重置表单

发布于 2024-12-17 00:55:58 字数 4688 浏览 3 评论 0原文

我已经创建了一个表单,到目前为止一切正常。我唯一剩下的就是在提交表单时重置字段。这似乎是一项简单的任务,但环顾四周并尝试了一些事情后,似乎没有任何效果:(我现在所拥有的内容是从另一篇文章中获取的,但我的提交事件与他们的不同,这就是为什么我认为它不起作用。任何帮助非常感谢

php 部分

////////////////////////////////上传图像

//define a maxim size for the uploaded images in Kb
define ("MAX_SIZE","500"); 

 //This function reads the extension of the file. It is used to determine if the file   is an image by checking the extension.
function getExtension($str) {
     $i = strrpos($str,".");
     if (!$i) { return ""; }
     $l = strlen($str) - $i;
     $ext = substr($str,$i+1,$l);
     return $ext;
}

//This variable is used as a flag. The value is initialized with 0 (meaning no error     found)  
//and it will be changed to 1 if an errro occures.  
//If the error occures the file will not be uploaded.
$errors=0;
//checks if the form has been submitted
if(isset($_POST['submit'])) 
{
//reads the name of the file the user submitted for uploading
$image=$_FILES['image']['name'];
//if it is not empty
if ($image) 
{
//get the original name of the file from the clients machine
    $filename = stripslashes($_FILES['image']['name']);
//get the extension of the file in a lower case format
    $extension = getExtension($filename);
    $extension = strtolower($extension);
    if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") &&      ($extension != "gif")) 
    {
    //print error message
        echo '<h1>Unknown extension!</h1>';
        $errors=1;
    }
    else
    {
//get the size of the image in bytes
//$_FILES['image']['tmp_name'] is the temporary filename of the file
//in which the uploaded file was stored on the server
$size=filesize($_FILES['image']['tmp_name']);

//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*1024)
{
echo '<h1>You have exceeded the size limit!</h1>';
$errors=1;
}

$newname="storeImages/".$filename;
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied) 
{
echo '<h1>Copy unsuccessfull!</h1>';
$errors=1;
}}}}

//If no errors registred, print the success message
if(isset($_POST['submit']) && !$errors) 
{
echo "<h1>File Uploaded Successfully!</h1>";
}

//direct to same page but refresh
//header('Location: storeListForm.php');

?>

表单

<body>

<script type="text/javascript">
function onFormSubmit ()
{
    document.storeList.reset();
    return true; // allow form submission to continue
}
</script>

<div id="signUp"> 

<?php 
//if the validation falls back to php, then print the validation error
if (isset($error_message)) echo $error_message;
?>

<form method="post" action="" id="storeList" name="storeList" enctype="multipart/form-  data">  
<table>
<tr>
  <td><label for="name">Name</label></td>
  <td><input type="text" name="name" id="name" value="<?php if (isset($_POST['name'])) echo $_POST['name'];?>"/></td>
</tr>
<tr>
   <td> <label for="storeLocation">Location</label></td>
   <td><input type="text" name="storeLocation" id="storeLocation" value="<?php if   (isset($_POST['storeLocation'])) echo $_POST['storeLocation'];?>"/></td>
</tr>
<tr>
   <td><label for="featured_items">Featured Items</label></td>
   <td><input type="text" name="featured_items" id="featured_items" value="<?php if  (isset($_POST['featured_items'])) echo $_POST['featured_items'];?>"/></td>
</tr>
<tr>
   <td><label for="keywords">Keywords</label></td>
   <td><input type="text" name="keywords" id="keywords" value="<?php if     (isset($_POST['featured_items'])) echo $_POST['keywords'];?>"/></td>
</tr>
<tr>
   <td><label for="fileImage">Image</lable></td>
   <td><input type="file" name="image"></td>

</tr>
   <td>Description</td>
   <td> <textarea for="description"></textarea type="text area" name="description"  id="description" value="><?php if (isset($_POST['description'])) echo  $_POST['description'];?>"/></td>
</tr>
<tr>
    <td></td>
    <td><input type="submit" name="submit" id="submit" value="Add Store"  onsubmit="this.submit(); this.reset(); return false;"></td>
</tr>
</table>
</form>
</div>
</body>

I have created a form and everything is working fine so far. The only thing I have left is to reset the fields when the form is submitted. This seems like an easy task but after looking around and trying some things nothing seems to do the trick :( What I have now was taken from another post but my submit event was different from theirs which is why I think its not working. Any help is much appreciated.

php part

/////////////////////////////////upload image

//define a maxim size for the uploaded images in Kb
define ("MAX_SIZE","500"); 

 //This function reads the extension of the file. It is used to determine if the file   is an image by checking the extension.
function getExtension($str) {
     $i = strrpos($str,".");
     if (!$i) { return ""; }
     $l = strlen($str) - $i;
     $ext = substr($str,$i+1,$l);
     return $ext;
}

//This variable is used as a flag. The value is initialized with 0 (meaning no error     found)  
//and it will be changed to 1 if an errro occures.  
//If the error occures the file will not be uploaded.
$errors=0;
//checks if the form has been submitted
if(isset($_POST['submit'])) 
{
//reads the name of the file the user submitted for uploading
$image=$_FILES['image']['name'];
//if it is not empty
if ($image) 
{
//get the original name of the file from the clients machine
    $filename = stripslashes($_FILES['image']['name']);
//get the extension of the file in a lower case format
    $extension = getExtension($filename);
    $extension = strtolower($extension);
    if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") &&      ($extension != "gif")) 
    {
    //print error message
        echo '<h1>Unknown extension!</h1>';
        $errors=1;
    }
    else
    {
//get the size of the image in bytes
//$_FILES['image']['tmp_name'] is the temporary filename of the file
//in which the uploaded file was stored on the server
$size=filesize($_FILES['image']['tmp_name']);

//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*1024)
{
echo '<h1>You have exceeded the size limit!</h1>';
$errors=1;
}

$newname="storeImages/".$filename;
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied) 
{
echo '<h1>Copy unsuccessfull!</h1>';
$errors=1;
}}}}

//If no errors registred, print the success message
if(isset($_POST['submit']) && !$errors) 
{
echo "<h1>File Uploaded Successfully!</h1>";
}

//direct to same page but refresh
//header('Location: storeListForm.php');

?>

The form

<body>

<script type="text/javascript">
function onFormSubmit ()
{
    document.storeList.reset();
    return true; // allow form submission to continue
}
</script>

<div id="signUp"> 

<?php 
//if the validation falls back to php, then print the validation error
if (isset($error_message)) echo $error_message;
?>

<form method="post" action="" id="storeList" name="storeList" enctype="multipart/form-  data">  
<table>
<tr>
  <td><label for="name">Name</label></td>
  <td><input type="text" name="name" id="name" value="<?php if (isset($_POST['name'])) echo $_POST['name'];?>"/></td>
</tr>
<tr>
   <td> <label for="storeLocation">Location</label></td>
   <td><input type="text" name="storeLocation" id="storeLocation" value="<?php if   (isset($_POST['storeLocation'])) echo $_POST['storeLocation'];?>"/></td>
</tr>
<tr>
   <td><label for="featured_items">Featured Items</label></td>
   <td><input type="text" name="featured_items" id="featured_items" value="<?php if  (isset($_POST['featured_items'])) echo $_POST['featured_items'];?>"/></td>
</tr>
<tr>
   <td><label for="keywords">Keywords</label></td>
   <td><input type="text" name="keywords" id="keywords" value="<?php if     (isset($_POST['featured_items'])) echo $_POST['keywords'];?>"/></td>
</tr>
<tr>
   <td><label for="fileImage">Image</lable></td>
   <td><input type="file" name="image"></td>

</tr>
   <td>Description</td>
   <td> <textarea for="description"></textarea type="text area" name="description"  id="description" value="><?php if (isset($_POST['description'])) echo  $_POST['description'];?>"/></td>
</tr>
<tr>
    <td></td>
    <td><input type="submit" name="submit" id="submit" value="Add Store"  onsubmit="this.submit(); this.reset(); return false;"></td>
</tr>
</table>
</form>
</div>
</body>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

友谊不毕业 2024-12-24 00:55:58

也许您正在寻找这个?

onClick="this.form.reset()"

(您的只是 this.reset())。

Perhaps you're looking for this?

onClick="this.form.reset()"

(Yours is only this.reset()).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文