View Full Version : Isset not working in chrome
ankush
10-18-2013, 02:38 PM
<input type="submit" value="Delete" name="b1">
<input type="submit" name="b2" value="Save">
<?php
if (isset($_POST["b1"])){
echo "delete";
} else if (isset($_POST["b2"])){
echo "saved";
}
?>
It is not working in chrome but working in firefox, any one can help?
Snookerman
10-18-2013, 03:30 PM
Use !empty instead because $_POST is always set, even if it's empty.
djr33
10-18-2013, 06:59 PM
Use !empty instead because $_POST is always set, even if it's empty.
The whole array ($_POST) is always set. But some particular index like $_POST['x'] is only set if it was submitted. So isset() should work. However, it is possible that if you have a form submitted with an optional field "x", that $_POST['x'] would be set but empty, because they entered no text. In that case it would be equivalent to '', so empty() would work. But it depends on context.
Overall, though, there's no harm using empty(), unless you'd like to distinguish the variable not existing from it having a blank value (as in the case of a form submission, where you might want to check whether the form has been submitted, with any value even the empty string).
djr33
10-19-2013, 03:00 AM
To add to my post above, in response to the original question, let me clarify a couple things:
1. It is completely irrelevant which browser you are using; PHP will do exactly the same thing. However, in this case the difference is likely due to how those browsers handle forms (empty fields), so that PHP is getting different input (which of course would make a difference).
2. My guess is that one browser is treating an empty form field as a null string (set to an empty value) and the other is treating the same empty field as not having a value. The information in the posts above should help you deal with this. Try isset() and !empty() to see which is best. Another approach would be to pre-process the input-- if no value for 'x' was submitted, set it to '', and so forth.
jscheuer1
10-19-2013, 03:25 AM
<input type="submit" value="Delete" name="b1">
<input type="submit" name="b2" value="Save">
<?php
if (isset($_POST["b1"])){
echo "delete";
} else if (isset($_POST["b2"])){
echo "saved";
}
?>
It is not working in chrome but working in firefox, any one can help?
That's not a valid form. If it's made to be one, works fine here, or the same as Firefox (WAMP server, PHP 5.3, Chrome 30.x, Firefox 24.0):
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="submit" value="Delete" name="b1">
<input type="submit" name="b2" value="Save">
</form>
<?php
if (isset($_POST["b1"])){
echo "delete";
} else if (isset($_POST["b2"])){
echo "saved";
}
?>
djr33
10-19-2013, 04:51 AM
Interesting. It certainly could be that the behavior of forms varies by whether they're valid HTML, or even by DOCTYPE.
ankush
10-19-2013, 05:47 AM
form.php
<form name="frm" action="submit.php" method="post">
<input type="submit" value="Delete" name="b1">
<input type="submit" name="b2" value="Save">
</form>
submit.php
<?php
if (!empty($_POST["b1"])){
echo "delete";
} else if (!empty($_POST["b2"])) {
echo "saved";
}
?>
no luck, nothing works for me, neither isset nor !empty
i have tried with
<?php
if (!empty($_POST["b1"])){
echo "delete";
} else if (!empty($_POST["b2"])) {
echo "saved";
}
?>
above condition working fine in firefox but print nothing when click on delete button and print saved when click on save in chrome
and
<?php
if (!empty($_POST["b1"])){
echo "delete";
} else {
echo "saved";
}
?>
above condition working fine in firefox but in both cased chrome is printing saved
i am trying to delete multiple records through delete button and wants to update a query through save button using check box in a single form, that's why i am trying to know, which button is clicked based on condition, so that i can perform particular action but no luck
i am using (Version 30.0.1599.101 m) chrome, any other way to do this?
ankush
10-19-2013, 05:59 AM
Oops! tried on local host using easyPHP, working perfectly in chrome now. I think something wrong with my host, is this anything i can fix my self or i have to ask my host?
jscheuer1
10-19-2013, 11:49 AM
If it works in one browser on the live host, then it's hard to see how it's a problem with the host. However, I do have access to a live server with PHP where I can test things. I cannot give out the address though. On it, it works fine in Chrome. So that tends to support the theory that it's something with the host you're using. Perhaps they automatically attach/add code to their pages, and that's somehow messing things up in Chrome. Or perhaps they have some sort of weird configuration that messes up Chrome. If the former, a link to the live page would help us to see that, if the latter, you would have to have access to the configuration or ask the host.
If you want more help, please include a link to the page on your site that contains the problematic code so we can check it out.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.