PDA

View Full Version : Keypress event



michelleLiu
07-05-2005, 05:23 PM
Hi,
We have php script for search customer record. We are using press button the post to other page right now. I want to change it to when user insert text work in textbox, instead of press button, just press Enter key, then search event happen. How can I realize it?

Thank you

Michelle

Fou-Lu
07-05-2005, 05:31 PM
Using a form, you should just be able to press enter and have the data submit. This is an html not a php property.
The exception of course for forms is the textarea, when you press enter there, it jumps to a new line. If you want to change this, javascript is your only route.

michelleLiu
07-05-2005, 06:33 PM
Hi,
Thank you. Could you give me more hint on Javascript?

Michelle

michelleLiu
07-05-2005, 09:41 PM
This is my code using javascript in php. However it does not work. Anyone can tell me what is wrong?

Michelle


<html>

<head>
<title>Customer Lookup</title>
<center><h2><font face=times new roman color="red">ICAM </font><font face=times new roman>Customers</font></h2></center>
<script type="text/JavaScript">
function entsub(event,ourform) {
if (event && event.which == 13)
ourform.submit();
else
return true;
}

</Script>
</head>

<body text=#000000>
<center>
<table width=325 border=1 cellspacing=1 cellpadding=10>

<?

print ("<form method=\"POST\" action=\"startup.php\">\n");
print ("<tr>\n");
print ("<td align=\"center\">\n");
print ("<br>\n");
print ("<font face=verdana size=2>Enter Customer Name (Partial or Complete):</font>\n");
print ("<br><br>\n");
Print ("<input type=\"text\" name=\"customer1\" value=\" \" onkeypress=\"return entsub(event,this.form)\" > \n");
print ("<input type=\"submit\" name=\"submit\" value=\"FIND\">\n");

print ("<br>\n");
print ("<br>\n");
print ("<center><font face=verdana size=2><b>OR</b></font></center>\n");
print ("<br>\n");
print ("<font face=verdana size=2><A HREF=\"./maintenance/customerinfo.php\">View customer list</A></font>\n");
print ("<br>\n");
print ("<br>\n");
print ("</td>\n");
print ("</tr>\n");
print ("</form>\n");

?>
</table>
</center>
</body>
</html>

Fou-Lu
07-06-2005, 04:18 AM
Eh?
No, no, don't use javascript. Javascript is unreliable, make sure you have a submit button that can be used.
All you need is the input fields and a submit field, and you can press enter to submit it.

As well.... I don't see any script action happening there.

michelleLiu
07-06-2005, 02:35 PM
Hi, I changed my script. Now it works

Michelle
<body text=#000000>
<center>
<?
print ("<table width=325 border=1 cellspacing=1 cellpadding=10>\n");

print ("<SCRIPT LANGUAGE=\"JavaScript\"> <!--\n");
print ("function entsub(myform) {\n");
print("if (window.event && window.event.keyCode == 13){\n");

print ("myform.submit();\n");
print ("alert(\"uyuyu\");}\n");
print ("else\n");
print ("return true;\n");
print ("}\n");

print ("--> </SCRIPT>\n");
print ("<form name=\"myform\" method=\"POST\" action=\"startup.php\">\n");
print ("<tr>\n");
print ("<td align=\"center\">\n");
print ("<br>\n");
print ("<font face=verdana size=2>Enter Customer Name (Partial or Complete):</font>\n");
print ("<br><br>\n");
Print ("<input type=\"text\" name=\"customer1\" value=\"$customer\" onkeypress=\"return entsub(this.form)\" > \n");
print ("<input type=\"submit\" name=\"submit\" value=\"FIND\">\n");

print ("<br>\n");
print ("<br>\n");
print ("<center><font face=verdana size=2><b>OR</b></font></center>\n");
print ("<br>\n");
print ("<font face=verdana size=2><A HREF=\"./maintenance/customerinfo.php\">View customer list</A></font>\n");
print ("<br>\n");
print ("<br>\n");
print ("</td>\n");
print ("</tr>\n");
print ("</form>\n");

?>
</table>
</center>
</body>

AaronW
07-06-2005, 03:08 PM
Umm, you realise that there's a reason why PHP is embedded in HTML, right? Why are you print()ing the HTML? It's hard to read, hard to edit, and doesn't make any sense.

And you have <table><form><tr></tr></form></table> which is illegal nesting in HTML. Use <form><table><tr></tr></table></form>.

Honestly now...

michelleLiu
07-06-2005, 03:18 PM
Hi, It is very good suggestion. Can you tell me what is simply syntex to embedded HTML or Javascript in php. The way I use does actually bugs me.

Thank you

Michelle

Fou-Lu
07-06-2005, 04:02 PM
<?php

... php code in here...

?>
<html>
.. More html: asking for a variable:
<input type="text" value="<?php echo $varname;?>" />
</html>

<?php
... more php code
?>

There are other ways of course, you can combine a string, use heredoc(sp?) syntax, etc.
Still don't understand the javascript....

[m] at
07-06-2005, 04:10 PM
If you need to dynamically generate HTML depending on user-input, just feed it into a variable in the head, and then print that variable in the body.



<?

$variable = "<dynamically generated html>";

?>

<html>
<head>
</head>
<body>
<static html>

<?
print $variable;
?>

<static html>

</body>


If you want to print html, you don't need to use a new print statement for each line. Just put it all into one print statement, and don't forget to use \n for line breaks where applicable.