views:

61

answers:

4

(Q1)Hi I'm using textbox in my project and I can't receive the values that are typed

<textarea rows="5" cols="60"> Type your suggestion </textarea>
<br>
<input type="submit" name="sugestao" value="Submit" />

Sorry I don't know how to 'kill' html code, that's why < is missing.

All I'm getting in the column of the database from this text box is "Submit", I'd like to receive whatever is written in the text area. How can I make the value equal whaterever is typed?

(Q2) How can I make sure that I'll only store the same type(int,varchar,text) that I setted,declared in the database. For example: age(int), but if someone types "abc" in the input it will be stored in my database as the value 0 . How can I forbid this, and only save the age when it's just int and all the other fields(like name, email) are filled ?. And if is still possible warn the user that he is typing something wrong, don't need to say where.

Sorry for any mistake in English and Thanks for the attention.

+3  A: 

You need a name attribute for your textarea, otherwise, when you submit the form, the text you have entered is not sent.

<form method="post" action="your_handler.php">
<textarea rows="5" cols="60" name="question">Type your suggestion</textarea>
<input type="submit" name="sugestao" value="Submit" /> 
</form>

For your second question, you can implement input validation on client side (with javascript), server side (with whatever language you are using), or both.

For age validation as an example you want to validate two things:

  1. You have a number
  2. It's equal to, or greater than say 16

With PHP you check this way:

if( (int) $_POST['age'] > 15) {
   // insert the record
} else {
   // display error message
}
Majid
+1  A: 

I think that you are missing the name="" attribute in your textarea. If you add it, you will receive its value in the $_POST array. For eaxmple, name="abc" will result in $_POST['abc']

For the second question, you need to do form validation. Look for this in Google, it is a basic task.

Palantir
A: 

q2. you can't do it automatically but it can be done manually.
Just add a validation code in the form handler script.

if ($_POST['age'] < 13 or $_POST['age'] > 110) $err[] = "Wrong age value!";
Col. Shrapnel
+1  A: 

If you want to check for data types before saving, you can use PHP's built in methods to do so. There are lots of methods for this with php - is_numeric(), is_string(), is_float() and so forth. When the data is passed to the server, the POST data will initially be in string form, so you may have to cast it ($castVar = (int)$var) before you run any of these methods.

jomanlk