How to clear fields

Posted on 2011-04-27
Last Modified: 2012-05-11

I am learning PHP so bear with me.  I had done much reading into how to clear a form and some articles suggested using JAVA, another using HTML onReload command but I am still unclear on how to do this.  I had tried many things but none seems to work.

Let's use a good existing code given by cxr for example:

How can I have the form clear itself after adding or deleting a record?

Thanks in advance.
Question by:Wayne88
    LVL 12

    Expert Comment

    by:Mohamed Abowarda
    There are alot of ways to accomplish that, you can refresh the page or consider using javascript:

    You can use:

    Open in new window

    LVL 13

    Author Comment

    Thanks for your answer but it is not working using the example given.

    include 'connection.php';
    $tablename = 'arraysg2008';
    function get_field($fld) {
      if(!isset($_POST[$fld])) return '';         # field not posted
      $value = $_POST[$fld];
      if(get_magic_quotes_gpc())                  # did php add slashes?
        $value = stripslashes($value);            # yes, remove them
      $value = mysql_real_escape_string($value);  # add mysql slashes
      return $value;
    $row_id = get_field('row_id'); 
    $firstname = get_field('firstname');
    $lastname = get_field('lastname');
    $department = get_field('department');
    if($_POST['user']=='Search') {
      $res = mysql_query(
        "select * from $tablename where
         firstname like '$firstname%' and
         lastname like '$lastname%' and
         department like '$department%'
         order by id");
      if($res) {
        $row = mysql_fetch_assoc($res);
        if($row) {
          $row_count = mysql_num_rows($res);
          if($row_count > 1) $msg = $row_count.' rows found, showing the first';
        } else $msg = 'No rows found';
      } else $msg = 'query failed: '.mysql_error();
    ?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Employee editing</title>
    <form action="<?php echo $_SERVER['SCRIPT_NAME']?>" method="post"> 
    <input type="hidden" name="row_id" value="<?php echo $row_id; ?>" />
    First Name: <input type="text" name="firstname" value="<?php echo $firstname; ?>" size="15"> <br>
    Last Name: <input type="text" name="lastname" value="<?php echo $lastname; ?>" size="15"> <br>
    Department: <input type="text" name="department" value="<?php echo $department; ?>" size="15"> <br>
    <input type="submit" name="user" value="Add">
    <input type="submit" name="user" value="Update">
    <input type="submit" name="user" value="Search">
    <input type="submit" name="user" value="Delete">
    <input type="button" value="Reset Form" onClick="this.form.reset()" />
    switch ($_POST['user']) {        
      case 'Add':
        $res = mysql_query(
          "insert into $tablename set 
         $msg = $res ? 'Record was added' : 'Error: '.mysql_error();
      case 'Update':
        $res = mysql_query(
          "update $tablename set 
           where id=$row_id");
        $msg = $res ? 'Record was updated' : 'Error: '.mysql_error();
      case 'Delete':
        $res = mysql_query(
          "delete from $tablename 
           where id=$row_id");
        $msg = $res ? 'Record was removed' : 'Error: '.mysql_error();
      echo '<p style="color:red">'.$msg.'</p>';

    Open in new window

    LVL 12

    Accepted Solution

    To clear text box:
    document.getElementById("TextBoxName").value = null;

    Open in new window

    Change 'TextBoxName' to the name of your text box.
    LVL 13

    Author Closing Comment

    Thanks Medo, that worked!

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now