cs1h Posted September 23, 2007 Share Posted September 23, 2007 Hi, Does anyone know if it is possible to validate form fields (i.e. make sure certain sections ae filled in) with php? If it is can anyone tell me how or where I can find out how? Thanks Colin Quote Link to comment https://forums.phpfreaks.com/topic/70364-form-validation/ Share on other sites More sharing options...
Orio Posted September 23, 2007 Share Posted September 23, 2007 You need to use the empty() function. Learn from this example: <form action="<?php echo basename($_SERVER['PHP_SELF']); ?>" method="POST"> <input type="text" name="field1"><input type="submit" name="submit"> <br> <?php if(isset($_POST['submit'])) { if(empty($_POST['field'])) echo "<center><font color=red>Fill in the Field!</font></center>"; } ?> Orio. Quote Link to comment https://forums.phpfreaks.com/topic/70364-form-validation/#findComment-353475 Share on other sites More sharing options...
BlueSkyIS Posted September 23, 2007 Share Posted September 23, 2007 when you submit a form, it is sent via either GET or POST. PHP can then get the values using the name for each form element. For instance, if you have a form that POSTs to a PHP file and one of the form elements is a text field with the name test_field, in the PHP file, you can get the value like this: $a_variable = $_POST['test_field']; Then compare the value of $a_variable to the allowed values. If $a_variable != $allowed_value, display an error. It's the same with GET. Quote Link to comment https://forums.phpfreaks.com/topic/70364-form-validation/#findComment-353492 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.