www.webdeveloper.com
Results 1 to 8 of 8

Thread: Form validation without deleting the fields?

  1. #1
    Join Date
    Jul 2012
    Posts
    11

    Form validation without deleting the fields?

    Hi to everyone

    I have implemented a html form with some fields like email and phone and I want to validate the data before submitting the form. I want to validate the data in the server side (because javascript can be easily avoided) and so I have write some PHP functions. Currently the form is in a Form.html file and the validation script is in a Validate.php file.

    If I the validation of the data return false how can I return to my form WITHOUT the fields to be erased?

    Thanks

  2. #2
    Join Date
    May 2011
    Location
    With Hobbits
    Posts
    125
    Considering its PHP and server side you are going to have to validate after a submission no matter what (unless you want to use JS)

    If you want to keep the values than you just store them in a seperate variable and echo that out in the html, (Requires the form to be a php file not an html file)

    here is an example of me echoing a variable to an html field (not tested), it should basically just convert numbers to letters and echo them to the same field after submission.

    Form.php
    PHP Code:
    <?php

    $val 
    "123-456-7890";

    if(isset(
    $_POST['submit'])){
        
    $num = array('1''2''3''4''5''6''7''8''9''0');
        
    $let = array('a''b''c''d''e''f''g''h''i''j');
        
    $val str_replace($num$let$_POST['phone-number']);
    }
    ?>
    <html>
        <body>
            <form method="post">
                <input type="text" name="phone-number" id="phone-number" value="<?php echo $val?>">
                <input type="submit" name="submit">
            </form>
        </body>
    </html>
    Thats my archaic way of doing it, although im sure the experts have a super sleek quick way of doing it rather than an ugly php tag in the middle of your php.
    Albert Einstein ~ "Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction."

    Things To help you on this Site

    1. Place all php code in [php] Tags
    2. Place all html code in [html] Tags
    3. Remember to mark all your Threads Resolved using thead tools at the top when your problem is solved

  3. #3
    Join Date
    Sep 2011
    Location
    Bristol, England, United Kingdom
    Posts
    192
    Not really archaic; you have to force the submitted values to become the values of the elements somehow. You could always use AJAX and have JavaScript use the .value to alter the element, but that's just more code for equal work.

  4. #4
    Join Date
    Jul 2012
    Posts
    11
    Thanks for your replies. Really useful.

    One last thing. In my form I have some <select> elements with many many options. In order to make the <select> fields to have the values before submission I have to put a small code snippet like this below in each option?

    PHP Code:
    <option value='option_value'<?php echo (isset($_POST['selectname']) && $_POST['selectname'] == 'option_value')?  "selected='selected'" '' ?>> option_text </option>
    Thanks

  5. #5
    Join Date
    May 2011
    Location
    With Hobbits
    Posts
    125
    Quote Originally Posted by NikosDim View Post
    Thanks for your replies. Really useful.

    One last thing. In my form I have some <select> elements with many many options. In order to make the <select> fields to have the values before submission I have to put a small code snippet like this below in each option?

    PHP Code:
    <option value='option_value'<?php echo (isset($_POST['selectname']) && $_POST['selectname'] == 'option_value')?  "selected='selected'" '' ?>> option_text </option>
    Thanks
    Make it into a php function
    PHP Code:
    <?php
    function FindSelected($selectname){
        
    $return = ($selectname == 'option_value') ? "selected='selected'" '';
        return 
    $return;
    }
    ?>

    <select>
        <option value='option_value'<?php FindSelected($_POST['selectname1']); ?>> option_text </option>
        <option value='option_value'<?php FindSelected($_POST['selectname2']); ?>> option_text </option>
    </select>
    Not sure if that would actually work considering your using $_POST's
    Albert Einstein ~ "Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction."

    Things To help you on this Site

    1. Place all php code in [php] Tags
    2. Place all html code in [html] Tags
    3. Remember to mark all your Threads Resolved using thead tools at the top when your problem is solved

  6. #6
    Join Date
    Jul 2012
    Posts
    11
    Quote Originally Posted by Nvenom View Post
    Make it into a php function
    PHP Code:
    <?php
    function FindSelected($selectname){
        
    $return = ($selectname == 'option_value') ? "selected='selected'" '';
        return 
    $return;
    }
    ?>

    <select>
        <option value='option_value'<?php FindSelected($_POST['selectname1']); ?>> option_text </option>
        <option value='option_value'<?php FindSelected($_POST['selectname2']); ?>> option_text </option>
    </select>
    Not sure if that would actually work considering your using $_POST's
    I got your point, but I think that the function should have two parameters, one for the value that was selected before submission and one for the value of each option that we run the function, right?

  7. #7
    Join Date
    May 2011
    Location
    With Hobbits
    Posts
    125
    are you worried about the function being ran with a blank variable?
    if it was ran empty i believe the function would still return properly.

    there are a million ways to tweak it though, i suppose you could do,

    PHP Code:
    <?php 
    function FindSelected($selectname){ 
        
    $return = ($selectname == 'option_value') ? "selected='selected'" ''
        return 
    $return

    ?> 

    <select> 
        <option value='option_value'<?php if(isset($_POST['submit'])){FindSelected($_POST['selectname1'])}; ?>> option_text </option> 
        <option value='option_value'<?php if(isset($_POST['submit'])){FindSelected($_POST['selectname1'])} ?>> option_text </option> 
    </select>
    Trial and error
    Albert Einstein ~ "Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction."

    Things To help you on this Site

    1. Place all php code in [php] Tags
    2. Place all html code in [html] Tags
    3. Remember to mark all your Threads Resolved using thead tools at the top when your problem is solved

  8. #8
    Join Date
    Jul 2012
    Posts
    11
    Quote Originally Posted by Nvenom View Post
    are you worried about the function being ran with a blank variable?
    if it was ran empty i believe the function would still return properly.
    I didn't get what you mean in the above sentences but I managed to do this. Thanks for your replies anyway. They were very useful.

    By the way here is what I did. Maybe someone else can be benefited from this.

    PHP Code:
    function FindSelected($submitted$current) {
                    
    $return = ($selected == $current) ? "selected='selected'" '';
                    return 
    $return;
                }

    echo 
    '<select name="SelectName" id="SelectName">';

    $optionValues = array('The''values''of''the''option''tags');

                                    foreach (
    $optionValues as $c) {
                                        echo 
    "<option ";
                                        if (
    array_key_exists('submit'$_POST)) {
                                            echo 
    FindSelected($_POST['SelectName'], $c);
                                        }
                                        echo 
    "value='$c'>$c</option>";

     echo 
    '</select>'

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles