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.

Member Avatar for diafol

what's wrong with a html reset button?

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

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.

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.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.