2

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.

4
  • Shouldn't line 27/32 be $error[$key] = $key instead of $error($key) = $key? And 49 -> $input['name'], 50 -> $error['name']... Commented Jul 24, 2014 at 4:44
  • @Sean: Thanks for a quick reply Sean. I thought that in php arrays are constructed with () instead of [] and that [] is new to php5. I'll try to change it and see what happens. Commented Jul 24, 2014 at 4:47
  • Building the array you use () -> $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 Commented Jul 24, 2014 at 4:51
  • That worked a treat. Isn't it great when a solution turns out simple. Thanks Sean. Commented Jul 24, 2014 at 4:55

2 Answers 2

0

To access array keys/values you use square brackets. Regular brackets are for functions:

$error[$key] = $key . " is required";   /* LINE 27 */

Same for line 32. And further down, same issue:

<input type="text" name="name" id="name" value="<?php echo $input['name']; ?>"> /* LINE 49 */
<span><?php echo $error['name']; ?></span> /* LINE 50 */
0

You are using square brackets as $error($key)which must be changed to $error[$key]

The second problem is on the html section.

you are using like <span><?php echo $error('name'); ?></span> /* LINE 50 */

which must be changed to <span><?php echo $error['name']; ?></span> /* LINE 50 */

You are repeating the same error on line 53 and line 56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.