Log in

View Full Version : form field onclick



chechu
04-29-2008, 01:29 PM
Hey all.
Below you will see the code of my email form. The first two fields are set that when you click in the field, the text ("name", "email adress") disappears. I'd like to do the same with the textarea, as now "message" remains while clicking in the field.


<script language="JavaScript" type="text/JavaScript">
function clearDefault(el) {
if (el.defaultValue==el.value) el.value = ""
}
</script>
<FORM NAME="form1" METHOD="post" ACTION="contact.php">
<input type="hidden" name="action" value="send">
<INPUT TYPE=TEXT NAME="name" VALUE=" name" onFocus="clearDefault(this)" style="border-top:0px; border-left:0px; border-right:0px; border-bottom: 1px dotted #cc6600; width:150px">
<br>&nbsp;<br>
<INPUT TYPE=TEXT NAME="email" VALUE=" email adress" onFocus="clearDefault(this)" style="border-top:0px; border-left:0px; border-right:0px; border-bottom: 1px dotted purple; width:150px;">
<br>&nbsp;<br>
<TEXTAREA NAME="message" style="border-top:0px; border-left:1px dotted #0066cc; border-right:0px; border-bottom: 1px dotted #0066cc; width:150px; height:63px"> message</TEXTAREA>
<br>&nbsp;<br>
<INPUT TYPE="image" SRC="images/sendarrow.gif" VALUE="send">
<img src="images/clear.gif" onclick="document.getElementById('form1').reset();" style="cursor:pointer"/>
</FORM>


Then my next question is how to adjust the .php


<?php

if ($_POST["action"] == "send"){

if ($_POST[name] != " name" and $_POST[name] != "" and $_POST[email] != " e-mail adress" and $_POST[email] != "" and $_POST[message] != "") {
mail ("info@site.com", "via site",
"
Name: ".$_POST['name']."
E-mail: ".$_POST['email']."
Message: ".$_POST['message']."

",
"From: ".$_POST['name']." <".$_POST['email'].">");

$subject = "your message to xx";

$msg = "

This is an automatically sent email. Please do not reply.

Dear $_POST[name],

Thanks for your message to xx.
She will get back to you as soon as possible.

This was your message:
$_POST[message]
";

mail($_POST[email], $subject, $msg);

echo 'Thanks ! Your message has been sent, and you will receive a confirmation mail. <br><br> We will get back to you as soon as we can.<br>&nbsp;<br><br>';

}

else{
echo '<font color="#FF0000">Please fill in all data!</font><br><br>Your name, email adress and message are mandatory fields.<br><br><a href="contact.html"><font color="#FF0000">[again please]</font></a><br>&nbsp;<br>';
}
}
?>

Nile
04-29-2008, 09:24 PM
Its very simple, just add the function you've got on the inputs to the textarea:


<script language="JavaScript" type="text/JavaScript">
function clearDefault(el) {
if (el.defaultValue==el.value) el.value = ""
}
</script>
<FORM NAME="form1" METHOD="post" ACTION="contact.php">
<input type="hidden" name="action" value="send">
<INPUT TYPE=TEXT NAME="name" VALUE=" name" onFocus="clearDefault(this)" style="border-top:0px; border-left:0px; border-right:0px; border-bottom: 1px dotted #cc6600; width:150px">
<br>&nbsp;<br>
<INPUT TYPE=TEXT NAME="email" VALUE=" email adress" onFocus="clearDefault(this)" style="border-top:0px; border-left:0px; border-right:0px; border-bottom: 1px dotted purple; width:150px;">
<br>&nbsp;<br>
<TEXTAREA NAME="message" style="border-top:0px; border-left:1px dotted #0066cc; border-right:0px; border-bottom: 1px dotted #0066cc; width:150px; height:63px" onFocus="clearDefault(this)"> message</TEXTAREA>
<br>&nbsp;<br>
<INPUT TYPE="image" SRC="images/sendarrow.gif" VALUE="send">
<img src="images/clear.gif" onclick="document.getElementById('form1').reset();" style="cursor:pointer"/>
</FORM>

And for the PHP do the same you did with the rest of the post data.

chechu
04-30-2008, 06:45 AM
if ($_POST[name] != " your name" and $_POST[name] != "" and $_POST[email] != " your e-mail adress" and $_POST[email] != "" and $_POST[message] != " your message" and $_POST[message] != "" ) {

This one seems not to work. When I leave the "your message" but filll in the rest, the message gets send.

Nile
04-30-2008, 11:37 AM
From in your code above, I'm guessing that you don't want "your message", or a nothing in the input field, right? So I don't gets what wrong?

chechu
04-30-2008, 11:52 AM
The fields "your name", "your email adress" and "message" need to be filled in. Now, when someone doesn't fill in anything, the mail gets sent anyway.
I want the email to be sent on two conditions:
- all fields need to be filled in
- they need to be filled in with other text than "your name", "your email adress" and "message"
Hope you can help me out !

codeexploiter
04-30-2008, 11:52 AM
When you submit a form all the elements exists in that form will be submitted to the server. If you don't want a particular form field you can adjust your PHP code that handles the incoming form fields.

You can perform a client-side validation and submit the form only if that part succeeds. Using onsubmit event in the form element, you can accomplish that.

Medyman
04-30-2008, 10:54 PM
The fields "your name", "your email adress" and "message" need to be filled in. Now, when someone doesn't fill in anything, the mail gets sent anyway.
I want the email to be sent on two conditions:
- all fields need to be filled in
- they need to be filled in with other text than "your name", "your email adress" and "message"
Hope you can help me out !

Are you trying to prevent "message" or "your message" from being submitted? Might be a simple oversight in typing, but if not...that might be what's causing the problem.

chechu
05-02-2008, 10:00 AM
Are you trying to prevent "message" or "your message" from being submitted?
Yes, that's what I am trying. I have adjusted the php code to "your message" instead of "message".
Codeexploiter, I have no idea what you are talking about.
The code worked fine, untill I added the "message" onclick function. Now the mail gets sent even when I don't fill in anything.
This is what it needs to do:
I want the email to be sent on two conditions:
- all fields need to be filled in
- they need to be filled in with other text than "your name", "your email adress" and "message"
Rather common actions, except that the onclick function is added. Please advise !

Medyman
05-02-2008, 04:13 PM
if ($_POST[name] != " name" and $_POST[name] != "" and $_POST[email] != " e-mail adress" and $_POST[email] != "" and $_POST[message] != "") {

Don't you want to be using "or" statements instead of "and"? And would mean that all of those statements need to be true for that conditional to be met. I *think* what you want to do is stop the form from submitting if any of those conditions are met. Correct?

If so, replace all instances of "and" with the or operator (||)

Example..

$_POST[email] != "" || $_POST[message] != ""

chechu
05-02-2008, 06:57 PM
Don't you want to be using "or" statements instead of "and"? And would mean that all of those statements need to be true for that conditional to be met. I *think* what you want to do is stop the form from submitting if any of those conditions are met. Correct?

I don't really understand your English in this one, sorry. I don't really know how to explain this. Right now when someone hits the "send" button, the message gets sent, like this:

name: your name
email adress: your email adress
message: your message
I don't want that; I want all fields to be filled in, with other than the words already written in the form (your name, your email adress, your message). If this is not done correctly, an error message should be displayed.
Should be possible, even with the onclick function ?

Medyman
05-03-2008, 09:29 PM
Let me try to explain it another way.

The way your PHP is coded right now is this:


if ($_POST[name] != " name" and $_POST[name] != "" and $_POST[email] != " e-mail adress" and $_POST[email] != "" and $_POST[message] != "") {


This means that if all of the following happen, an error will result:

1. Name does not equal " name"
2. Name is not blank
3. Email does not equal " e-mail address"
4. Email is not blank
5. Message is not blank

if all of those are true, an error will result because you're using the "and" operator.

I think you want an error if any of those happen. So you would need to change the "and" operator to "or".

chechu
05-04-2008, 03:55 PM
if ($_POST[name] != " je naam" || $_POST[name] != "" || $_POST[email] != " je e-mail adres" and $_POST[email] != "" || $_POST[message] != " je bericht" || $_POST[message] != "" ) {

I changed it into the above, but still doesn't function as I wish.
The form should not be sent if
1. Name equals " your name"
2. Name is blank
3. Email equals " your e-mail address"
4. Email is blank
3. Message equals " your message"
5. Message is blank

ReadyToLearn
05-04-2008, 04:04 PM
I do not know very much PHP but shouldn't there be quotes around the post variable.

Like this:

$_POST['email']

Nile
05-04-2008, 04:08 PM
It doesn't matter really, with all arrays.

chechu
05-06-2008, 04:44 PM
Can anyone please advise.

chechu
05-09-2008, 07:23 AM
Really need some help with this one !

kuau
05-11-2008, 09:21 PM
I admit to knowing almost zero about javascript, but it seems to me you are making it more complicated than necessary. To simplify, don't put default text in the fields - that's what the labels are for. (although I would change adress to address)

This is what I use (you can add one for the message field)....

<script language=Javascript type="text/javascript">
function validate() {
if (document.infoform.name.value == "") {
alert("Please fill in your name.");
return false;
}
if (document.infoform.email.value == "") {
alert("Please fill in your email address.");
return false;
}
return true;
}
</script>

Then in the form call the function with onsubmit. Same result, way simpler.


<form name="infoform" method="POST" action="/php/contact-form.php" class="infoform" onsubmit="return validate()" >

Hope this helps. erin :)

chechu
05-11-2008, 09:39 PM
Hey Erin. Thanks for your input. But you're talking about Javascript, while I guess the real issue here is php, no ?
In your example, how does the php-page look like ?

although I would change adress to address
Depends where your customers are from, but that is a detail.

kuau
05-11-2008, 09:54 PM
My method doesn't let them submit the form unless the fields are filled in. Yours makes the decision based on what they submit, so presumably they would have to start over.

My php file called by the form sends an email to the customer telling them they will receive a reply within 24 hours, and then sends an email to the site owner as follows:

// Send email to site owner

$body2 = "Please respond to this website inquiry from:\n\n";
$body2.= "Name : ".$_POST['name']."\n";
$body2.= "Email : ".$_POST['email']."\n";
$body2.= "Inquiry : ".strip_tags($_POST['inquiry'])."\n";

$to = "Your Name<info@website.com>";
$subject2 = "Website Inquiry";
$headers2 = "From: Your Website<info@website.com>\n";
mail($to, $subject2, $body2, $headers2);

Is that what you were asking? Erin :)

chechu
05-11-2008, 10:02 PM
My method doesn't let them submit the form unless the fields are filled in. Yours makes the decision based on what they submit, so presumably they would have to start over.

Thanks for your solution, but I really need it this way. Indeed they would need to start over. Can you adjust the existing code (see above in this conversation), and adjust it to these needs:

The form should not be sent if
1. Name equals " your name"
2. Name is blank
3. Email equals " your e-mail address"
4. Email is blank
3. Message equals " your message"
5. Message is blank

chechu
05-16-2008, 03:30 PM
Administrator, could you move this thread to the php section, please, as it would be more in its place there. Could anyone please help ?

chechu
05-24-2008, 04:48 PM
This is a solution someone gave me, but it still doesn't work


<?php
function check_emptyness ($array) {
$return = array();
if(is_array($array)) {
foreach($array as $key => $value) {
if(is_array($value)) {
$return[] = check_emptyness($value);
} else {
$return[] = empty($value);
}
}
} else {
$return[] = false;
}

if(in_array(false, $return)) {
return false;
} else {
return true;
}
}

if($_SERVER['REQUEST_METHOD'] == "POST") {
if(!check_emptyness($_POST)) {
// niet leeg
if(trim($_POST['naam']) != "je naam" && trim($_POST['email']) != "je email" && trim($_POST['message']) != "je bericht") {
// alles anders ingevuld

$jouwmail = 'info@me.com';
$cSubject = 'Via site';
$cBericht = 'Naam: '.$_POST['naam'].'\n';
$cBericht .= 'E-mailadres: '.$_POST['email'].'\n';
$cBericht .= 'Bericht: '.$_POST['message'].'\n';
$headers = 'From: '.$_POST['naam'].' <'.$_POST['email'].'>'.'\r\n';

if(mail($jouwmail, $cSubject, $cBericht)) {
// mail naar ceci is verstuurd
$bSubject = 'Bevestiging ontvangst mail aan Ceci Casariego-Mazereel';
$bBericht = "Dit is een automatisch verzonden e-mailbericht. Gelieve hierop niet te antwoorden.".'\n\n';
$bBericht .= "Beste ".$_POST['naam'].",".'\n\n';
$bBericht .= "Bedankt voor je bericht aan Ceci Casariego-Mazereel. Ze beantwoordt zo snel mogelijk.".'\n'."Dit was je bericht: ".$_POST['message'];
if(mail($_POST['email'], $bSubject, $bBericht)) { // headers??
// bevestigingsmail is verzonden
echo 'Bedankt! Je bericht is verzonden en je hebt een bevestiging ontvangen in je mailbox. Ceci antwoord z.s.m. je bericht.';
} else {
// bevestigingsmail is niet verzonden
echo 'Bedankt! Je bericht is verzonden. Ceci antwoord z.s.m. je bericht.';
}
} else {
// mail naar ceci is niet verstuurd
echo 'Helaas... Je bericht is niet verzonden. Probeer het nogmaals.';
}
} else {
// niet anders ingevuld, geef foutmelding
echo 'Vul je wel alles in? Probeer het nogmaals.';
}
} else {
// leeg
echo 'Vul je wel alles in? Probeer het nogmaals.';
}
}

?>
The form should not be sent if
1. Name equals " your name"
2. Name is blank
3. Email equals " your e-mail address"
4. Email is blank
3. Message equals " your message"
5. Message is blank

Could anyone please help ?