Link to home
Start Free TrialLog in
Avatar of Wayne88
Wayne88Flag for Canada

asked on

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:

https://www.experts-exchange.com/questions/24005839/Want-to-be-able-to-Add-Edit-and-Search-in-the-same-form-using-PHP-and-mySQL.html?sfQueryTermInfo=1+10+30+arraysg2008

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

Thanks in advance.
Avatar of Brad Brett
Brad Brett
Flag of United States of America image

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

Avatar of Wayne88

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of Brad Brett
Brad Brett
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Wayne88

ASKER

Thanks Medo, that worked!