Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 21

Thread: PHP classes for a newbie like me... Help plss T_T

  1. #11
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    if ($var) is a very messy way to check a value.
    Here's why:
    if (X) will return true unless X is (1) 0, (2) FALSE, (3) '' (empty string), (4) null; (5) not set*, and possibly other cases that I'm not remembering

    *However, this will also give a warning about using a variable that doesn't exist.

    There are better ways to do this, and one is:
    if (isset($var))


    The best way to debug this and to determine what sort of if statement you need is to just use echo $_POST["count"]; (immediately before the if statement).
    Then you can decide what to do.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  2. #12
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by I am Abby View Post
    after hitting the submit the code should run...otherwise not.
    The first time you run this...it does not run. If you hit the submit and the page sends the data back to itself it works correctly. After that if you refresh the page I don't want it to work.
    That's the problem: if you click Refresh, the browser usually resubmits the POST information. You can avoid this by automatically redirecting the page (you could even simply redirect it to itself) - then if the user refreshes, he'll be refreshing the new page, not the one that just submitted the form. You have to do this before you output anything, so you'll have to pass the information along somehow.

    (not tested)
    PHP Code:
    <?php

    // if the form was submitted, process the input
    if (isset($_POST['Quote']))
    {
    $newcount $_POST['Quote'] = 1;
    // instead of echoing the value, redirect ("refresh") the page 
    // and pass the value as a query string
    header('Location: '.$_SERVER['PHP_SELF'].'?newcount='.$newcount);
    }

    // if the form was not just submitted (no $_POST['Quote'])
    elseif(isset($_GET['newcount']))
    {
    // if the query string is present, echo the value
    echo $_GET['newcount'];
    }

    // you could also use sessions.

    ?>

  3. #13
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    Thanks for the help guys...
    It seems to be working now...and better than that, I'm starting to understand why.

    However it the line header('Location: '.$_SERVER['PHP_SELF'].'?newcount='.$newcount); kept giving me an error when submitting, so I'm not sure if I'm going to have problems when people refresh the page or not.
    Last edited by I am Abby; 04-14-2010 at 08:41 PM.

  4. #14
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    What is your error? If it says "Headers already sent", then it's because you've already output something to the browser. header() doesn't work unless the server hasn't sent anything to the browser.

  5. #15
    Join Date
    May 2009
    Posts
    62
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Default

    Ahw, tnx for the replies guyz.... hahahhhaha (sarcastic)... To abby, if you get error in using header, maybe there's a space b4 your <?php .... or something that you output b4 the <?php

    If you use the header function:

    <space><?php ---> this will generate an error

    <?php ---> this will not generate an error

  6. #16
    Join Date
    May 2009
    Posts
    62
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Default

    In summary:

    This is wrong when using header function:

    <html>
    <?php

    header("location: blahblahblah.php");

    ?>
    </html>

    wrong because there's a tag <html> before the <?php
    Just remember that there should be no html tags, spaces, even comments, or what so ever b4 the <?php

  7. #17
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    Quote Originally Posted by heavensgate15 View Post
    Ahw, tnx for the replies guyz.... hahahhhaha (sarcastic)... To abby, if you get error in using header, maybe there's a space b4 your <?php .... or something that you output b4 the <?php

    If you use the header function:

    <space><?php ---> this will generate an error

    <?php ---> this will not generate an error

    You guys are so smart. I had a dreamweaver created line above <?php after getting rid of it traq's line worked perfectly.

    And heavensgate15 (your name makes me smile) I didn't really mean to take over your thread. Your question was so close to what I was needing.

    Again thanks boys.

  8. #18
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Watch out for that in dreamweaver. It tends to do annoying things with spacing that in general keep the code clean and organized (so arguably it's helpful), but sometimes do weird things, especially with the issue of the header() function. So don't be surprised if you have this same problem again, but now you know how to fix it.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #19
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    of course, ANY html editor is going to have issues like that. FrontPage, for example, is far worse.

    I use notepad++ for my coding.

  10. #20
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Correct. But to clarify, DW specifically likes to add a return before <?php sometimes.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •