• Home
  • PHP
  • MySQL
  • Laravel
  • Demos
  • HTML
  • jQuery
  • Framework
  • Request Tutorial
PHP Lift
  • Home
  • Demos
  • Advertisement
PHP Lift
  • Home
  • PHP
  • MySQL
  • Laravel
  • Demos
  • HTML
  • jQuery
  • Framework
  • Request Tutorial
  • Follow
    • Facebook
    • Twitter
    • Google+
    • Pinterest
    • Youtube
    • Instagram
    • RSS
Passing input parameters in PHP
Home
PHP

Passing input parameters in PHP

April 30th, 2025 Ivan Dimov PHP 0 comments

Facebook Twitter Google+ LinkedIn Pinterest

In this brief article, we will see how to work with forms and inputs in PHP.

Using HTML forms, you can pass parameters to the PHP back-end. Let us examine the following form:

<form action=’’>



<input type=’text’ name=’name’>



<input type=’submit’ name=’submit’ value=’Submit’>



</form>

Now, this form will perform a GET submission. In PHP, you can get the text input with the following code:

If (array_key_exists(“name”, $_GET)) {



// do something with $_GET[‘name’]

}

Should the form be using a POST request method

<form method=’post’ action=’’>



<input type=’text’ name=’name’>



<input type=’submit’ name=’submit’ value=’Submit’>



</form>

Now, the difference is that the form above uses POST for submission. A major difference between GET and POST is that GET submits the parameters as part of the URL. The previous submission would have looked like this with GET: page.com?name=someName&submit=Submit. An obvious drawback with such submission is that it would stay within the browser’s history, it can be bookmarked and shared which is totally not a good idea when the data ought not to be shared. Therefore, for data which is not meant to be shared publicly and data that is private or sensitive always use POST.

In PHP, you can get the name input from a POST submission by using the $_POST superglobal instead of $_GET:

If (array_key_exists(“name”, $_POST)) {



// do something with $_POST[‘name’]

}

It is also possible to expect both POST and GET submissions with the same inputs; or allow both for an API, etc. In such a case, you can simply check the $_REQUEST superglobal for the desired input, and if the input exists from a POST or a GET submission, it will be available in the $_REQUEST superglobal.

If (array_key_exists(“name”, $_REQUEST)) {



// do something with $_REQUEST[‘name’]

}

There are also PUT and DELETE request methods with the HTTP protocol but they are not typically used with forms yet. You may still rely on it when using APIs though. DELETE is used when you want to delete some form of a record and PUT is used when you want to put some file or a record at a specific URL.

Finally, the form could submit to any/arbitrary URL, where you could check the $_GET, $_POST, or $_REQUEST superglobals for inputs.

<form action=’myscript.php’>



<input type=’text’ name=’name’>



<input type=’submit’ name=’submit’ value=’Submit’>



</form>

In the case above, we should have a file called myscript.php in which we check for the inputs.

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook

Related

  • Tags
  • PHP
Facebook Twitter Google+ LinkedIn Pinterest
Next article Using the PayPal Payments API with PHP and Guzzle
Previous article WhatsApp chatbot in Python using Dialogflow.com

Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: http://www.dimoff.biz. facebook, twitter

Related Posts

Web 3.0 and PHP: A Comprehensive Guide Guide
June 16th, 2025

Web 3.0 and PHP: A Comprehensive Guide

8 interesting functions of Laravel Eloquent (ORM) PHP
June 16th, 2025

8 interesting functions of Laravel Eloquent (ORM)

Top Laravel packages for advanced programmers Framework
June 16th, 2025

Top Laravel packages for advanced programmers

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe
Get new posts by email:
Powered by follow.it
Advertisement
Like us
Recent Posts
  • Web 3.0 and PHP: A Comprehensive Guide
  • 8 interesting functions of Laravel Eloquent (ORM)
  • How to create a screen recorder in JavaScript
  • Top Programming Languages in 2024
  • 9 Tricks and Tips to Search Google Like a Pro
Categories
  • API
  • Bootstrap
  • Bot
  • CSS
  • CSS 3
  • Database
  • Designing
  • Framework
  • Guide
  • HTML
  • HTML 5
  • JavaScript
  • jQuery
  • Laravel
  • MySQL
  • Node.js
  • oAuth
  • Payment
  • PHP
  • Python
  • Social
  • Tips
  • Web 3.0
  • WordPress
Weekly Tags
  • PHP
  • How to
  • javascript
  • laravel
  • MYSQL
  • PHP framework
  • css
  • jQuery
  • HTML to PDF
  • Web Development
  • About
  • Privacy Policy
  • Back to top
© PHPLift.net. All rights reserved.