Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10

Thread: Keypress event

  1. #1
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Keypress event

    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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,
    Thank you. Could you give me more hint on Javascript?

    Michelle

  • #4
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Still does not work

    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>

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #6
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    I got it now

    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>

  • #7
    Senior Coder
    Join Date
    Feb 2003
    Location
    Ontario, Canada
    Posts
    1,223
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...

  • #8
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    PHP Code:
    <?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....

  • #10
    New Coder
    Join Date
    Apr 2005
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Code:
    <?
    
    $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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •