clear field before database insertion

jo0

macrumors regular
Original poster
Nov 25, 2009
224
0
Seattle, WA
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.
 

Attachments


plinden

macrumors 68040
Apr 8, 2004
3,968
3
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.
 

jo0

macrumors regular
Original poster
Nov 25, 2009
224
0
Seattle, WA
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?
 

Cerebrus' Maw

macrumors 6502
Mar 9, 2008
409
0
Brisbane, Australia
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
 

angelwatt

Moderator emeritus
Aug 16, 2005
7,857
7
USA
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'] = '';
}