dreinuqm992401 2015-11-12 20:02
浏览 28

PHP表单提交,验证变量

I am hung up on an issue. I've got an html form, nothing fancy. On submission I need to verify the values submitted and if they fail I need to go back to the form and point out the error. I've done this many times before but with simple data verification like blank input fields or email address formatting. There is a lot of validation here and when I return to my form all the previous input it lost.

Here's what I have so far (some things may be currently missing as I am constantly tweaking it but you'll get the idea)

index.php

    <div id="tabs-2">
    <center><h1>Add Event Form:</h2></center>
    <form name="htmlform" method="POST" action="htmlform.php">
        <table>
            <tr>
                <td class="left"><label for="first_name">Event Name *</label></td>
                <td><span><input type="text" name="event_name" value="<?PHP if(isset($_POST['event_name'])) echo htmlspecialchars($_POST['event_name']); ?>" required /></span></td>
            </tr>
            <tr>
                <td class="left"><label for="first_name">Event Date *</label></td>
                <td><span><input id="datepicker" type="text" name="event_date" value="<?PHP if(isset($_POST['event_date'])) echo htmlspecialchars($_POST['event_date']); ?>" required /></span></td>
            </tr>
            <tr>
                <td class="left"><label for="first_name">Start Time *</label></td>
                <td>
                    <span>
                        <select name="start_time" value="<?PHP if(isset($_POST['start_time'])) echo ($_POST['start_time']); ?>" required />
                        <?php
                            foreach($times as $time){
                                echo '<option value="' . $time . '">' . $time . '</option>';
                            }
                        ?>
                    </span>
                </td>
            </tr>
            <tr>
                <td class="left"><label for="first_name" >End Time *</label></td>
                <td>              
                    <span>
                        <select name="end_time" value="<?PHP if(isset($_POST['end_time'])) echo ($_POST['end_time']); ?>" required />
                        <?php
                            foreach($times as $time){
                                echo '<option value="' . $time . '">' . $time . '</option>';
                            }
                        ?>
                    </span>
                </td>
            </tr>
            <tr>
                <td class="left"><label for="first_name">Venue Name *</label></td>
                <td><span><input type="text" name="venue_name" value="<?PHP if(isset($_POST['venue_name'])) echo htmlspecialchars($_POST['venue_name']); ?>" required /></span></td>
            </tr>
            <tr>
                <td class="left"><label for="first_name">Venue Address *</label></td>
                <td><span><input type="text" name="venue_address" value="<?PHP if(isset($_POST['venue_address'])) echo htmlspecialchars($_POST['venue_address']); ?>" required /></span></td>
            </tr>
            <tr>
                <td class="left"><label for="first_name">Description</label></td>
                <td><span><textarea name="description" style="height: 150px; font-size: 13px;" value="<?PHP if(isset($_POST['description'])) echo htmlspecialchars($_POST['description']); ?>" required ></textarea></span></td>
            </tr>
            <tr>
                <td class="left"><label for="first_name">Event URL *</label></td>
                <td><span><input type="text" name="event_url" value="<?PHP if(isset($_POST['event_url'])) echo htmlspecialchars($_POST['event_url']); ?>" required /></span></td>
            </tr>
            <tr>
                <td class="left"><label for="first_name">Event Image URL *</label></td>
                <td><span><input type="text" name="event_image" value="<?PHP if(isset($_POST['event_image'])) echo htmlspecialchars($_POST['event_image']); ?>" required /></span></td>
            </tr>
        </table>
        <p><input class="eventSub" type="submit" name="form_submit" value="Submit"></p>
        <br/>
    </form>
</div>

and htmlform.php

<?php

    $event_name = $_POST['event_name'];
    $event_date = $_POST['event_date'];
    $start_time =  $_POST['start_time'];
    $end_time =  $_POST['end_time'];
    $venue_name = $_POST['venue_name'];
    $venue_address = $_POST['venue_address'];
    $description = $_POST['description'];
    $event_url = filter_var($_POST['event_url'], FILTER_SANITIZE_URL);
    $event_image = filter_var($_POST['event_image'], FILTER_SANITIZE_URL);

    $start = strtotime($start_time);
    $end = strtotime($end_time);

    if (filter_var($event_url, FILTER_VALIDATE_URL) === false) {
        //die(header("location:index.php?error=true&reason=url#tabs-2"));
    }

    if (filter_var($event_image, FILTER_VALIDATE_URL) === false) {
        //die(header("location:index.php?error=true&reason=url#tabs-2"));
    }

    if ($end < $start){
        $next_day_format = strtotime("+1 day", strtotime($event_date));
        $next_day = date("Y-m-d", $next_day_format);
        $json_start_time = $event_date . "T" . $start_time;
        $json_end_time = $next_day . "T" . $end_time;

    }
    else {
        $json_start_time = $event_date . "T" . $start_time;
        $json_end_time = $event_date . "T" . $end_time;
    }

    $cityclean = str_replace (" ", "+", $venue_address);
    $fullurl = "http://maps.googleapis.com/maps/api/geocode/json?address=" . $cityclean . "&sensor=true";
    $string = file_get_contents($fullurl); 
    $params = json_decode($string, true);

    $lon = $params['results'][0]['geometry']['location']['lng'];
    $address = substr($params['results'][0]['formatted_address'],0,-5);
    $lat = $params['results'][0]['geometry']['location']['lat'];
    $address_array = explode(',',$address);
    $state_zip = trim($address_array[2]);
    $sz_array = explode(' ',$state_zip);
    $state = $sz_array[0];
    $city = trim($address_array[1]);
    $zip = $sz_array[1];
?>

It's not quite finished yet. The ideal situation will send the submit to a json formated string once all fields are validated.

The question is if anything fails validation I would like to reload the previous page which the form was submitted but retain all entered values, as of now when it errors out and reloads the page, the form is blank.

  • 写回答

3条回答 默认 最新

  • duanou2016 2015-11-12 20:08
    关注

    It looks like the problem is that the form action is htmlform.php, so when you submit the form you are posting to that PHP file. This means that your PHP in index.php that tries to echo these variables (e.g. echo htmlspecialchars($_POST['event_name'] ), simply doesn't have access to them.

    You have a couple of options

    • You could move the form handing logic out of htmlform.php into index.php. Then you have access to the $_POST array for repopulating the form fields as well as showing an error.
    • You could use AJAX to post the values from your form fields in index.php to htmlform.php, and then parse the reponse (probably using JSON) to add error messages by your form fields whilst still retaining the user's input. There are lots of useful tutorials on the web to help you do this.
    评论

报告相同问题?