1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Resetting form values after submission

Discussion in 'HTML & Website Design' started by karl_murphy, Jun 6, 2014.

  1. #1
    Hi,

    Is it possible to reset a form's values once they have been reset? The following code allows the values to be reset before the form is submitted, but it does not work once the form is submitted:

    <script>
    function resetValues()
    {
    document.getElementById("search_recipes_form").reset();
    }
    </script>

    <form role = "form" method = "get" id = "search_recipes_form" action = "search-results.php">
    <div class = "form-group">
    <h4>Quick search</h4>
    <input type = "text" class = "form-control" id = "keyword_search" name = "keyword_search" placeholder = "Enter keywords" value = "<?php echo trim($_GET['keyword_search'],"'"); ?>">
    </div>
    <h4>Meal</h4>
    <div class="form-group">
    <div class="checkbox">
    <label><input type = "checkbox" id = "breakfast" name = "breakfast" <?php if($breakfast == 'Y'){echo 'checked';} ?>> Breakfast &nbsp;</label>
    </div>
    <div class="checkbox">
    <label><input type = "checkbox" id = "lunch" name = "lunch" <?php if($lunch == 'Y'){echo 'checked';} ?>> Lunch &nbsp;</label>
    </div>
    <div class="checkbox">
    <label><input type = "checkbox" id = "starter" name = "starter" <?php if($starter == 'Y'){echo 'checked';} ?>> Starter &nbsp;</label>
    </div>
    <div class="checkbox">
    <label><input type = "checkbox" id = "dinner" name = "dinner" <?php if($dinner == 'Y'){echo 'checked';} ?>> Dinner &nbsp;</label>
    </div>
    <div class="checkbox">
    <label><input type = "checkbox" id = "dessert" name = "dessert" <?php if($dessert == 'Y'){echo 'checked';} ?>> Dessert &nbsp;</label>
    </div>
    <div class="checkbox">
    <label><input type = "checkbox" id = "snack" name = "snack" <?php if($snack == 'Y'){echo 'checked';} ?>> Snack &nbsp;</label>
    </div>
    <div class="checkbox">
    <label><input type = "checkbox" id = "drink" name = "drink" <?php if($drink == 'Y'){echo 'checked';} ?>> Drink &nbsp;</label>
    </div>
    </div>

    <div class="form-group">
    <div class = "row">

    <div class = "col-sm-6">
    <button type="submit" class="btn btn-md btn-default">Search recipes</button>

    </div>

    <div class = "col-sm-6">

    <button type = "reset" class="btn btn-md btn-default" onclick="resetValues()">Reset form</button>
    </div>

    </div>
    </div>
    </form>

    Any help would be greatly appreciated.

    Thanks.
     
    karl_murphy, Jun 6, 2014 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #2
    Why are you using javascript to do what button type="reset" does in the first place?

    Or are you trying to "reset" to the values your PHP is plugging in? If so you'll need to save those values to JS on startup, since reset sets them all to blank.

    Your question just really doesn't make any sense... but then neither does your incomplete/oddball form structure... and the telltales of one of those garbage HTML/CSS frameworks with the uselessly vague and completely defeating the point of using CSS presentational classnames like "col-sm-6".
     
    deathshadow, Jun 6, 2014 IP
  3. karl_murphy

    karl_murphy Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    I am able to reset the form values before posting them. That works fines. I need a method of clearing the form once it has been posted and any PHP values are initiated. Does that make sense?

    The form structure is absolutely fine thank you very much.
     
    karl_murphy, Jun 6, 2014 IP
  4. hdewantara

    hdewantara Well-Known Member

    Messages:
    536
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #4
    I assume $dinner, $breakfast, etc have been defined somewhere else, right?
    Then you may use cookies / HTML5 storages to get form default / initial values.
    But... why don't just include them in post?

    PS: I agree with Deathshadow. One is about that resetValues(). It's not needed.
     
    hdewantara, Jun 6, 2014 IP