clear field before database insertion

Discussion in 'Web Design and Development' started by jo0, Apr 5, 2010.

  1. jo0 macrumors regular

    Joined:
    Nov 25, 2009
    Location:
    Seattle, WA
    #1
    i have a user entry form on an html file gets sent to a php file where it is entered into a database.

    in the picture you can see the example of the form and then db upload.

    i want to make it so if a certain value, for example "name" was entered, it would clear it out and return "" to the db. all other values of course would be accepted as they were.

    thanks for your input and suggestions.
     

    Attached Files:

  2. plinden macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #2
    Are you talking about clearing placeholder text - something like this http://lab.dotjay.co.uk/experiments/forms/input-placeholder-text/ ?

    I wouldn't just clear the input if it's a certain value. What if that particular value is what the user means to store?

    HTML 5 browser input elements have a placeholder attribute. Doesn't help with other browsers though (hint: IE), so you still need a scripting solution.
     
  3. jo0 thread starter macrumors regular

    Joined:
    Nov 25, 2009
    Location:
    Seattle, WA
    #3
    the text for name and credit entry is prefilled but goes away when the user types something. if a person doesnt want to type a name in the text that is prefilled gets passed to the database. i just need to clean that so if the prefilled text gets submitted its cleared to a blank entry before it goes to the db as the username.

    does that help?
     
  4. Cerebrus' Maw macrumors 6502

    Joined:
    Mar 9, 2008
    Location:
    Brisbane, Australia
    #4
    You can use the javascript onSubmit function to do your work here.

    Attach the handler to the form, and have it check the value of your field when the html is submitted. Very quick and dirty -

    Code:
    <form method="post" action="ex.php" onSubmit="check_value();">
      <input type="text" name="sample" id="sample" value="Enter Your Value" />
    </form>
    
    <!--Javascript-->
    <script>
    function check_value() {
      var tmp = document.getElementById('sample');
      if (tmp.value=="Enter Your Value") {
         tmp.value="";
      }
    
    }
    </script>
    
    I think that should work, but I'm tired, sore and lazy, so no promises...
    :)

    - Maw
     
  5. jo0 thread starter macrumors regular

    Joined:
    Nov 25, 2009
    Location:
    Seattle, WA
    #5
    that did the trick. thanks, i was way over thinking that one :/
     
  6. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #6
    I wouldn't rely on JavaScript. When dealing with submitting forms you always need to do the validation on the server-side as JavaScript is bypassed easily. It's easy to check for a default name entry.

    PHP:
    if ($_POST['name'] == 'name') {
      
    $_POST['name'] = '';
    }
     
  7. jo0 thread starter macrumors regular

    Joined:
    Nov 25, 2009
    Location:
    Seattle, WA

Share This Page