Simple onfocus command

focus

Member
Messages
128
Reaction score
0
Points
16
At the moment i have this:

<textarea name="comment" cols="55" rows="8">Please feel free to leave a comment or a question here</textarea>

I want it to go blank when the user click on it.


Can anyone help me with this?
 

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
HTML:
<textarea name="comment" cols="55" rows="8" onclick="this.value=''">Please feel free to leave a comment or a question here</textarea>
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
1. Textarea does not have onclick event
2. If it did, that would clear the area even when a user went back to edit the area

HTML:
<html>
<head><title>Test</title>
<script type="text/javascript">
var flag = true ;
function clear_me(){
  if( flag ){
      var t = document.getElementById( 'comm' );
      t.defaultValue  = '' ;
      flag = false;
  }
}
</script>
<body>
<textarea name="comment" cols="55" rows="8" id="comm" 
onfocus="clear_me()" ;>Please feel free to leave a comment or a question here</textarea>
</body>
</html>
 

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
Ha, how could I forget that. :confused:
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Ha, how could I forget that. :confused:

Not your mistake, mine. :confused:
It does have an onclick event.


But the part about needing a flag to disable it after the first use is valid. Plus, onfocus works even in the case they use TAB to get to the textarea rather than by clicking.

Hmmm.....under further testing, using 'defaultValue' seems to remove the need for the flag, while 'value' does require it.
 
Last edited:

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
Hmmm.....under further testing, using 'defaultValue' seems to remove the need for the flag, while 'value' does require it.
You mean something like this:
HTML:
<textarea name="comment" cols="55" rows="8" id="comm" 
onfocus="if(this.value == this.defaultValue){this.value = ''}">Please feel free to leave a comment or a question here</textarea>
I have no idea if that works on text areas though.
 

focus

Member
Messages
128
Reaction score
0
Points
16
Thanks works fine :)

I have one other thing, thought I'd ask here instead of making a new thread...


How do i get the below to work?


if (document.orderform.terms.value== false) {
alert("You must agree to our terms and conditions below an order can be processed")
document.orderform.terms.focus()
return false
}

HTML:
<p>By clicking on the below you have read and agree to our terms and conditions</p>
<input type="checkbox" name="terms" /><p>I Agree</p>
 

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
You'll want to use the '.checked' term, instead of '.value' to see if a box is checked. You may also have problems with your referencing of the DOM objects.
 

focus

Member
Messages
128
Reaction score
0
Points
16
I tried the below and it still doesn't work :((

if (document.orderform.terms.checked== false) {
alert("You must agree to our terms and conditions below an order can be processed")
return false
}



What do you mean by DOM objects?
 

as4s1n

New Member
Messages
174
Reaction score
4
Points
0
try:
Code:
var checkBox = document.forms.orderForm.elements.terms;
if(checkBox.checked == false) {
    // Run code
}
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
if (document.orderform.terms.checked== false) {
alert("You must agree to our terms and conditions below an order can be processed")
return false
}

When posting, use
PHP:
, [html] or [code] tags (as appropriate) to separate and format code:
[code]if (document.orderform.terms.checked== false) {
    alert("You must agree to our terms and conditions below an order can be processed");
    return false;
}[/code]

[quote="focus, post: 681413"]What do you mean by DOM objects?[/QUOTE]
The [URL="http://www.w3.org/DOM/"]Document Object Model[/URL] is the standard that defines the capabilities of document objects (e.g. HTML elements and attributes). In this case, accessing the form with [FONT="Courier New"]document.orderform[/FONT] isn't covered by the DOM. It will work in IE, but fail in other browsers. [FONT="Courier New"]document.forms.orderform[/FONT] is the cross browser way of accessing the form, and is covered in DOM levels 1 and 2.

However, a better approach is to pass the form to the validation function:

[html]<form name="orderForm" action="..." onsubmit="return validate(this)">
  ...[/html]
[code]function validate(form) {
  ...
  // comparisons with 'false' is almost always bad form
  if (! form.elements.terms.checked) { 
    // by abstracting out the alert function, you can display notices 
    // in whichever manner you wish: alerts, [URL="http://www.google.com/search?q=inline+form+validation"]in-line messages[/URL], ...
    notice(form.elements.terms, 'You must agree to our terms and conditions [COLOR="red"]before[/COLOR] an order can be processed');
    form.elements.terms.focus();
    return false;
  }
  return true;
}[/code]
 
Last edited:

focus

Member
Messages
128
Reaction score
0
Points
16
Thanks. It's working perfectly :)

I was wondering about my PHP after that as below

I have it so it gets sent to me via email...

Terms & Conditions:
I $fullname, agree with the terms & conditions.

at the moment it turned out like this:

Terms & Conditions:
I have Peter
, agree with the terms & conditions.

I was wondering how do i get it on the same line?
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
You can use trim() to remove leading & trailing whitespace, including newlines.

Make sure you validate the form server-side in addition to client side.
 

focus

Member
Messages
128
Reaction score
0
Points
16
Apologies. The last thing i require with this is to Cc the from_email in. How would i go about doing that?

Below is the PHP Code.


PHP:
 <?php 
	

function is_valid_email($from_email)
{
    return preg_match('#^[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([^\s\'"<>]+\.+[a-z]{2,6}))$#si', $from_email);
} 


$headers =  	"From:xxxx@hotmail.com\r\n";

$headers .= 	"MIME-Version: 1.0\r\n"
  	. "Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
	. "Content-Transfer-Encoding: 7bit\r\n"; 
	
$to_email = xxxx@hotmail.com";
$subject = "Order";
$productid.=$_POST['productid']."\n" ;
$sneakername.=$_POST['sneakername']."\n" ;
$fullname.=$_POST['fullname']."\n" ;
$size.=$_POST['size'] ."\n" ;
$address.=$_POST['address'] ."\n" ;
$suburb.=$_POST['suburb']."\n" ;
$city.=$_POST['city']."\n" ;
$state.=$_POST['state'] ."\n" ;
$postcode.=$_POST['postcode']."\n" ;
$phone2.=$_POST['phone2']."\n" ;
$phone.=$_POST['phone'] ."\n" ;
$from_email.=$_POST['from_email']."\n" ;
$payment.=$_POST['payment']."\n" ;
$comment.=$_POST['comment']."\n" ;




$message = "


<body>



<b>Product ID:</b><br>
$productid

<br> <br>

<b>Sneaker Name:</b><br>
$sneakername

<br> <br>

<b>Full Name:</b><br>
$fullname

<br> <br>

<b>Shoe Size:</b><br>
$size

<br> <br>

<b>Street Address:</b><br>
$address
	
<br> <br>

<b>Suburb:</b><br>
$suburb
	
<br> <br>

<b>City:</b><br>
$city

<br> <br>
	
<b>State:</b><br>
$state
	
<br> <br>

<b>Post Code:</b><br>
$postcode

<br> <br>	

<b>Contact Number</b><br>
$phone

<br> <br>
	
<b>Email:</b><br>
$from_email
	
<br> <br>

<b>Payment Method:</b><br>
$payment
	
<br> <br>

<b>Comment:</b><br>
$comment

<br> <br>

<b>Terms & Conditions:</b><br>
I $fullname, have read and agreed with the terms & conditions.






</body>
";
    
	
$sent = mail($to_email, $subject, $message, $headers) ;
if($sent)
{print "Your mail was sent successfully."; }

else
{print "We encountered an error sending your mail"; }

?>
 
Last edited:

dlukin

New Member
Messages
427
Reaction score
25
Points
0
PHP:
$headers  .=      "CC: $from_email\r\n";

Should work. But make sure the $from_email is valid.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
But make sure the $from_email is valid.
One very important reason to do this is to prevent an injection attack. Make sure $from_email is sanitized. There isn't much damage possible with an e-mail injection, but there is some.
 

focus

Member
Messages
128
Reaction score
0
Points
16
Would the below also work for changing the name of from where its sent from?

PHP:
$headers =      "From:$from_email\r\n";


Also just out of curiosity, whats the worse that can happen if an email has an injection?
 
Last edited:

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
The worst that could happen is that they use your script to send hundreds or thousands of spam emails from your email address. That could lead to it being placed on a spam list, but it will also cause your site to be suspended for sending too many emails (on free hosting you are only allowed 100/hour).
 
Top