1

I'm fairly new to php and was wondering how do I validate the array for the states and where do I place the php code at? I already did the address part but I'm stuck at the states part.

Here is the validation code.

if (isset($_POST['contact_info_submitted'])) { // Handle the form.

    $address = mysqli_real_escape_string(htmlentities($_POST['address']));

}

Here is the form code.

<li><label for="address-1">Address 1: </label><input type="text" name="address-1" id="address-1" size="25" class="input-size" value="<?php if (isset($_POST['address'])) echo $_POST['address']; ?>" /></li>

<li><label for="state-province">State/Province: </label>
<?php
echo '<select name="state-province" id="state-province">' . "\n";
    foreach($state_options as $option) {
        if ($option == $state) {
            echo '<option value="' . $option . '" selected="selected">' . $option . '</option>' . "\n";
            } else {
              echo '<option value="'. $option . '">' . $option . '</option>'."\n";
            }
        }
            echo '</select>';

?>
</li>
<li><input type="submit" name="submit" value="Save Changes" class="save-button" />
    <input type="hidden" name="contact_info_submitted" value="true" />
5
  • Can you also post the structure of your form so the answers we provide can be identical to your code? Commented Mar 18, 2010 at 18:42
  • @Anthony Forloney what exactly do you mean? sorry for begin ignorant. Commented Mar 18, 2010 at 18:44
  • Agree with Anthony, we need to see more of your page code. As it is now your validation code doesn't match the field names in your form, so nothing will be validated... Commented Mar 18, 2010 at 18:45
  • @SIAcKer: I'm having a difficult time understanding your question. Please edit your question and explain yourself in a bit more detail. Specifically what do you mean by "how do I validate the array for the states"? Are you trying to check that they selected a state? Commented Mar 18, 2010 at 18:48
  • @Josh what I was wondering do I have to check to see if the states need to be in code like this $address = mysqli_real_escape_string(htmlentities($_POST['address'])); Commented Mar 18, 2010 at 18:50

1 Answer 1

1

If you're wondering how to check if someone submitted a valid value for state just check to see if there value they posted is in your array of states. If it's not there it's not valid:

if (!in_array($_POST['state-province'], $state_options)) {
    // not valid
}

If you're just trying to store in the a database like the other form values it's no different then any text field:

if (isset($_POST['contact_info_submitted'])) { // Handle the form.
    $state= mysqli_real_escape_string($_POST['state-province']);
}
Sign up to request clarification or add additional context in comments.

Comments

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.