tags:

views:

933

answers:

5

I'm encountering a problem. I'm using Wordpress, but this ain't a Wordpress question.

I'm using two forms, on one form I have all the input fields and one hidden input field which I use for checking which form the user has submitted. I have saved its value as 'save'. There is another form which is just for resetting all the options and its value is 'reset'. In PHP, I check the value of the hidden field and take respective actions. But the problem is that the reset thingy isn't working.

Here is my HTML for the forms:

<fieldset>
    <form method="post">
     <!-- Some input fields here-->
     <p class="submit">
      <input name="save" type="submit" value="Save changes" />
      <input type="hidden" name="action" value="save" />
     </p>
    </form>
</fieldset>
<fieldset>
    <form method="post">
     <p class="submit">
      <input name="reset" type="submit" value="Reset" />
      <input type="hidden" name="action" value="reset" />
     </p>
    </form>
</fieldset>

In PHP, I verify them like this:

// if I change the 'save' literal to something else like 'savea', $_POST variable will not be empty
// but if I dont, then $_POST variable is NULL
if ('save' == $_POST['action']) {
    foreach ($this->cp_options as $option) {
        if (isset($_POST[$option['id']])) {
            update_option($option['id'], $_POST[$option['id']]);
        }
        else {
            delete_option($option['id']);
        }
    }

    header("Location: themes.php?page=functions.php&saved=true");
    die;
}
// if I change the 'reset' literal to something else like 'reseta', $_POST variable will not be empty
// but if I dont, then $_POST variable is NULL

elseif ('reset' == $_POST['action']) {
    foreach($this->cp_options as $option) {
        delete_option($option);
    }

    header("Location: themes.php?page=functions.php&reset=true");
    die;
}

The problem is if I change the 'reset' or 'save' literal to anything else like 'reseta' or 'saveasdfasd', $_POST variable won't be empty, but if I dont, then $_POST variable is NULL.

Any ideas on why this is happening?

A: 

I know you said that $_POST is null but are you assuming that or did you actually check $_POST == null? Have you tried doing a var_dump($_POST) to print out exactly what is getting sent across? Just comment out your re-direction and see what is in $_POST. Maybe that will give you a better clue as to what is happening.

Anthony
I did a var_dump($_POST) and it shows an empty array.
hab
A: 

[Old Answer Redacted]

EDIT

Try to isolate your testing environment first. This gave me results I expected.

<?php

if ( isset( $_POST['action'] ) )
{
  switch( $_POST['action'] )
  {
    case 'save':
      echo 'Save Action Requested';
      break;
    case 'reset':
      echo 'Reset Action Requested';
      break;
    default:
      echo 'Unknown action requested:';
      var_dump( $_POST['action'] );
  }
} else {
  echo 'No action parameter received';
}

?>
<fieldset>
    <form method="post">
        <!-- Some input fields here-->
        <p class="submit">
                <input name="save" type="submit" value="Save changes" />
                <input type="hidden" name="action" value="save" />
        </p>
    </form>
</fieldset>
<fieldset>
    <form method="post">
        <p class="submit">
                <input name="reset" type="submit" value="Reset" />
                <input type="hidden" name="action" value="reset" />
        </p>
    </form>
</fieldset>
Peter Bailey
That is quite ridiculous, what if you have multi-language site? Then value of submit buttons is different for every language?
usoban
Well, when I hit the Reset button I don't want the data of the first form and this doesn't answers my question :(
hab
I'm sorry - I thought I understood the nature of your question, but upon a 2nd reading I realize I missed the point of your question. Consider my answer redacted.
Peter Bailey
@usoban - It's not ridiculous. It's the only way to achieve mutli-submit-button forms w/o using javascript. Besides, for an multi-language site you're going to have a store of i18n data so it would be trivial to normalize the submitted value with a reverse lookup of some sort.
Peter Bailey
@Pater thank you, I'll think about that :)
usoban
A: 

Simple... remove the hidden inputs and change both of your "submit" buttons to have the same name, but different values:

<input type="submit" name="action" value="Reset" />
<input type="submit" name="action" value="Save" />

Then you can test it like this:

if ($_POST['action'] === 'Reset') {
    // Do a reset here
} else {
    // Do a save here
}

And you probably want to wrap the whole thing in:

if (isset($_POST['action'])) {
    // Put your form handling here
}
shadowhand
Done but still ain't working. I guess this is an issue of Wordpress because I couldn't reproduce the behavior without WordpressThanks y'all
hab
A: 

If you have multiple forms on one page I'd recommend you send each form to a different URL. This is by far the simplest and most reliable way to detect where the form is going, just have two different scripts to deal with processing that form. You can then include or redirect to the final page you want the user to see.

simonrjones
A: 

This might because there are duplicate elements with the same name. Can you try putting an id or name to your form?

jerjer