Folks,
For the time being, I removed this “required” from the input fields on the html form as I want to check via php if all fields got filled-in or not. Doing this for my learning purpose ofcourse on how php checks for user inputs.
Is this code ok or not ?
““
<?php
?>
<form method=”POST” name=”textfield” id=”textfield” action=””>
<fieldset>
<label for=”email”>Email:</label><br>
<input type=”email” name=”email” id=”email_address” maxlength=”255″ size=”20″>
<br>
<label for=”domain”>Domain:</label><br>
<input type=”text” name=”domain” id=”website_domain” maxlength=”255″ size=”25″>
<br>
<label for=”text”>Url:</label><br>
<input type=”text” name=”url” id=”website_address” maxlength=”255″ size=”50″>
<br>
<label for=”text”>Text:</label><br>
<input type=”text” name=”text” id=”test” value=”” maxlength=”255″ size=”60″>
<br>
<label for=”description”>Description:</label><br>
<textarea name=”description” id=”website_description” cols=”50″ rows=”10″></textarea>
<br>
<label for=”table”><b>Database:</b></label><br>
<select name=”database” id=”db” multiple>
<option value=””></option>
<option value=”searchengine”>Searchengine</option>
<option value=”classifieds”>Classifieds</option>
</select>
<br>
</fieldset>
<fieldset>
<b>Terms & Conditions:</b>
<input type=”radio” name=”tos_agree” id=”yes” value=”Agree to TOS: Yes:”><label for=”yes”>Agree to TOS: Yes:</label>
<input type=”radio” name=”tos_agree” id=”no” value=”Agree to TOS: No:”><label for=”no”>Agree to TOS: No:</label>
<br>
<b>Newsletter:</b>
<label for=”newsletter_subcribe:yes”>Newsletter Subcribe:Yes</label>
<input type=”checkbox” name=”newsletter_subcribe:yes” id=”newsletter_subcribe:yes” value=”Newsletter Subcribe:Yes”>
</fieldset>
<fieldset>
<input type=”reset”><br>
<button type=”reset”>Reset 2!</button>
<br>
<input type=”submit”><br>
<button type=”submit”>Submit 2!</button><br>
</fieldset>
</form>
<?php
if($_SERVER[‘REQUEST_METHOD’]==’POST’)
{
$errors = array();
$labels = array(“email”=>”required”,”domain”=>”required”,”url”=>”required”,”text”=>”required”,”description”=>”required”,”database”=>”required”,”tos”=>”required”);
foreach($labels as $key=>$value)
{
if($value==”required”)
{
if(empty($_POST[“$key”]))
{
$errors[] = “<b>$key</b> ” .’is required!’;
}
else
{
echo ‘<b>Line ‘ .__LINE__ .’: </b>’; echo ‘ ‘ .$_POST[$key]; echo ‘<br>’;
}
}
}
if(!empty($errors))
{
foreach($errors as $error)
{
echo ‘<b>Line ‘ .__LINE__ .’: </b>’; echo ‘Error Alert: ‘ .$error; echo ‘<br>’;
}
}
if(!filter_input(INPUT_POST,’email’,FILTER_VALIDATE_EMAIL))
{
echo ’email wrong’;
}
if(!filter_input(INPUT_POST,’domain’,FILTER_VALIDATE_DOMAIN))
{
echo ‘domain wrong’;
}
if(!filter_input(INPUT_POST,’url’,FILTER_VALIDATE_URL))
{
echo ‘url wrong’;
}
}
//TEST RESULT:
//$_POST[”] outputs the value of value=””.
//For php to extract user input, the radio html needs to have the value=””. Php cannot extract without it.
?>
Anyway, can this code can be shortened as much as possible your way or not ? If yes then let’s see your way!
Thanks!