We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

How to clear fields

Wayne88
Wayne88 asked
on
Medium Priority
351 Views
Last Modified: 2012-05-11
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/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.
Comment
Watch Question

Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
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

CERTIFIED EXPERT
Top Expert 2015

Author

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

Senior Software Engineer
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
CERTIFIED EXPERT
Top Expert 2015

Author

Commented:
Thanks Medo, that worked!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.