Notice: Array to string conversion

Web Form Design and Development :: Lesson 8
<!-- Stylesheets -->
<link rel="stylesheet" href="css/normalize.css" />
<link rel="stylesheet" href="css/styles.css" />

<!--[if lt IE 9]>
	<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->

Web Form Design and Development

Lesson 8: Server-side validation (the classic method)

<div class="demo-lesson-nav">
	<a href="lesson7.html" class="demo-lesson-nav-prev">&larr; Previous Lesson</a>
	<a href="lesson9.html" class="demo-lesson-nav-next">Next Lesson &rarr;</a>
</div> <!-- end demo-lesson-nav -->

<div class="demo-container">
  <form action="scripts/lesson 8/validate.php" method="POST" id="form-new-account" novalidate>
  <fieldset>
    <ol>
      <li>
        <div class="demo-container">
          <fieldset>
            <legend>Create a New Account</legend>
            <p>
              <label for="name">Name:</label>
              <br />
              <input type="text" name="name" id="name" maxlength="25" class="validate-locally" />
              <span class="demo-input-info">E.g. John Smith, must be between 3 and 25 characters, letters and spaces only</span> <span class="demo-errors"></span> </p>
            <p>
              <label for="username">Username:</label>
              <br />
              <input type="text" name="username" id="username" maxlength="15" class="validate-locally" />
              <span class="demo-input-info">E.g. johnsmith, must be between 3 and 15 alphanumeric characters</span> <span class="demo-errors"></span> </p>
            <p>
              <label for="username">Gender:</label>
              <br />
              <select name="gender" id="gender">
                <option value="0">- Select a Value -</option>
                <option value="1">Male</option>
                <option value="2">Female</option>
              </select>
              <span class="demo-errors"></span> </p>
            <p>
              <label for="email">Email Address:</label>
              <br />
              <input type="email" name="email" id="email" class="validate-locally" />
              <span class="demo-input-info">E.g. [email protected]</span> <span class="demo-errors"></span> </p>
            <p>
              <label for="password">Password:</label>
              <br />
              <input type="password" name="password" id="password" class="validate-locally" />
              <span class="demo-input-info">Must be 6 characters minimum, alphanumeric</span> <span class="demo-errors"></span> </p>
            <p>
              <label for="confirm-password">Confirm Password:</label>
              <br />
              <input type="password" name="confirm-password" id="confirm-password" class="validate-locally" />
              <span class="demo-errors"></span> </p>
            <p>            
            <div class="grouphead">Subscribe</div>
            <ol>
              <li>
                <input type="checkbox" name="subscribe[]" value="subscribe" id="subscribeitem" 
					<?php if ((isset($subscribe)) && (in_array("subscribe", $subscribe))) { echo "checked"; } ?> 
					>
                <label for="subscribeitem">Subscribe</label>
              </li>
              <li>
                <input type="checkbox" name="subscribe[]" value="No" id="Noitem"
					<?php if ((isset($subscribe)) && (in_array("No", $subscribe))) { echo "checked"; } ?> 
					>
                <label for="Noitem">No</label>
              </li>
              <li>
                <input type="checkbox" name="subscribe[]" value="Later" id="Lateritem"
					<?php if ((isset($subscribe)) && (in_array("Later", $subscribe))) { echo "checked"; } ?> 
					>
                <label for="Lateritem">Later</label>
              </li>
            </ol>
            <p></p>
            <p>
              <input type="submit" value="Create Account" name="submit" />
            </p>
            <p><small><em>Note: All fields are required!</em></small></p>
          </fieldset>
        </div>
      </li>
      </ol>
  </fieldset>
  </form>
</div><!-- end demo-container -->

<footer class="demo-footer">
	<p><small>a <a href="https://tutsplus.com/">Tuts+</a> course by Adi Purdila</small></p>
</footer>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script src="scripts/lesson 8/validate.js"></script>
*****************************************************************************************************

[php]

<?php ini_set('error_reporting', E_ALL); ini_set('display_errors', '1'); /***********************************************************************************************/ /* If nothing is posted then we exit */ /***********************************************************************************************/ if (!$_POST) { die("This file cannot be accessed directly!"); } /***********************************************************************************************/ /* Define regular expression patterns */ /***********************************************************************************************/ $expEmail = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/'; $expLettersOnly = '/^[a-zA-Z ]+$/'; $expLettersNumbers = '/^[a-zA-Z0-9]*$/'; /***********************************************************************************************/ /* Define the function for checking the field length */ /***********************************************************************************************/ function validateLength($fieldValue, $minLength) { return (strlen(trim($fieldValue)) > $minLength); } /***********************************************************************************************/ /* Get the posted field values and validate each field */ /***********************************************************************************************/ $name = $_POST["name"]; $username = $_POST["username"]; $gender = $_POST["gender"]; $email = $_POST["email"]; $password = $_POST["password"]; $confirmPassword = $_POST["confirm-password"]; $subscribe = $_POST["subscribe"]; $errorExists = false; $errors = "Errors:
    "; // Name if (!validateLength($name, 2)) { $errorExists = true; $errors .= "
  • The name is too short!
  • "; } if (preg_match($expLettersOnly, $name) !== 1) { $errorExists = true; $errors .= "
  • The name can only contain letters and spaces!
  • "; } // Username if (!validateLength($username, 2)) { $errorExists = true; $errors .= "
  • The username is too short!
  • "; } if (preg_match($expLettersNumbers, $username) !== 1) { $errorExists = true; $errors .= "
  • The username can only contain alphanumeric characters!
  • "; } // Gender if ($gender === "1") { $gender = "Male"; } else if ($gender === "2") { $gender = "Female"; } else { $errorExists = true; $errors .= "
  • Please select a gender!
  • "; } // Email if (preg_match($expEmail, $email) !== 1) { $errorExists = true; $errors .= "
  • The email address format is invalid!
  • "; } // Password if (!validateLength($password, 5)) { $errorExists = true; $errors .= "
  • The password is too short!
  • "; } if (preg_match($expLettersNumbers, $password) !== 1) { $errorExists = true; $errors .= "
  • The password can only contain alphanumeric characters!
  • "; } // Confirm Password if ($confirmPassword !== $password) { $errorExists = true; $errors .= "
  • The passwords don't match!
  • "; } // dont subscribe // If no errors, echo the results if (!$errorExists) { echo "

    Success! The form has been submitted!

    " . "

    Details:

    " . "
      " . "
    • Name: $name
    • " . "
    • Usernname: $username
    • " . "
    • Gender: $gender
    • " . "
    • Email: $email
    • " . "
    • Subscribe to newsletter: $subscribe
    • " . "
    "; } else { echo "

    Error! Please address the following issues:

    " . $errors; } ?>

    [/php]

    tryed googling this and cannot understand all i can read from it is it is saying the array to string conversion has not taken place am i wrong

    web page shows

    Notice: Array to string conversion in C:\xampp\htdocs\webs\06-08\scripts\lesson 8\validate.php on line 114

    Success! The form has been submitted!
    Details:

    Name: antony bromley
    Usernname: jp
    Gender: Male
    Email: [email protected]
    Subscribe to newsletter: Array
    i guess it is on about writing the word array in the subscribe line , b ut if i can not understand what chance have i

    any help would be appreciated thanks

<input type=“checkbox” name=“subscribe[]” <<---- you see these square brackets, they create an array.
I don’t know why you are doing that only the subscribe set and not the rest??

You don’t deal with this array in your php script, you simply post it:
[php]$subscribe = $_POST[“subscribe”];[/php]

What you need to do is loop through the array
[php]foreach($subscribe as $key => $value) {
echo $value;
}[/php]
or call an array value directly.
[php]echo $subscribe[0];
echo $subscribe[1];[/php]
or change it to a string:
[php]$subscribe = implode(’ ', $subscribe);[/php]

Hope that helps,
Red :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service