Newbie: Question about $_Post

Discussion in 'PHP' started by irule272, Jun 25, 2009.

  1. #1
    I'm an average C#.net programmer but that's way back before and I almost forgot how to code, I just started learning php an hours ago in w3school and I'm confused on the $_Post function.


    based on my understanding on the tutorial you can't put the $_Post function together with the for example Registration form. You need to have a landing page where the php code will execute if the user click the submit button am I right? Because if i try to put it together the php code will execute even if the textboxes are empty everytime the page will load. Because as far as I remember in .net there's a portion there where you can put a code in a page load event together with the textboxes on the same page.


    I have this on my mind to use if statement but I just want to clarify what's the proper way to insert data in mysql using php? Can you guys also provide a sample code for this on how you do it?

    I know this is very basic for you guys. I just want to clarify it as a beginner so that the next time I will code I can do it the right way.

    and btw where's the best website to learn php besides w3school and lynda.com tutorial videos?


    Thanks!
     
    irule272, Jun 25, 2009 IP
  2. credobyte

    credobyte Peon

    Messages:
    38
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $_GET :
    http://www.domain.com/login.php?user=admin&pass=123456
    Code (markup):
    $_POST :
    http://www.domain.com/login.php
    Code (markup):
    Basically, you can use it to pass some data from one page to another by hiding all the details ( as you can see, GET method allows you to modify request ). Registration forms, login forms, etc. - whatever you need.
    If you send something from index.php to login.php, without using JavaScript/AJAX, you can't check data integrity. The easiest way is to make a few if/elseif blocks on your landing page ( checking empty boxes, string parsing, MySQL queries, etc. ).
     
    credobyte, Jun 25, 2009 IP
    irule272 likes this.
  3. zeronese

    zeronese Peon

    Messages:
    83
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    you can have your php code on the same page or on a seperate php page. you can tell your form where the code will be executed using the action tag of the form.

    also, you can tell if the $_post array by using an if statement, example:
    if($_post){
    // insert values to mysql
    }
    else{
    //just display the form
    }
    PHP:
    A typical post form would be something like this

    include the code in a form.php file
    <?php
    if($_POST){
    	$fieldname = $_POST['fieldname'];
    	//do any thig here, like verifications, insert to mysql.... etc
    	echo "You have submitted the following text: ".$fieldname;
    }
    else{
    	$fieldname = "enter test string here";
    }
    ?>
    <form action="" method="post">
    <input name="fieldname" type="text" value="<?php echo $fieldname;?>" />
    <input name="" type="submit" />
    </form>
    PHP:
    Good luck :)
     
    zeronese, Jun 25, 2009 IP
    irule272 likes this.
  4. irule272

    irule272 Well-Known Member

    Messages:
    1,153
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    155
    #4
    You guys are the best!

    Didn't know php is this hard compared to C#.net which have GUI.
    good thing is MySql and MS Sql have the same syntax.

    what website do you recommend for beginner? beside w3school?

    Thanks again to both of you repped!
     
    irule272, Jun 25, 2009 IP
  5. credobyte

    credobyte Peon

    Messages:
    38
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Personally, C# is harder than PHP :p

    • http://www.tizag.com/phpT/
    • http://lv.php.net/manual/en/
     
    credobyte, Jun 26, 2009 IP
  6. susan8051

    susan8051 Peon

    Messages:
    1,358
    Likes Received:
    49
    Best Answers:
    0
    Trophy Points:
    0
    #6
    susan8051, Jun 26, 2009 IP
  7. irule272

    irule272 Well-Known Member

    Messages:
    1,153
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    155
    #7
    ^Maybe that's my 1st language that I learned :)
     
    irule272, Jun 26, 2009 IP
  8. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Sorry but I thought it might be appropriate to ask here, when is it appropriate to use $_GET ?
     
    wd_2k6, Jun 26, 2009 IP
  9. zeronese

    zeronese Peon

    Messages:
    83
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #9
    you use $_GET when you need to pass the query in the url. it is mostly used in search queries and pagination. when you use the $_GET, the variables will show in your url

    if you need the $get array to be filled from a form, the method tag in the form has to be set to method="get" instead of method="post"

    for example when you query:
    page.php?page_number=2
    PHP:
    then if
    $pagenumber = $_GET['page_number'];
    PHP:
    $pagenumber is now set to 2
     
    zeronese, Jun 26, 2009 IP
  10. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thanks, this is where I was getting stuck in my tutorial I never knew you could pass it through the URL like that. So in real world usage, let's say I had a

    homepage.php

    Then I could have in my products.php

    And would Search Engine would see the 3 products as 3 different pages even though i've only written 1 ? That's pretty cool if so.
     
    wd_2k6, Jun 26, 2009 IP
  11. zeronese

    zeronese Peon

    Messages:
    83
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #11
    exactly, that is the purpose of dynamic web development.
    only watch out, your query should be:
    $result = mysql_query(SELECT * FROM products WHERE name = '$item');


    also it is alway recommended not to pass the query string directly to the mysql_query function.
    so better do this:
    $query = "SELECT * FROM products WHERE name = '$item'";
    $result = mysql_query($query);
     
    zeronese, Jun 26, 2009 IP
  12. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Thanks for the tips zeronese i'm just beginning to learn so tips like this are of great help.
    It's great when you start to realise the possiblities of PHP/MySQL
     
    wd_2k6, Jun 26, 2009 IP
  13. NatalicWolf

    NatalicWolf Peon

    Messages:
    262
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #13
    NOTE: headers are data sent to the server for use in the execution of either a server side code, or locally for rendering of content...

    Ok, I hope I can help clear this up.

    POST and GET definitions (in my words)

    GET supports up to 255 characters, which is part of the max length of a URL. You use GET to pass simple variables (theme change, page, language, ref id, etc).

    POST on the other hand, is sent AFTER the headers of a request. It is not effected by a 255 character limit either.

    A GET request is as follows (Shortened for example)

    GET /page.php?page=1 HTTP/1.1
    HOST: google.com
    -- Followed by 2 or 4 return spaces, im tired so I'm not sure

    Notice the ?page=1 which is parsed by PHP into the $_GET super global variables (means no matter where in code it's used, it is accessible...)

    POST Example

    POST /page.php HTTP/1.1
    Host: google.com
    content-length: 10 (This is the length of the POST data)
    (One return space)
    test=12345
    (Followed by 2 returns...I think, but that's not important)

    Notice in POST the data is AFTER all other headers.

    When do I use POST vs GET
    • File transfers (form/multipart-data) - POST
    • Page selection - GET
    • Passing information to another site -- Both depending on the content
    • Contact form -- Both...but recommended to be POST
    I am available for consulting if you wish to receive more help on understanding the differences and uses.

    Hope this helps in some way...
     
    NatalicWolf, Jun 26, 2009 IP
  14. dweebsonduty

    dweebsonduty Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    Digital Goods:
    1
    #14
    PHP + MYSQL is soo much easier $_POST["field"] is so easy to work with.
     
    dweebsonduty, Jun 27, 2009 IP