I'm having trouble using postback because I've more than one form and isset only works with name="submit" buttons.
My code will work fine if I use name="submit" but if I changed the name to something else, isset($_POST['somethingelse']) will always be false, why does this happen?
EDIT:
<input type="submit" name="submit" value="Submit" onclick ="validate(document.getElementById('form')); return false;" />
if (isset($_POST['submit'])) <-- works as expected
<input type="submit" name="asdf" value="Submit" onclick ="validate(document.getElementById('form')); return false;" />
if (isset($_POST['asdf'])) <-- does not works as expected
EDIT2:
<?php
$validated = false;
if (isset($_POST['submit'])) {
// preserve form values by storing the values from $_POST into variables
$test = $_POST['test'];
// validated is now true as submit button only submits if validated (script)
$validated = true;
} else {
// make variables empty as there's no values to preserve
$test = '';
}
if (!$validated) {
?>
<form id="form" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<p>Test: <input type="text" name="test" size="20" value="<?php echo $test ?>" /></p>
<p><input type="submit" name="submit" value="Submit" onclick ="validate(); return false;" /></p>
<?php } ?>
For example here, works as expected (I can keep the values) as name="submit" and if (isset($_POST['submit'])) is true when the button submits but if I change both the name="" and the if (isset($_POST[''])) then it will not work as the isset is returning false (even though the button submits).
It's normal because only the clicked submit button is set and not the second, if you click on submit button with name submit isset($_POST['submit']) will give you true and isset($_POST['asdf']) false, and if you click on asdf isset($_POST['submit']) gives false and isset($_POST['asdf']) true, i tested it and it works without any problem.
Test code:
<?php
$validated = false;
if (isset($_POST['submit'])) {
// preserve form values by storing the values from $_POST into variables
$test = "submit";
// validated is now true as submit button only submits if validated (script)
$validated = true;
}
else if(isset($_POST['asdf'])){
$test = "asdf";
// validated is now true as submit button only submits if validated (script)
$validated = true;
}
else {
// make variables empty as there's no values to preserve
$test = '';
}
if (!$validated) {
echo $test;
} ?>
<form id="form" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<p>Test: <input type="text" name="test" size="20" value="<?php echo $test ?>" /></p>
<p><input type="submit" name="submit" value="Submit" onclick ="validate(); return false;" /></p>
<p><input type="submit" name="asdf" value="Submit" onclick ="validate(document.getElementById('form')); return false;" />
</p>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With