Jump to content

Submitting a form by pressing "Enter"


zece

Recommended Posts

If somebody can help me make this form to submit by pressing the "Enter" key, I would be very grateful.

I've been searching for hours to get an answer, still nothing that works came up...

It works just fine if I press the submit button, but I need to get the "Enter"-submitting working.

 

 

This is the code I have:

 

#############THIS IS THE CODE#######

<?php

 

include 'connect/logged_in.php';  #this file has session_start(); in it and checks if the user is logged in

include 'session.php';  #this file is needed for $session_website...on my login-file, there's an option, where you can chose the site from where to take the DB information from

 

 

$errmsg  = '';

$catName  = '';

echo $_SERVER['REQUEST_URL'];

if(isset($_POST['add_cat']))

{

  $catName  = trim($_POST['cat_name']);

  $catName  = htmlspecialchars($catName);

 

  if(trim($catName) == '')

    {

$errmsg = 'Please enter category name!';

    }

if($errmsg == '')

{  

  include 'connect/db_config.php';    ###this contains Hostname, Username, Password and DB

  include 'connect/db_connect.php';  ###this connects to the DB

 

          $date =date(Ymd);

 

  $query = " INSERT INTO ". "$ses_website" . "_category (cat_name, cat_date ) ".

" VALUES ('$catName','$date')";

mysql_query($query) or die('Error ,query failed');

 

  header('Location: ' . 'view_category.php' );

 

  include 'connect/db_close.php';

 

 

      exit;

  }

 

}

else{

?>

 

 

<!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>Add category</title>

<link href="all.css" rel="stylesheet" type="text/css">

 

 

 

<script language="javascript">

 

###This is where the checkForm function is declared, it's just there for form validation, nothing more

function checkForm()

{

// the variables below are assigned to each

// form input

var catName;

with(window.document.forms['category'])

{

catName  = document.forms['category'].elements['cat_name'];

}

 

// if name is empty alert the visitor

if(trim(catName.value) == '')

{

alert('Please enter category name!');

catName.focus();

return false;

}

// alert the visitor if email is empty or the format is not correct

 

else

{

// when all input are correct

// return true so the form will submit

catName= trim(catName);

alert('Category added! sasa');

return true;

}

}

 

 

 

/*

Strip whitespace from the beginning and end of a string

Input  : a string

Output : the trimmed string

*/

function trim(str)

{

return str.replace(/^\s+|\s+$/g,'');

}

</script>

</head>

 

<body>

 

<div align="left" class="all">

  <p class="all">

  <a href="home.php">Home</a>  |

  <a href="products.php">Products</a>  |  <a href="subcategories.php">Subcategories</a>  | 

  <a href="categories.php">Categories</a>    | #admin#

  <a href="logout.php">Logout</a> |

  <table class="all">

  <tr>

  <td ><a href="add_category.php">Add New Category</a> |</td>

<td ><a href="view_category.php">View All Categories</a> |</td>

  </tr>

  </table>

</p>

 

  <p class="all"> </p>

   

 

####this is the form I want to submit

<form method="post" name="category" onsubmit="checkForm();return false;">

  <table>

  <tr>

  <td >New category name: </td>

<td ><input class="input" name="cat_name" type="text" id="cat_name" > </td>

 

  </tr>

  <tr>

        <td>Confirm: </td>

  <td ><input class="input" name="add_cat" type="submit" id="add_cat" value="Add new category"  > </td>

  </tr>

 

</table>

</form> 

 

</div>

</body>

</html>

<?php } ?>

 

#####End of the code

 

Link to comment
https://forums.phpfreaks.com/topic/81684-submitting-a-form-by-pressing-enter/
Share on other sites

Try the following:

 

<form method="post" name="category">
  <table>
   <tr>
         <td >New category name: </td>
      <td ><input class="input" name="cat_name" type="text" id="cat_name" > </td>
      
   </tr>
   <tr>
        <td>Confirm: </td>
         <td ><input class="input" name="add_cat" type="submit" id="add_cat" value="Add new category"  onclick="checkForm();return false;"> </td>
   </tr>
   
</table>
</form> 

</div>
</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.