Jump to content

onfocus question for input box ?


Dicko_md
Go to solution Solved by zmasta101,

Recommended Posts

This is my current code but if I try and swap the code

function FormLogin() {
	$html = "
               <div id='login'>
		<form method='post' name='forml' action='index_1.php?s=login'>
              <input type='hidden' name='dologin' value='1'>
              <table cellpadding='2'>
		<tr><td class='formlabel'>Email:</td><td><input type='text' name='email' value='EMAIL' style='width: 100%'></td></tr>
		<tr><td class='formlabel'>Password:</td><td><input type='password' name='password' style='width: 100%'></td></tr>
		<tr><td> </td><td><input type='submit' class='submit' name='login' value='SIGN IN' id='submit' style='width: 100%'></td></tr>
		<tr><td> </td><td><p align='center'><a class='dark' href='index.php?s=remindpass'>FORGOT PASSWORD</a></p></td></tr>
		</table>
<br /></div>
		<script language='javascript'>
			document.forml.email.focus();
		</script>
		
		</form>
	";
	return $html;
}

with the below it doesn't work. I have tried swapping the " with ' and that didn't work. Have I missed something as the below code works in a normal php file .

<form method='post' name='forml' action='index_1.php?s=login'>
<input type='hidden' name='dologin' value='1'>
<input type="text" name="email" value="email" onfocus="if (this.value=='email') this.value='';"/>
<input type="password" name="password" value="password" onfocus="if (this.value=='password') this.value='';"/>
<input type="submit" name="login" id="submit" value="SIGN IN" class="submit">
<a class="dark" href='index_1.php?s=remindpass'>FORGOT PASSWORD</a>
</form>

Thanks in advance Martyn

Link to comment
Share on other sites


<form method='post' name='forml' action='index_1.php?s=login'>
<input type='hidden' name='dologin' value='1'>
<input type='text' name='email' value='email' onfocus="if (this.value=='email') this.value='';"/>
<input type='password' name='password' value='password' onfocus="if (this.value=='password') this.value='';"/>
<input type='submit' name='login' id='submit' value='SIGN IN' class='submit'>
<a class="dark" href='index_1.php?s=remindpass'>FORGOT PASSWORD</a>
</form>

Ive just tried this in the normal php file and it works fine.

When I tried taking off the " off the onfocus to ' it left the EMAIL and ****** in the boxes.

Link to comment
Share on other sites

I took your code and ran it and it worked just fine.  When I tabbed into the email field the prompt went away.

although I did add an onload to the body tag to get the focus to happen automatically (and some ids).

 

Show us what your "swapped" version looks like.

Link to comment
Share on other sites

This is what I tried.

function FormLogin() {
	$html = "
               <form method='post' name='forml' action='index_1.php?s=login'>
<input type='hidden' name='dologin' value='1'>
<input type='text' name='email' value='email' onfocus="if (this.value=='email') this.value='';"/>
<input type='password' name='password' value='password' onfocus="if (this.value=='password') this.value='';"/>
<input type='submit' name='login' id='submit' value='SIGN IN' class='submit'>
<a class="dark" href='index_1.php?s=remindpass'>FORGOT PASSWORD</a>
		<script language='javascript'>
			document.forml.email.focus();
		</script>
		
		</form>
	";
	return $html;
}

Ive tried with and without the script. Im guessing it is something with the " on the onfocus as its closing the line.

Ive also tried \" also

 

Thanks Martyn

Link to comment
Share on other sites

Yes - you will need to escape that double quote for the onfocus part. So - what isn't working? Are you getting the $html var set with the content of the form? Have you echoed it to see what it looks like? If you are getting the content, what are you doing with it next? What isn't working???

Link to comment
Share on other sites

you need to escape the double quotes on the onfocus= because it is competing with the starting " for the whole string.

 

LIke this:

 

onfocus=\"if (this.value=='password') this.value='';\"/>

 

You're only getting the default html and body tags because your $html var is not valid because of the quote marks confusion.

Link to comment
Share on other sites

  • Solution
<?php
function FormLogin() {
	$html = "
               <form method='post' name='forml' action='index_1.php?s=login'>
<input type='hidden' name='dologin' value='1'>
<input type='text' name='email' value='email' onfocus=\"if (this.value=='email') this.value='';\"/>
<input type='password' name='password' value='password' onfocus=\"if (this.value=='password') this.value='';\"/>
<input type='submit' name='login' id='submit' value='SIGN IN' class='submit'>
<a class='dark' href='index_1.php?s=remindpass'>FORGOT PASSWORD</a>
		<script language='javascript'>
			document.forml.email.focus();
		</script>
		
		</form>
	";
	return $html;
}

echo FormLogin();
?>

The above example works. Since you are only returning $html in the function, make sure you are using either print or echo so it will output to the screen :)

Link to comment
Share on other sites

You don't understand the usage of quotes and double quotes.  If you begin a string with one type of quote (single or double) you can't use that same kind of quote again in the string without escaping it (preceding it with a \).  Should you need to quote something always use the other type of quote to do so.  So, in your case, when you start a string with a double quote ($html = "....), you have to use single quotes to encase any other strings inside meaning that the parms of your <a> tag are quoted incorrectly.

 

IMHO it is preferable to always use double quotes on strings since any php var used inside will then be evaluated without having to break the string up.  Anything inside the string requiring quotes can always use the single quote, again avoiding having to use escape sequences.

 

$var = "<input type='text' name='name' value='$myvar'>";

 

will always work while

$var = '<input type="text" name="name" value="$myvar">';

will not do what you want since the outer quotes were single and thus the php var will show up literally as '$myvar'.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.