SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Quick PHP advice for a newbie who (almost) has it...

  1. #1
    Non-Member
    Join Date
    Mar 2010
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Quick PHP advice for a newbie who (almost) has it...

    Hi,

    I have a PHP sendmail script (working) and I want to say:

    • IF a checkbox is checked
    • Then check for additional empty fields


    • IF those additional fields are empty = redirect to an error page
    • IF they are not empty = continue to submit and process the form as normal



    The code i've managed to write myself does redirect if the additional fields are empty, but when they are populated (which is the aim) for some reason it doesn't continue to process the form. Any idea why!?

    This is my PHP code snippet:

    Code:
    // If checkbox is checked
    if($checkbox == '1'){ 
        // Then check is field1 and field 2 are empty
    	if (empty($field1) || empty($field2)) {
            // If field 1 or 2 are empty, redirect user to the errorURL
    	   header( "Location: $errorurl" );
    	   exit ;
    	}
    exit(); 
    }

  2. #2
    Non-Member
    Join Date
    Mar 2010
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...scrap that.

    I've just tried removing the second "exit()" after the inner IF statement, and it seems to be working now.

    I'll leave this open til morning incase anyone has anything to add.

    Cant believe I didnt see or think of that before posting (but I did say I was a newbie!) lol.

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,849
    Mentioned
    16 Post(s)
    Tagged
    1 Thread(s)
    Just an observation really ...

    When preparing to code a condition such as that get into the habit of var_dump() ing all of the variables involved, and in you condition check not only that the values match but that also the type matches too, use " === " (type and value are the same ie Identical) instead of "==" (value only is the same ie Equal).

    Also, stub out the actions with simple echo rather that writing redirects etc straight away.
    PHP Code:
    if($condition){
      echo 
    'show form here';
      exit();
    }else{
      echo 
    'exit and redirect to home page';
      exit();

    Its only simple stuff, but might save you some heartache when starting out. You can always comment those lines out for debugging/sanity checks later, then remove them before going live.

    Given that PHP is a loosely typed language (string "1" is evaluated as being int 1, and 1 is evaluated as Boolean true) really appreciating the diffs between "==" and "===" is a critical piece of information.

    http://php.net/manual/en/language.op...comparison.php

    because:

    http://php.net/manual/en/types.comparisons.php

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •