• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 337
  • Last Modified:

How to clear fields

Hi,

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:

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_24005839.html?sfQueryTermInfo=1+10+30+arraysg2008

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

Thanks in advance.
0
Wayne88
Asked:
Wayne88
  • 2
  • 2
1 Solution
 
Mohamed AbowardaSoftware EngineerCommented:
There are alot of ways to accomplish that, you can refresh the page or consider using javascript:
http://www.javascript-coder.com/javascript-form/javascript-reset-form.phtml

You can use:
document.formName.reset();

Open in new window

0
 
Wayne88Author Commented:
Thanks for your answer but it is not working using the example given.


<?php
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';
      $row_id=$row['id'];
      $firstname=$row['firstname'];
      $lastname=$row['lastname'];
      $department=$row['department'];
    } else $msg = 'No rows found';
  } else $msg = 'query failed: '.mysql_error();
}
 
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Employee editing</title>
</head>
<body>
<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()" />
 </form>
 
<?php
 
switch ($_POST['user']) {        
  case 'Add':
    $res = mysql_query(
      "insert into $tablename set 
       firstname='$firstname',
       lastname='$lastname',
       department='$department'");
     $msg = $res ? 'Record was added' : 'Error: '.mysql_error();
     break;
        
  case 'Update':
    $res = mysql_query(
      "update $tablename set 
       firstname='$firstname',
       lastname='$lastname',
       department='$department'
       where id=$row_id");
    $msg = $res ? 'Record was updated' : 'Error: '.mysql_error();
    break;
 
  case 'Delete':
    $res = mysql_query(
      "delete from $tablename 
       where id=$row_id");
    $msg = $res ? 'Record was removed' : 'Error: '.mysql_error();
    break;
}
 
if(isset($msg))
  echo '<p style="color:red">'.$msg.'</p>';
 
?>
 
</body>
</html>

Open in new window

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

Open in new window


Change 'TextBoxName' to the name of your text box.
0
 
Wayne88Author Commented:
Thanks Medo, that worked!
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now