Page 1 of 3 123 LastLast
Results 1 to 10 of 24
  1. #1
    Join Date
    Oct 2007
    Posts
    824
    Tokens
    71

    Latest Awards:

    Default [PHP] Proper Way To Check For POST?

    What's the proper way to check to see if POST data was submitted?

    if($_POST) doesnt work and if($_POST['submit']) doesnt work if I had a submit button named submit.
    Vouches
    [x][x]

  2. #2

    Default

    PHP Code:
    if ($_POST)
    {
       
    // Post submited

    That works for me also.
    PHP Code:
    if (empty($_POST) === false)
    {
       
    // Post submited

    But I usually do..
    PHP Code:
    if (isset($_POST['submit']))
    {
       
    // Post submited

    So yeah, look into empty and isset functions.
    Last edited by Iszak; 28-02-2009 at 02:08 AM.

  3. #3
    Join Date
    Sep 2008
    Location
    UK
    Posts
    3,670
    Tokens
    0

    Latest Awards:

    Default

    Usually I use.

    PHP Code:
    if (empty($var)) //Define your variable here
    {


    Back for a while.

  4. #4
    Join Date
    Dec 2006
    Location
    Swindon
    Posts
    3,299
    Tokens
    215
    Habbo
    dunko

    Latest Awards:

    Default

    Use Iszaks or use

    PHP Code:
    if( $_POST['variable'] != '' )
    {
    // posted


  5. #5
    Join Date
    Oct 2007
    Posts
    824
    Tokens
    71

    Latest Awards:

    Default

    if($_POST) and if(isset($_POST['submit']) don't work for me... they just refresh the page
    Vouches
    [x][x]

  6. #6
    Join Date
    Sep 2008
    Location
    UK
    Posts
    3,670
    Tokens
    0

    Latest Awards:

    Default

    Quote Originally Posted by Fazon View Post
    if($_POST) and if(isset($_POST['submit']) don't work for me... they just refresh the page
    if(!$_POST)
    Back for a while.

  7. #7
    Join Date
    Oct 2007
    Posts
    824
    Tokens
    71

    Latest Awards:

    Default

    isn't

    if($_POST) {
    // process info
    } else {
    // error msg
    }

    and

    if(!$_POST) {
    // error
    } else {
    // process data
    }

    the same?
    Vouches
    [x][x]

  8. #8
    Join Date
    Sep 2008
    Location
    UK
    Posts
    3,670
    Tokens
    0

    Latest Awards:

    Default

    Quote Originally Posted by Fazon View Post
    isn't

    if($_POST) {
    // process info
    } else {
    // error msg
    }

    and

    if(!$_POST) {
    // error
    } else {
    // process data
    }

    the same?
    Yeah, just in reverse order. But what I was saying is if you're wanting to check if data is empty, use either the empty function or !$_POST['lol']. You need to make sure they're submitting the form by doing something like:

    PHP Code:
    if(isset($_POST['form']))
    {


    That will check if they have posted the form. On the submit name just add 'form'.

    This isn't the best method but it'll work.

    Edit: Oh and add the method as POST.
    Last edited by Excellent2; 28-02-2009 at 11:33 PM.
    Back for a while.

  9. #9
    Join Date
    Oct 2007
    Posts
    824
    Tokens
    71

    Latest Awards:

    Default

    Same problem. Page just refreshes.

    For example:

    PHP Code:
    <?php
    if(!$_POST) {
     echo 
    "hi $_POST['name']";
    } else {
    echo 
    '
     <form method="post" enctype="text/plain">
     Name: <input type="text" name="name"> <br />
     <input type="submit" value="Submit" name="submit">
     </form>
    '
    ;
    ?>
    same with isset
    Vouches
    [x][x]

  10. #10
    Join Date
    Oct 2007
    Location
    Luton, England
    Posts
    1,548
    Tokens
    388
    Habbo
    DeejayMachoo

    Latest Awards:

    Default

    Quote Originally Posted by Fazon View Post
    Same problem. Page just refreshes.

    For example:

    PHP Code:
    <?php
    if(!$_POST) {
     echo 
    "hi $_POST['name']";
    } else {
    echo 
    '
     <form method="post" enctype="text/plain">
     Name: <input type="text" name="name"> <br />
     <input type="submit" value="Submit" name="submit">
     </form>
    '
    ;
    ?>
    same with isset
    PHP Code:
     <?php
    if($_POST) {
     echo 
    "hi $_POST['name']";
    } else {
    echo 
    '
     <form method="post" enctype="text/plain">
     Name: <input type="text" name="name"> <br />
     <input type="submit" value="Submit" name="submit">
     </form>
    '
    ;
    ?>


Page 1 of 3 123 LastLast

Posting Permissions

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