I only use 3 fields here for simplicity, but it should apply to a larger form. I am trying to check if the form is submitted, validate input, display error message if input is missing or invalid and keep in the field what ever user has entered. I am using two arrays with same keys, so I can detect input and set correlating error for the same key.
My code is:
<!DOCTYPE html>
<html>
<head><meta content="text/html; charset=utf-8" http-equiv="Content-Type" /><title>Test2</title></head>
<body>
<?php
$input = array("name"=>"", "phone"=>"", "email"=>"");
$error = array("name"=>"", "phone"=>"", "email"=>"");
if ($_SERVER["REQUEST_METHOD"] == "POST") {
foreach ($input as $key => &$value) {
$value = test_input($_POST[$key]);
$pregMatch = "\W";
if ($key == "phone") {
$pregMatch = "/^[0-9() ]*$/";
}
elseif ($key == "email") {
$pregMatch = "/([.\-]+\@[.\-]+\.[.\-]+)/";
}
if (in_array($key, array("name", "phone", "email"))) {
if (empty($_POST[$key])) {
$error($key) = $key . " is required"; /* LINE 27 */
}
} /* end if in_array */
if (!preg_match($pregMatch, $value)) {
$error($key) = "Invalid " . $key; /* LINE 32 */
}
} /* end foreach */
} /* end check if form is submitted */
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form name="form1" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]) ;?>">
<label for="name">Name</label>
<input type="text" name="name" id="name" value="<?php echo $input('name'); ?>"> /* LINE 49 */
<span><?php echo $error('name'); ?></span> /* LINE 50 */
<label for="phone"><span class="red">*</span>Phone</label>
<input type="text" name="phone" id="phone" value="<?php echo $input('phone'); ?>">
<span> <?php echo $error("phone"); ?></span>
<label class="label" for="email"><span class="red">*</span>Email</label>
<input type="text" name="email" id="email" value="<?php echo $input('email'); ?>">
<span><?php echo $error("email"); ?></span>
<input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>
There are 3 problems arising:
1 - Fatal error: Can't use function return value in write context in ...public_html/test2.php on line 27. Same for line 32 in 27 is removed.
2 - If I comment out lines 27 and 32, the code will stop at line 49. By this I mean that only text "Name" will appear. Nothing after. No text field called 'name', no phone or email text and fields.
3 - If I comment out php code in line 49, I get: Fatal error: Function name must be a string in ...public_html/test2.php on line 50. This is caused by the php code on line 50.
NOTE: I've marked the lines in the code.
I'm new to php and I would appreciate any help you can provide.
$error[$key] = $key
instead of$error($key) = $key
? And 49 ->$input['name']
, 50 ->$error['name']
...()
->$array = array(1,2,3);
, but when you access an array value you use[]
->$array[0]
/$array[1]
. As of 5.4 you can also use$array = [1,2,3]
, but you still use$array[0]
/$array[1]
to access the value. php.net/manual/en/language.types.array.php