Link to home
Start Free TrialLog in
Avatar of Brainstormer
BrainstormerFlag for United States of America

asked on

Adding domain info after username similar to Hotmail at SquirrelMail login page

The title says it all I guess. I am not a JS guru. I am looking for a javascript script that would add the domain after the user name same as Hotmail does on their site. I am trying to modify the SquirrelMail webmail application's /src/login.php file so that a designated @domain.com can be inserted after user types his ID and moves to the password field. This is a requirement of the site's login process. The site is unique and the @... is specific and static, but if anyone can invent something better such as pulling the domain info from the address URL, that's great too.

Since the page has PHP, it may also be easier to add some PHP tags there, but I think JS would be the easiest way to go. If you want to see how Hotmail script works, look at the source in their login page.

Any working solution or workaround would help.

Since I have seen JS scripts implement similar to this with as little as 5 lines, I don't think this will be a majot effort. A similar sample is at http://www.mybannertaxi.com/scripts/echo.html

Avatar of jaysolomon
jaysolomon

maybe this

<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!--
function addEmail(fObj){
      fObj.value = fObj.value +"@jay.com";
}      
// -->
</script>
</head>
<body>
<form name="form1" method="post" action="">
<input type="text" name="email" onChange="addEmail(this);">@jAy.com
</form>
</body>
</html>
wait maybe i missread

Do you want it to do the same thing as the link you posted?
Avatar of Brainstormer

ASKER

You have to remember that it's a PHP file and the form implementation is not done as pure HTML.
yes i know, can you not just use the script i provided and add the onchange to the text box?


this is the complete login.php file contents:


<?php

/**
 * login.php -- simple login screen
 *
 * Copyright (c) 1999-2003 The SquirrelMail Project Team
 * Licensed under the GNU GPL. For full terms see the file COPYING.
 *
 * This a simple login screen. Some housekeeping is done to clean
 * cookies and find language.
 *
 * $Id: login.php,v 1.98.2.1 2003/07/03 20:24:47 stekkel Exp $
 */

/* Path for SquirrelMail required files. */
define('SM_PATH','../');

/* SquirrelMail required files. */
require_once(SM_PATH . 'functions/strings.php');
require_once(SM_PATH . 'config/config.php');
require_once(SM_PATH . 'functions/i18n.php');
require_once(SM_PATH . 'functions/plugin.php');
require_once(SM_PATH . 'functions/constants.php');
require_once(SM_PATH . 'functions/page_header.php');
require_once(SM_PATH . 'functions/html.php');
require_once(SM_PATH . 'functions/global.php');

/*
 * $squirrelmail_language is set by a cookie when the user selects
 * language and logs out
 */
set_up_language($squirrelmail_language, TRUE, TRUE);

/**
 * Find out the base URI to set cookies.
 */
if (!function_exists('sqm_baseuri')){
    require_once(SM_PATH . 'functions/display_messages.php');
}
$base_uri = sqm_baseuri();

/*
 * In case the last session was not terminated properly, make sure
 * we get a new one.
 */

sqsession_destroy();
 
header('Pragma: no-cache');

do_hook('login_cookie');

/* Output the javascript onload function. */

$header = "<script language=\"JavaScript\" type=\"text/javascript\">\n" .
          "<!--\n".
          "  function squirrelmail_loginpage_onload() {\n".
          "    document.forms[0].js_autodetect_results.value = '" . SMPREF_JS_ON . "';\n".
          "    var textElements = 0;\n".
          "    for (i = 0; i < document.forms[0].elements.length; i++) {\n".
          "      if (document.forms[0].elements[i].type == \"text\" || document.forms[0].elements[i].type == \"password\") {\n".
          "        textElements++;\n".
          "        if (textElements == " . (isset($loginname) ? 2 : 1) . ") {\n".
          "          document.forms[0].elements[i].focus();\n".
          "          break;\n".
          "        }\n".
          "      }\n".
          "    }\n".
          "  }\n".
          "// -->\n".
          "</script>\n";
$custom_css = 'none';          
displayHtmlHeader( "$org_name - " . _("Login"), $header, FALSE );

echo '<body text="#000000" bgcolor="#FFFFFF" link="#0000CC" vlink="#0000CC" alink="#0000CC" onload="squirrelmail_loginpage_onload();">' .
     "\n" . '<form action="redirect.php" method="post">' . "\n";

$username_form_name = 'login_username';
$password_form_name = 'secretkey';
do_hook('login_top');

$loginname_value = (sqGetGlobalVar('loginname', $loginname) ? htmlspecialchars($loginname) : '');

/* If they don't have a logo, don't bother.. */
if (isset($org_logo) && $org_logo) {
    /* Display width and height like good little people */
    $width_and_height = '';
    if (isset($org_logo_width) && is_numeric($org_logo_width) &&
     $org_logo_width>0) {
        $width_and_height = " width=\"$org_logo_width\"";
    }
    if (isset($org_logo_height) && is_numeric($org_logo_height) &&
     $org_logo_height>0) {
        $width_and_height .= " height=\"$org_logo_height\"";
    }
}

echo html_tag( 'table',
    html_tag( 'tr',
        html_tag( 'td',
            '<center>'.
            ( isset($org_logo) && $org_logo
              ? '<img src="' . $org_logo . '" alt="' .
                sprintf(_("%s Logo"), $org_name) .'"' . $width_and_height .
                ' /><br />' . "\n"
              : '' ).
            ( (isset($hide_sm_attributions) && $hide_sm_attributions) ? '' :
            '<small>' . sprintf (_("SquirrelMail version %s"), $version) . '<br />' ."\n".
            '  ' . _("By the SquirrelMail Development Team") . '<br /></small>' . "\n" ) .
            html_tag( 'table',
                html_tag( 'tr',
                    html_tag( 'td',
                        '<b>' . sprintf (_("%s Login"), $org_name) . "</b>\n",
                    'center', '#DCDCDC' )
                ) .
                html_tag( 'tr',
                    html_tag( 'td',  "\n" .
                        html_tag( 'table',
                            html_tag( 'tr',
                                html_tag( 'td',
                                    _("Name:") ,
                                'right', '', 'width="30%"' ) .
                                html_tag( 'td',
                                    '<input type="text" name="' . $username_form_name .'" value="' . $loginname_value .'" />' ,
                                'left', '', 'width="*"' )
                                ) . "\n" .
                            html_tag( 'tr',
                                html_tag( 'td',
                                    _("Password:") ,
                                'right', '', 'width="30%"' ) .
                                html_tag( 'td',
                                    '<input type="password" name="' . $password_form_name . '" />' . "\n" .
                                    '<input type="hidden" name="js_autodetect_results" value="SMPREF_JS_OFF" />' . "\n" .
                                    '<input type="hidden" name="just_logged_in" value="1" />' . "\n",
                                'left', '', 'width="*"' )
                            ) ,
                        'center', '#ffffff', 'border="0" width="100%"' ) ,
                    'left', '#FFFFFF' )
                ) .
                html_tag( 'tr',
                    html_tag( 'td',
                        '<center><input type="submit" value="' . _("Login") . '" /></center>',
                    'left' )
                ),
            '', '#ffffff', 'border="0" width="350"' ) . '</center>',
        'center' )
    ) ,
'', '#ffffff', 'border="0" cellspacing="0" cellpadding="0" width="100%"' );
do_hook('login_form');
echo '</form>' . "\n";

do_hook('login_bottom');
echo "</body>\n".
     "</html>\n";
?>
so, is that username that should be appended with domain?

find string:
<input type="text" name="' . $username_form_name .'" value="' . $loginname_value .'" />

and append
<select name="quickhack"><option>@some.com</option><option>@awesome.net</option></select>

than, find
><input type="submit" value="' . _("Login") . '"

replace "submit" with "button" and add
onclick="document.forms[0].elements.' . $username_form_name . '.value+=document.forms[0].elements.quickhack.options[document.forms[0].elements.quickhack.selectedIndex].text"
...****. forgot. last line in previous post, at the very end: .text" should be .text;document.forms[0].submit()"
Sorry makc, I lost you in the code. I tried to patch but did not work. Can you present a more detailed code patch with the new code already in place?

I will try to use jaysolomon's approach. His was correct for HTML, it just needs implementing thru the PHP jungle :-)
changes to login.php:


$header = "<script language=\"JavaScript\" type=\"text/javascript\">\n" .
          "<!--\n".
          "  function addEmail(fObj){ \n".
          "         fObj.value = fObj.value + "@mysite.net"; \n".
          "         } \n".
          "  function squirrelmail_loginpage_onload() {\n".

...

                                    '<input type="text" name="' . $username_form_name .'" value="' . $loginname_value .' onChange="addEmail(this);" />' ,
                                'left', '', 'width="*"' )

but I get a : Parse error: parse error, unexpected '@' in ...\wwwroot\webmail\src\login.php on line 58

I used \@ but that did not work. I took out @ completely, but it gave me another error for unexpected T_STRING

any input?
What you need is the onBlur javascript event handler.  Use it like this:

<?php
if($_POST['submit']=="submit") {
      echo "Username was: $_POST[username]<br>";

}
?>
<html>
<head>
<title>Add Domain Test</title>
<script type="text/javascript" language="Javascript1.2">

function addDomain(element) {

      element.value = element.value + "@hotmail.com";

}

</script>
</head>

<body>


<form action="<?php echo $PHP_SELF; ?>" method="post" name="addDomainTest">
<input type="text" name="username" onBlur="javascript:addDomain(this);"><br>
<input type="text" name="password"><br>
<input type="submit" value="submit" name="submit">
</form>

</body>
</html>
Again, althugh both your answers are correct for simple HTML, I can not implement this in PHP. Either the @ character in @domain.net or the whole @domain.net is causing a PHP parse error.

Parse error: parse error in /home/public_html/mail/src/login.php on line 58

which is:

"  function function addDomain(element) {\n".                                   // added the custom JS    line 57
"        element.value = element.value + "@domain.net";\n".               // in these lines               line 58
"        }\n".                                                                                    // here for domain           line 59

any suggestions with PHP implementation?
I mistakenly typed:
function function addDomain(element) {\n"

the actual code for line 57 is:
function addDomain(element) {\n"
I'm not sure why you are putting the javascript function into the PHP code, but if you are, you do have some parse error:

should be:

"  function function addDomain(element) {\n".                                   // added the custom JS    line 57
"        element.value = element.value + \"@domain.net]\";\n".               // in these lines               line 58
"        }\n".    

notice the way I have escaped the double quotes in line 58 with slashes.  that is why you are getting the parse error.  You need those slashes.
the code from rstorey2079 worked. the working changes are as follows:

$header = "<script language=\"JavaScript\" type=\"text/javascript\">\n" .
          "<!--\n".
          "  function addDomain(element) {\n".
          "        element.value = element.value + \"@domain.net\";\n".
          "       }\n".  
          "  function squirrelmail_loginpage_onload() {\n".
...

html_tag( 'td',
      '<input type="text" name="' . $username_form_name .'" value="' . $loginname_value .'" onChange="javascript:addDomain(this);" />' ,
      'left', '', 'width="*"' )


but one problem I noticed is that the script adds a @domain.net everytime I change something in the field. Example:

I type USRE and the script adds @domain.com
I type the password, but I notice that USRE should have been USER

I make the changes, but after I leave the field, now the text looks like:  USER@domain.net@domain.net

One workaround would be to erase everything in the field and type USER at a blank box, but I am afraid the user may fail to realize what has happend, and can not see all the  USER@domain.net@domain.net
due to the visible character limit in the field.

Anyone with a suggestion to kill that part of the script if @domain.net is already inserted, or make the script a one time use only?

Currently the script lines are inserted inside an existing javascript from the SquirrelMail author. Can I move them outside, so 2 JavaScripts exist in the page, but one has a 1 time use or die call.

Thanks for your help so far.
addition to the above comments:

I assume some users will not know that the script is active and will attempt to add the domain themselves. This will too cause a USERx@domain.net@domain.net
Change the javascript function to this:

function addDomain(element) {

    var str = element.value;

     if(str.search(/\@domain\.net/)==-1) {
     element.value = element.value + "@hotmail.com";
     }

}

Make sure you properly escape any quotes or slashes if you are outputting this from PHP.
ASKER CERTIFIED SOLUTION
Avatar of rstorey2079
rstorey2079

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
it worked like a charm! Thanks to everyone that contributed to this. I will award the points to  rstorey2079 for the great help in JavaScript and the final working code, even thought it was not embedded in PHP. I figured that part out.

final code for the new SquirrelMail /scr/login.php is as follows:

$header = "<script language=\"JavaScript\" type=\"text/javascript\">\n" .
          "<!--\n".
          "  function addDomain(element) {\n".
          "        var str = element.value; \n".
          "             if(str.search(/\@domain\.net/)==-1) { \n".
          "        element.value = element.value + \"@domain.net\";\n".
          "        }\n".  
          "  }\n".  
          "  function squirrelmail_loginpage_onload() {\n".
...

html_tag( 'td',
     '<input type="text" name="' . $username_form_name .'" value="' . $loginname_value .'" onChange="javascript:addDomain(this);" />' ,
      'left', '', 'width="*"' )
I added some code to check if the input string contains '@' and replace everything after '@' with the default domain:

$header = "<script language=\"JavaScript\" type=\"text/javascript\">\n" .
          "<!--\n".
          "  function addDomain(element) {\n".
          "        var str = element.value; \n".
          "        var field = str.split('@',2); \n".
          "             if(str.search(/\@domain\.net/)==-1) { \n".
          "        element.value = field[0] + \"@domain.net\";\n".
          "        }\n".  
          "  }\n".  
          "  function squirrelmail_loginpage_onload() {\n".
Thanks erisch, I have since moved on from SquirrelMail to Google Apps for my mail, but hopefully this is useful to others that still use it.