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!
$_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. ).
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
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!
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
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.
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);
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
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...