Page 1 of 3 123 LastLast
Results 1 to 10 of 22

Thread: form field onclick

  1. #1
    Join Date
    Jul 2006
    Location
    Antwerp, Belgium (Europe)
    Posts
    927
    Thanks
    121
    Thanked 2 Times in 2 Posts

    Default form field onclick

    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.

    Code:
    <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

    Code:
    <?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>';
    }
    }
    ?>

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Its very simple, just add the function you've got on the inputs to the textarea:
    HTML Code:
    <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.
    Last edited by Nile; 04-30-2008 at 11:37 AM.
    Jeremy | jfein.net

  3. The Following User Says Thank You to Nile For This Useful Post:

    chechu (04-30-2008)

  4. #3
    Join Date
    Jul 2006
    Location
    Antwerp, Belgium (Europe)
    Posts
    927
    Thanks
    121
    Thanked 2 Times in 2 Posts

    Default

    Code:
    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.

  5. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    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?
    Jeremy | jfein.net

  6. #5
    Join Date
    Jul 2006
    Location
    Antwerp, Belgium (Europe)
    Posts
    927
    Thanks
    121
    Thanked 2 Times in 2 Posts

    Default

    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 !

  7. #6
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    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.

  8. #7
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    Quote Originally Posted by chechu View Post
    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.

  9. #8
    Join Date
    Jul 2006
    Location
    Antwerp, Belgium (Europe)
    Posts
    927
    Thanks
    121
    Thanked 2 Times in 2 Posts

    Default

    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 !

  10. #9
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    PHP Code:
    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..
    Code:
    $_POST[email] != "" || $_POST[message] != ""

  11. #10
    Join Date
    Jul 2006
    Location
    Antwerp, Belgium (Europe)
    Posts
    927
    Thanks
    121
    Thanked 2 Times in 2 Posts

    Default

    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 ?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •