i need help in my project,
i need to get the mous focus on the username text field in the admin page
but the problem is the body & head of the page is in another page
so when i use onload="focus();" and get the element by id e cant see it

is their any other way to get mouse focus without using the onload method???

Recommended Answers

All 14 Replies

i need help in my project,
i need to get the mous focus on the username text field in the admin page
but the problem is the body & head of the page is in another page
so when i use onload="focus();" and get the element by id e cant see it

is their any other way to get mouse focus without using the onload method???

You mean like frames?

what do u mean by frames ??
i mean i have the <head> and <body> tags in a script called X.php and the body of this script includes a script called (admin.php) this admin.php is the one containing my text field i want to get focus ON. that's what i mean

what do u mean by frames ??
i mean i have the <head> and <body> tags in a script called X.php and the body of this script includes a script called (admin.php) this admin.php is the one containing my text field i want to get focus ON. that's what i mean

That should work just fine. Post your scripts.

If I am understanding you correctly, the following should work:
In the script containing the <head> add something like this inside the <head></head> tags:

<?
$currentFile = $_SERVER["SCRIPT_NAME"];
$parts = Explode('/', $currentFile);
$currentFile = $parts[count($parts) - 1];

if($currentFile == 'admin.php') {
  //put JS code here
}
?>

This will mean that the JavaScript code will only be loaded with the admin.php script and you can then use the onload function.

<?
$currentFile = $_SERVER["SCRIPT_NAME"];
$parts = Explode('/', $currentFile);
$currentFile = $parts[count($parts) - 1];

if($currentFile == 'admin.php') {
  //put JS code here
}
?>

Instead we can use simply:

$currentFile=basename($_SERVER['PHP_SELF']);

Instead we can use simply:

$currentFile=basename($_SERVER['PHP_SELF']);

That would be the better way, yes. Forgot about that one :)

U know i tried what u said but nothing happens ??
i want to ask sth.
does the admin.php script needs 2 be in the same directory as the script X.php ..
and i also copied it to the same directory but nothing happens

U know i tried what u said but nothing happens ??
i want to ask sth.
does the admin.php script needs 2 be in the same directory as the script X.php ..
and i also copied it to the same directory but nothing happens

It doesn't matter, it's all html when it come out anyway. I can include 100 php files from any directory I want and the javascript dom has access to the whole page. Again, post your scripts.

<? ob_start();?>
<?session_start() ;

include("include/connection.php") ;

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>Untitled</title>



<?

 $currentFile= basename($_SERVER['PHP_SELF']);
 $parts = Explode('/', $currentFile);
 $currentFile = $parts[count($parts) - 1];
 if($currentFile == 'login.php')
     {
         echo " <SCRIPT TYPE='text/javascript'>
             function focus(){
          document.form1.getElementById('1').focus();
                       }

                </SCRIPT> ";

      }


?>





<meta http-equiv="content-type" content="text/html; charset=UTF-8">



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

<script type="text/JavaScript">





function MM_swapImgRestore() { //v3.0

  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;

}



function MM_preloadImages() { //v3.0

  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();

    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)

    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}

}



function MM_findObj(n, d) { //v4.01

  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {

    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}

  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];

  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);

  if(!x && d.getElementById) x=d.getElementById(n); return x;

}



function MM_swapImage() { //v3.0

  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)

   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}

}

function focus(){
 document.form1.getElementById('1').focus();
      }





</script>

</head>

<body onLoad="MM_preloadImages('images/m1_1.gif','images/m2_2.gif','images/m3_3.gif','images/m4_4.gif','images/m5_5.gif','images/m6_6.gif');focus()">



<table class="bg">

    <tr>

        <td class="w">

            <table class="w">

                <tr>

                    <td style="width:766px" class="bg_v">

                        <table style="width:766px">

                            <tr>

                              <td>

                                <table style="height:76px">

                                    <tr>

                                        <td width="587">

                                            <div style="margin:21px 0 0 78px">

                                                <a href="index.html"><img alt="" src="images/logo.gif" border="0"></a><br>

                                          </div>

                                        </td>

                                        <td width="179" class="header">

                                            <br style="line-height:28px">

                                            <a href="index.html" style=" margin-right:38px">home</a><a href="#" style="margin-right:36px">search</a><a href="#">help</a><br>

                                        </td>

                                    </tr>

                                </table>

                              </td>

                            </tr>

                            <tr>

                                <td>

                                    <table style="height:193px">

                                        <tr>

                                            <td width="536"></td>

                                            <td width="230">

                                                <br style="line-height:16px">

                                                <a href="index-1.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image6','','images/m1_1.gif',1)"><img src="images/m1.gif" alt="" name="Image6" width="230" height="26" border="0"></a><a href="index-2.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image7','','images/m2_2.gif',1)"><img src="images/m2.gif" alt="" name="Image7" width="230" height="26" border="0"></a><a href="index-3.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image8','','images/m3_3.gif',1)"><img src="images/m3.gif" alt="" name="Image8" width="230" height="26" border="0"></a><a href="index-4.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image9','','images/m4_4.gif',1)"><img src="images/m4.gif" alt="" name="Image9" width="230" height="26" border="0"></a><a href="index-5.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image10','','images/m5_5.gif',1)"><img src="images/m5.gif" alt="" name="Image10" width="230" height="26" border="0"></a><a href="index-6.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image11','','images/m6_6.gif',1)"><img src="images/m6.gif" alt="" name="Image11" width="230" height="23" border="0"></a><br>

                                            </td>

                                        </tr>

                                    </table>

                                </td>

                            </tr>

                        </table>

                    </td>

                </tr>

            </table>

        </td>

    </tr>

    <tr>

        <td height="206" class="w">

            <table class="w">

                <tr>

                  <td>

                  <?

                  if($_SESSION['utype']=='admin')

            {

            ?>

            <table width="959">

  <tr><td width="677" class="headline">welcome  <? echo $_SESSION['uName'] ;?></td><td width="162" class="myfont"><a href="admin/logout.php">Log out</a></td>

            </tr></table>



    <table width="963" border="0">

  <tr>

    <td width="200" valign="top" class="fontie"><? include("admin/includes/links.php") ;?></td>

    <td width="753" valign="top" class="myfont"><? include("admin/includes/adminlinks.php") ;?></td>

  </tr>

</table>

            <?

            }

            else

            {

            [COLOR="Red"]include("login.php") ;[/COLOR]

            }

            ?>

                  </td>

                </tr>
.....
the LOGIN.PHP (or ADMIN.PHP)
 <?

if($_GET['mode']=='')

  {

?>

<form name="form1" action="index2.php?action=login&&mode=submit" method="post">

<?

if($_GET['log']=='fail')

     {

?>

<p class="mytext2">Sorry this username or password must be wrong</p>

<?

      }

?>


<table height="27" border="0">

  <tr >

    <td width="61" class="username">Username</td>

    <td width="54"><input name="UserName" type="text"  id="1" size="18"  class="input"/></td>

    </tr>

    <tr>

    <td width="60" class="pass">Password</td>

    <td width="54" ><input name="Password" type="password" size="18" class="input"/></td>

    </tr>

    <tr>

    <td width="167">  <input type="submit" name="Submit" value="Login"  class="input" /></td>



  </tr>

</table>

</form>


<?

        }

else if($_GET['mode']=='submit')

     {

$UserName      = $_POST['UserName'];

$Password      = $_POST['Password'];

$encPassword=md5($Password);

$sql="SELECT * FROM admin where username='$UserName' and password='$encPassword' " ;

$result=mysql_query($sql);

if($array=mysql_fetch_array($result))

  {

$_SESSION['uName']=$array[1];

$_SESSION['uID']=$array[0] ;

$_SESSION['utype']='admin' ;

?>

<META HTTP-EQUIV='Refresh' CONTENT='0; URL=index2.php'>

<?

     }

else

 {

?>

<META HTTP-EQUIV='Refresh' CONTENT='0; URL=index2.php?log=fail'>

<?

   }

      }

?>

You have two lines that look like this: document.form1.getElementById('1').focus(); Once I changed it to: document.getElementById('1').focus(); It worked fine.

Im So Glad It Works With You :)
But i dont know why its not working with mee
inaddition to that
at the bottom there is a message that says "Error On Page"

Open up the error console of firefox, clear the errors out of it, do a test run of your application and tell me what the errors are.

Excuse Me But im not using FireFox im using Opera and IE7 ..
and i dont know how to open the error console

I don't know about opera since I do most of my debugging in firefox and ie, but the error console in firefox is under the tools -> error console. It is very helpful when debugging javascript.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.