开发者

Validate Input if Submitted (ie, "not disabled")

I have a contact form that is being submitted, I have PHP validation all set up before it sends the email, however, I have 2 fields that are dynamically disabled or enabled based on the link clicked to get to the contact form.

I need to know how to determine whether they are disabled or not and then validate them only if they're not disabled.

Here's part of my form:

<label for="name">Name</label><br />
<input type="text" name="name" id="name" />

<label for="email">Email</label><br />
<input type="text" name="email" id="email" />

<label for="number">Phone Number</label><br />
<input type="text" name="number" id="number" />

<div id="date-time">
    <label for="date">Date</label><br />
    <input type="text" name="date" id="date" <?php
        if (!isset($_GET["appt"])==true) echo('disabled="disabled"'); ?> />

    <label for="time">Time</label><br />
    <input type="text" name="time" id="time" <?php
        if (!isset($_GET["appt"])==true) echo('disabled="disabled"'); ?> />
</div>

Here's part of my mail.php file (disregard the cookie functions):

    $name   = trim($_POST["name"]);
    $email  = trim($_POST["email"]);
    $phone  = trim($_POST["phone"]);
    $date   = trim($_POST["date"]);
    $time   = trim($_POST["time"]);

//Check to make sure that the name field is not empty
    if($name == NULL) {
        $hasError = true;
        $emailSent = false;
        cookie("hasError", "true");
        cookie("nameError", "true");
    } else {
        $name = $name;
    }

//Check to make sure sure that a valid email address is submitted
    if($email == NULL) {
        $hasError = true;
        $emailSent = false;
        cookie("hasError", "true");
        cookie("emailError", "true");
    } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", $email)) {
        $hasError = true;
        $emailSent = false;
        cookie("hasError", "true");
        cookie("emailError", "true");
    } else {
        $email = $email;
    }

//Check to make sure that the phone field is not empty
    if($phone == '' || $phone < 10) {
        $hasError = true;
        $emailSent = false;
        cookie("hasError", "true");
        cookie("phoneError", "true");
    } else {
        $phone = $phone;
    }

//Check to make sure that the date field is not empty
    if($date == '') {
        $hasError = true;
        $emailSent = false;
        cookie("hasError", "true");
        cookie("dateError", "true");
    } else {
        $date = $date;
    }

//Check to make sure that the time field is not empty
    if($time == '') {
        $hasError = true;
        $emailSent = false;
        cookie("hasError", "true");
        cookie("timeError", "true");
    } else {
        $time = $time;
    }

I'm still learning my way around PHP so thanks in advance for any help you can give. :)

//EDIT

appt is from a URL query string. Even if the query string is not present & the inputs are disabled, I'm using jQuery to add or remove disabled fr开发者_StackOverflowom the inputs upon clicking a link on the page.


You can use isset($_POST['date']) to check if the value is set during the submit, cos, if the fields that are disabled will not be submitted in the form right.

EDIT

You might face a problem here, as it is very easy to tamper with the form in client side and pass both date and time here. So the best thing you can do here is have a hidden field which you set with either date or time along with setting the "dynamic" fields disabled. So, in the server side, just read the hidden field and read the required value accordingly.


I could be mistaken, but shouldn't the indexes simply not exist if the inputs are disabled? Meaning if !isset($_POST[<index>]) don't validate.

Oh, BTW !isset($_GET["appt"])==TRUE is redundant. Just use !isset($_GET["appt"])


On the form page, put $_GET["appt"] in a hidden field. Since it is what you used to determine whether or not the fields should be disabled, passing it along to the PHP validating page can let you determine whether or not you disabled the fields on the form page.

On the form page: " />

And on the mail.php page:

$disabled = trim($_POST["disabled_var"]);

If true, the elements were disabled.

Also, no, you can't just see whether or not they exist in the post, because they could very much be empty (but enabled).


You can add a hidden field for each field, and set them to values that you can extract. Probably not the most elegant solution, but it still works.

<input type="hidden" name="datedisabled" id="datedisabled" value="<?php
        if (!isset($_GET["appt"])) { echo 'true'; } else { echo 'false'; } ?>" />
<input type="hidden" name="timedisabled" id="timedisabled" value="<?php
        if (!isset($_GET["appt"])) { echo 'true'; } else { echo 'false'; } ?>" />

Then, you can check with PHP.

By the way, doing explicit checks like if ($a == true) are redundant. Just do if ($a).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜