So I am having a similar issue to this post: PHP strpos not working , but not quite.
Here is my situation (from a CodeIgniter application):
$_submit = strtolower($this->input->post('form-submit'));
if(strpos('save', $_submit) !== FALSE){
// we have to save our post data to the db
}
if(strpos('next'), $_submit) !== FALSE){
// we have to get the next record from the db
}
The problem is, neither of these actually fire, despite form-submit containing one, or both of those values. The values form-submit receives are: 'save', 'save-next', and 'skip-next' (which I have confirmed by looking at the post data as it comes in). Now for the real head scratcher, I also have this line in the same code chunk:
if ($_submit === 'add-comment'){
//do something
}
And that works perfectly fine. So === is working as expected, but !== is not?
You are giving arguments wrong to the strpos function.......
$submit = strtolower($this->input->post('form-submit'));
if(strpos($submit,'save') !== FALSE){
// we have to save our post data to the db
}
if(strpos($submit,'next') !== FALSE){
// we have to get the next record from the db
}
Please look into the strpos function in php.net manual....first argument is full string and second one is key string
You can find a small example here also.
Your arguments to strpos
are the wrong way round: as the manual states it is strpos ( string $haystack , mixed $needle )
. In your code you are looking for the needle $_submit
in the haystack 'save'
.
So if(strpos($_submit, 'save') !== FALSE)
[Of course, when testing 'save'
against 'save'
, either way around will work, which is probably what confused you.]
I would recommend to use switch instead of multiple if conditions.
$_submit = strtolower($this->input->post('form-submit'));
switch($_submit) {
case 'save':
case 'save-comment': // example for different spelling but same action...
// we have to save our post data to the db
break;
case 'next':
// we have to get the next record from the db
break;
case 'add-comment':
// we have to save our post data to the db
break;
default:
die('Unknown parameter value');
break;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.