简体   繁体   中英

A beginner's question…How do I use $_POST instead of $_GET? (PHP)

Okay, for all my coding so far I been using GETs and only $_POST on the same script.

Ex: profile.php would show all the posts an user made and under each of their post, they can edit their post. The way I been using it is through $_GETs. So..

    //this is the link users click to edit their post
    echo '<td><a href="editad.php?posting_id='.$row3['posting_id'].' ">Edit</a>';

//on the editad.php
$posting_id=$_GET['posting_id'];
if (isset($posting_id)){
   //show all the forms, sqls to do the editing
}
else{ //if the $_GET hasn't been set
   echo "You have not specified which ad to edit. Please go back";
}

How do I do this via POST? Thanks :)

The browser will submit a POST request when the HTML <form> element contains method="POST" . You do not need to do anything special on the PHP end.

<form action="index.php" method="POST"><input type="text" name="blue"></form>

When submitted, PHP will see a variable $_POST['blue'] with the content of whatever was in the text that was submitted.

That said, take heed. In the first line of the code you give, you should stick with the message ID in the URL because that is GET, which you should use when no change is made to the data.

Also, since I saw your other question, let me just remind you to make sure that you check that the user has the permission to see that page. For example, search engines will crawl GET pages, but not POST.

Finally, there is no way to force the browser to use POST without it being specified AFAIK, so there's no way to do it in pure PHP.

Usually you use POST variables through html forms.

You cannot use POST variables in links, however. By using post variables, the variables get submitted behind the scenes.

Explaining the functionality of post variables, especially if you haven't been introduced to it before can be tedious, so I'm going to point you to a few links:

Explaining $_POST: http://w3schools.com/php/php_post.asp

A tutorial explaining the difference between $_POST and $_GET http://www.tizag.com/phpT/postget.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM