SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast mihai001's Avatar
    Join Date
    Sep 2003
    Location
    Bucharest
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Fields check problem

    Howdy!

    I have this little piece of code:

    PHP Code:
    $missingfieldfalse;
    foreach (
    $_POST as $k => $v

        if (
    trim($v) == ''
        { 
            echo 
    '<span class="textlink=9"><font color="#993333">Campul <b> '.$k.' </b>nu este completat</span><br>'
            
    $missingfield true
        } 


    if (
    $missingfield

    echo 
    '<font size="2" color="#9C0A0D" face="Arial, Helvetica, Tahoma, sans-serif">Va rugam sa va<a href onClick="history.go(-1)" class="button0"><b> intoarceti </b></a> si sa completati datele lipsa</font>';
    die(
    $footer);

    that was supposed to check for missing fields.
    How can I make it check for some empty fields while excepting others?

    Thanx a lot

  2. #2
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this.
    PHP Code:
    $missingfieldfalse

    $notempty = array('name''phone'); // Form field NOT allowed to be left empty

    foreach ($_POST as $k => $v

        if (
    in_array($k$notempty) && trim($v) == ''
        { 
            echo 
    'Campul <b> '.$k.' </b>nu este completat</span><br>'
            
    $missingfield true
        } 


    if (
    $missingfield

    echo 
    'Va rugam sa va<a href onClick="history.go(-1)" class="button0"><b> intoarceti </b></a> si sa completati datele lipsa'
    die(
    $footer); 

    -Helge

  3. #3
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's a logic error here. the if($missingfield) branch doesn't need to be there. Instead, include all the contents of that branch in the foreach loop since...$missingfield will always be true (you wrote your code that way!).
    Aaron Brazell
    Technosailor



  4. #4
    SitePoint Enthusiast mihai001's Avatar
    Join Date
    Sep 2003
    Location
    Bucharest
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanx a lot Helge!This did the trick!

    But I don't think I have this logic error Sketch , because the last echo is a back button that has to apear only once .

    If I'd put it in the foreach loop it would apear on every echo for empty field, and I don't want that.

  5. #5
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, well the point remains...there's no reason to check for $missingfield. Or is there? The code you are using seems to indicate that 100% of the time, $missingfield will be true. So why perform the check?
    Aaron Brazell
    Technosailor



  6. #6
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sketch. I don't see why $missingfield allways will be true? It only sets to 'true' if the if-clause evaluates to true.

    Try this test code
    PHP Code:
    error_reporting(E_ALL);
    $bool false;

    if(
    $bool) {
        echo 
    'This won\'t show before $bool is set to "true" (without the quotes)';

    Am I missing something?

    -Helge

  7. #7
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mihai001: Glad the code solved it.

    -Helge

  8. #8
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My bad. you're right. I stand corrected. I was focusing on the foreach() and failed to consider the if loop.

    Aaron
    Aaron Brazell
    Technosailor



  9. #9
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You don't need to stand corrected.

    I was just wondering if I was missing something.

    -Helge


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
  •