HOW TO: Clear default form value on form submit

Discussion in 'JavaScript' started by j0563, Oct 18, 2010.

  1. #1
    In my wordpress blog comment form, my default value for the visitor's URL field is "Website (optional)".

    When the textbox is clicked, the value is cleared. However, if the visitor does not enter anything in the textbox, the form passes the default value, being "Website (optional)" as the visitor's URL.

    My problem is that I'm trying to NOT pass this default value if nothing is entered.

    Here's what I've done (NOT WORKING!):

    <form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform" onSubmit="ClearDefaultURL()">
    HTML:
    For the javascript, I have:

    function ClearDefaultURL() {
    	if ($_POST["url"] == "Website (optional)") 
    	  $_POST["my_field"] = "";
    }
    HTML:
    I was thinking this would not pass the default value, but it still is.

    Any suggestions on how to fix this???
     
    j0563, Oct 18, 2010 IP
  2. _:codefan:_

    _:codefan:_ Active Member

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    58
    #2
    What you're doing here? This is php or javascript? You must know the id of the website field. Assuming that the id is 'website' :

    
    
    function ClearDefaultURL() {
       var input = document.getElementById('website');
        if (input.value == "Website (optional)") 
          input.value = "";
       
        return true;
    }
    
    
    Code (markup):
    <form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform" onSubmit="return ClearDefaultURL()">
    HTML:
     
    _:codefan:_, Oct 18, 2010 IP