0

i am just fixing some form validation, and i seem to have a very odd issue.

$newsletter = $_POST['newsletter']; // value 0 or 1 is passed from form, as "value"
// input type is radiobuttens

then i have a very simple

if ($newsletter != "0" || $newsletter != "1") {
    $error_message .= 'Invalid newsletter?<br />';
}

it does not pass this? i did try:

if ($newsletter != 0 || $newsletter != 1) {

does not pass either, but thats comparing a string to an int as far as i know

8
  • What are you trying to accomplish? Edit: What do you get if you echo $newsletter ? That will probably help you answer it yourself Commented May 3, 2013 at 13:36
  • Im trying to filter the validation and prevent html editing, i have a form with a input type radiobutten newsletter, yes = 1 no = 0, very simple, it gets sent to the form, and i want to filter it and return an error if it does not = 1 or 0 Commented May 3, 2013 at 13:37
  • I think you need && instead of || in the query - an OR is matched if either side is true, and the number is always going to be a 0 (which matches != 1) or 1 (which matches != 0) Commented May 3, 2013 at 13:37
  • 1
    x != A || x != B is always true iff A != B. Commented May 3, 2013 at 13:38
  • 1
    I am a retard it is supposed to be and Commented May 3, 2013 at 13:38

4 Answers 4

1

You'll have to change the statement to &&

if ($newsletter != "0" && $newsletter != "1") {

This will check if it is neither 0 nor 1.

Sign up to request clarification or add additional context in comments.

Comments

0

If it's "0", it's not "1". If it's "1", it's not "0". So it will always either not be zero or not be one. No value would pass that test.

You only want to return an error if it's not equal to "1" and it's also not equal to "0".

Comments

0

Answering the comment, you can just do:

if(!$newsletter) { $error_message .= 'Invalid newsletter?<br />'; }

Comments

0

You want an AND comparison. I am assuming that you want to make sure that only a value of 0 or 1 is passed to the script.

if ($newsletter != "0" && $newsletter != "1") {
    $error_message .= 'Invalid newsletter?<br />';
}

Since 0 != 1 either of side of your OR condition was true. So this always passed.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.