0

Hi Daniwebbers,

I've created a web form and code in order to reset/clear the form with a button. However, in a specific circumstance, the form only resets properly if I go to a separate page then come back to the form and reset it. It only happens directly after I have passed a variable from another page to the form. I will try to lay this out.

Here is the reset code from my form processing page:

if(isset($_POST['reset'])) {

session_start(); 
unset($_SESSION['cf_returndata'], $_SESSION['report']);
header('location: ' . $_SERVER['HTTP_REFERER']);

}

The variable in question is "$_SESSION['report']."
Here is the code from my form page:

session_start();

//init variables  
$cf = array();  
$sr = false;
$st = false;

$ur = false;

if(isset($_SESSION['cf_returndata'])){  
    $cf = $_SESSION['cf_returndata'];  
    $sr = true;
    $st = true;
}

//Check if URL was passed
if(isset($_GET['report'])) {
    $_SESSION['report'] = $_GET['report'];
}

if(isset($_SESSION['report'])) {
    $report = $_SESSION['report'];
    $ur = true;
    $st = false;
}

if(isset($cf['errors']) && count($cf['errors']) == 0) {
    unset($_SESSION['cf_returndata'], $_SESSION['report']);
}

And the code from the field itelf which as you can see is two ternary operators in conjunction:

value="<?php echo ($ur && isset($_SESSION['report'])) ? $report : ''; echo ($st && !$cf['form_ok']) ? $cf['posted_form_data']['url'] : ''; ?>"

Any suggestions I can get on this is much appreciated as it is the last minor hurdle before I publish the site.

Edited by eternalhour: Better clarification

3
Contributors
5
Replies
46
Views
4 Years
Discussion Span
Last Post by eternalhour
0

what's wrong with a html reset button?

<input type="reset" value="Reset" />
0

A reset button doesn't work because I'm holding onto the field data in session variables, nothing happens. I should also clarify that the only field that is not resetting is the field I have mentioned. All others reset properly.

Edited by eternalhour

0

how about try to insert some javascript into your into your html.
on your <head></head> part insert a jquery.
then add this code to your head after your jquery.

<script>
$("document:).ready({
    $("#IdNameofYourResetButton").click(function(){
        $("#yourFormID")[0].reset();
    })
});
</script>

in this part everytime you click on that certain button it will execute this code that will reset your form.

0

Ultimately what the reset is doing, is destroying the variables. Unfortunately JQuery is not the solution here.

Edited by eternalhour

0

I ended up figuring this out. The probem was here:

header('location: ' . $_SERVER['HTTP_REFERER']);

After the variable was passed, the querystring was still in the URI, so when I reset the form it was referring me back to the same page and passing the variable again. Changed the location to the absolute address of the contact form and problem solved.

Edited by eternalhour

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.