May 2, 2012

make an ifnot statement and if statement in one line

Question by sven30

I’m trying to make an if statement with 2 conditions. One that checks if one variable is NOT present & does NOT matches the word “good2go” and the other that checks to make sure “body” variable is present. I’m trying to trip the error message here. Here is what I have and what I’ve tried, and none of it seems to work.

if (stripos($_POST['check'], 'good2go') == FALSE && $_POST['body']) {        
                $error = true; } 

if (!$_POST['check'] == 'good2go' && $_POST['body']) {  
                $error = true; }

if (!stripos($_POST['check'], 'good2go') && $_POST['body']) {   
                $error = true; }

if ((!stripos($_POST['check'], 'good2go')) && $_POST['body']) { 
                $error = true; }

How do I get this to work?

here’s the entire code of contact_us.php this has the validation code and the email code.

    $error = false;
  if (isset($_GET['action']) && ($_GET['action'] == 'send')) {

          // Winnie the pooh check
         //$t = tep_db_prepare_input($_POST['verify']);
         if (!isset($_POST['check']) && !$_POST['check']=='good2go' && isset($_POST['body'])) {
                $error = true;              
        } else  {  // Winnie the pooh Check

    $name = tep_db_prepare_input($_POST['name']);
    $email_address = tep_db_prepare_input($_POST['email']);


//IP recorder start
$ipaddress = $_SERVER["REMOTE_ADDR"];
$ip = "nnIP: " . $ipaddress;
$content = "nnName: ".$name."nnComments: ".$_POST['enquiry'];
$product = tep_db_prepare_input($_POST['product']);
    if ($product) { 
$product_text = "nnProduct Interest: ".$product; }
$content_ip = $content . $product_text. $ip;
$enquiry = tep_db_prepare_input($content_ip);
//IP recorder end


        }
 // BOF: Remove blank emails
// if (tep_validate_email($email_address)) {
// tep_mail(STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS, EMAIL_SUBJECT, $enquiry, $name, $email_address);
// tep_redirect(tep_href_link(FILENAME_CONTACT_US, 'action=success'));
// } else {
// $error = true;
// $messageStack->add('contact', ENTRY_EMAIL_ADDRESS_CHECK_ERROR);
    if (! tep_validate_email($email_address)) {
        $error = true;
        $messageStack->add('contact', ENTRY_EMAIL_ADDRESS_CHECK_ERROR);
    }
    if ($enquiry == '') {
        $error = true;
        $messageStack->add('contact', ENTRY_EMAIL_CONTENT_CHECK_ERROR);
    }
    if ($error == false) {      
      tep_mail(STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS, EMAIL_SUBJECT, $enquiry, $name, $email_address);

      tep_redirect(tep_href_link(FILENAME_CONTACT_US, 'action=success'));
// EOF: Remove blank emails
    }
  }

Answer by dweiss

Solution to your updated problem:

if (!isset($_POST['check']) || !$_POST['check']=='good2go' || !isset($_POST['body'])) {
                $error = true;              
} 

The reason for the pipes vs ampersands is that you want to throw an error if ANY of the fields has issue. Also, you want to check if body is NOT set vs IS set. Glad this worked out for you!

Answer by Starx

No need for all those unneeded functions. What you are trying to achieve is:

if (isset($_POST['check']) && $_POST['check']=='good2go' && !isset($_POST['body']) {
   // your code 
}

However, As per the title of the question: Use a ternary statement. Syntax is as such

$var = <condition> ? <true> : <false>;

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!