Page 1 of 1

php validation

Posted: Fri Aug 01, 2014 6:08 pm
by chris93

Code: Select all

<?php
ob_start();
?>
<html>
<head>
<title>fillInFormValues: short example</title>
<style>
.error { color: red; }
</style>
</head>
<body>
<h1>Sign up for our newsletters</h1>
<ul class="error"><li>PLACEHOLDER FOR FORM ERRORS</li></ul>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="GET">
<table border="0">
<tr>
 <td><label for="email">Your Email Address:</label></td>
 <td><input type="text" name="email" id="email"></td>
</tr>
<tr>
 <td><label for="newsletter">Sign up for these newsletters:</label></td>
 <td>
 <input type="checkbox" name="news" id="news">
  <label for="news">News</label><br />
 <input type="checkbox" name="security" id="security">
  <label for="security">Security Notices</label><br />
 <input type="checkbox" name="specials" id="specials">
  <label for="specials">Specials</label>
 </td>
</tr>
<tr><td> </td>
 <td><input type="submit" name="submit" value="Sign Up"></td>
</tr>
</table>
</form>
</body>
</html>

<?php
$html = ob_get_contents();
ob_end_clean();

require_once("fillInFormValues.php");
require_once("validateForm.php");

$request = (get_magic_quotes_gpc() ?
             array_map('stripslashes', $_REQUEST), $_REQUEST);

$validationData['email'] = array('isRequired', 'type' => 'email');
if (isset($request['submit'])) {
  $formErrors = validateForm($request, $validationData);
  if (count($formErrors) == 0) {
    // Normally there would be code here to process the form
    // and redirect to a thank you page...
    // ... but for this example, we just always re-display the form.
    $info = "No errors; got these values:".
      nl2br(htmlspecialchars(print_r($request, 1)));
    $html = preg_replace('/<body>/', "<body><p>$info</p>", $html);
  }
}
else {
  $formErrors = array();
}

echo fillInFormValues($html, $request, $formErrors);

?>
when i run this code im getitng


Parse error: syntax error, unexpected ',' in C:\xampp2\htdocs\tutorials\forms\form4.php on line 47

Re: php validation

Posted: Fri Aug 01, 2014 8:27 pm
by Celauran
You've got a comma in place of a colon in your ternary operator. It should be of the format:

Code: Select all

expression ? true : false

Re: php validation

Posted: Mon Aug 04, 2014 8:08 am
by pbs
replace this with 47 line number

Code: Select all

$request = (get_magic_quotes_gpc() ? array_map('stripslashes', $_REQUEST) : $_REQUEST);

Re: php validation

Posted: Mon Aug 04, 2014 8:11 am
by Celauran
Note that as of PHP 5.4, get_magic_quotes_gpc always returns false.

Re: php validation

Posted: Mon Aug 11, 2014 11:12 pm
by KarlRice
The PHP Validation script is a set of validation rules which lets you add server-side validation to your forms quickly and with as little effort as possible.

We’ll go into more detail in the later pages – this page is mainly just to give you a general sense of how it fits together.
1. Download and upload the PHP validation script (validation.php) to your web server and include() or require() that file via PHP in your form page. Note: your form will need to be a .php page.
2. Direct your form to submit its contents to itself using the action=”” attribute. When the form successfully passes your validation rules, you can redirect via PHP to whatever page you want the user to see next. If it fails, you can output the errors directly in the form page for the user to fix.
3. Add your form-specific validation rules to the top of your form page before the opening tag. These validation rules are basically a LIST (or an array for the technically-literate), saying which form fields need to validated, in what way, and what errors should be displayed to the user if they’re not filled in properly.
4. Add some display code into your page which will output any errors that occur.

Re: php validation

Posted: Tue Aug 12, 2014 7:20 am
by Celauran
KarlRice wrote:We’ll go into more detail in the later pages – this page is mainly just to give you a general sense of how it fits together.
O RLY?