JIYIK CN >

Current Location:Home > Learning > PROGRAM > PHP >

Check if a Post exists in PHP

Author:JIYIK Last Updated:2025/04/13 Views:

PHP $_POSTis a super global variable that can contain key-value pairs of HTML form data submitted through the post method. We will learn different ways to check $_POSTif a and contains some data in this article. These methods will use isset(), empty()and empty string checks.


Check $_POSTif exists inisset()

isset()The function is a PHP built-in function that can check if a variable is set and not NULL. Also, it works with arrays and array keys. PHP $_POSThas array keys included so isset()it can handle it.

To check $_POSTif an exists, pass it as the value to isset()the function. Also, you can check if the user submitted a specific form input. If the user submitted a form input, it will $_POSTbe available in , even if it is empty.

The HTML below gives us something to work with. It has a form field with a pre-populated name field.

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
	<label>First Name</label>
	<input type="text" name="first_name" value="DelftStack">
	<input type="submit">
</form>

When you click the submit button, the following PHP will check $_POSTif :

<?php
    if (isset($_POST['first_name'])) {
        $first_name = $_POST['first_name'];
        echo $first_name;
    }
?>

Output:

DelftStack

Use Empty()the function to check $_POSTif

You can empty()check for the existence of using the function $ _POST. However, empty()the function will return if true:

  • When all $_POSTvalues ​​are empty strings
  • Parameter is zero

The following HTML is the same as the previous one, this time with different names:

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
    <label>First Name</label>
    <input type="text" name="first_name" value="Mathias Jones">
    <input type="submit">
</form>

The next code block shows you how to use empty()the function check $_POST:

<?php
    if (!empty($_POST)) {
        $first_name = $_POST['first_name'];

        echo $first_name;
    }
?>

Output:

Mathias Jones

Check $_POSTif function exists isset()and empty string check

If the value of $_POST is an empty string, isset()the function returns true, but for a NULL value, it will return false. If you try to print the value of isset($_POST['x']) = NULLand isset($_POST['x']) = '', in both cases you will get an empty string.

Therefore, you will need to check for empty strings. isset()The combination of and empty string check eliminates $_POSTthe possibility that contains an empty string before you process its data.

In the next code block, we have an HTML to work with:

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
    <label>First Name</label>
    <input type="text" name="first_name" value="Mertens Johanssen">
    <input type="submit">
</form>

Check $_POSTif :

<?php
    if (isset($_POST['first_name']) && $_POST['first_name'] !== "") {

        $first_name = $_POST['first_name'];

        echo $first_name;
    }
?

Output:

Mertens Johanssen

Checks $_POSTif there is a negation operator

The negation operator (!) turns a true statement into a false one and a false statement into a true one. Therefore, you can use the negation operator to check $_POSTif . To check $_POST, add the negation operator to the if-else statement.

In the first part, if $_POSTis empty, you can stop processing its data. In the second part of the condition, you can process the data.

First, the HTML:

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
    <label>First Name</label>
    <input type="text" name="first_name" value="Marcus Alonso">
    <input type="submit">
</form>

The next code block demonstrates using the negation operator to check for $_ POSTthe existence of .

<?php
    if (!$_POST) {
        echo "Post does not exist";
    } else {
        $first_name = $_POST['first_name'];

        echo $first_name;
    }
?>

Output:

Marcus Alonso

Previous:PHP with Ajax

Next: None

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

PHP with Ajax

Publish Date:2025/04/13 Views:139 Category:PHP

We will use PHP and ajax by printing a simple sum of two numbers 2 and . Also, print a php array in JSON. 3 object We will also use PHP with ajax by getting the HTML formatted output from the number division in PHP. Printing simple addition

Store Div Id in PHP variable and pass it to JavaScript

Publish Date:2025/04/13 Views:51 Category:PHP

This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s

Returns the article tag with ID from the action page

Publish Date:2025/04/13 Views:80 Category:PHP

Let's say you're in a login form and you enter the wrong information; in this case, you probably want to go back to the login page. PHP has a built-in function header() to redirect a page to a specific page. But what if the login page is at

Switching PHP versions on Ubuntu

Publish Date:2025/04/13 Views:78 Category:PHP

Different tasks may require running multiple versions of PHP. You may need to switch PHP versions by running two sites on the same server or testing older versions of code using outdated methods. We can switch PHP versions on Ubuntu using t

Resizing images in PHP

Publish Date:2025/04/13 Views:155 Category:PHP

In this tutorial article, we will discuss about resizing images in PHP. Load the image before resizing Before we can resize an image, we must first load it as an image resource in our script. This is file_get_contents() different from using

PHP upload image

Publish Date:2025/04/13 Views:61 Category:PHP

We can upload images in PHP using simple file upload operation, but first, php.ini file upload should be enabled from Files. This tutorial demonstrates how to upload images in PHP. php.ini Enable file upload from file in PHP to upload image

Creating a signature from Hash_hmac() and Sha256 in PHP

Publish Date:2025/04/13 Views:107 Category:PHP

PHP has one of the best encryption functions for data security. Hash_hmac() The encrypt function is one of the most famous encryptors. We'll show you how to use hash_hmac and sha256 encryptors to create 安全签名 one that you can store i

Updating PHP 7.x to 7.4 on CentOS

Publish Date:2025/04/13 Views:131 Category:PHP

This article shows the steps to update the PHP version from 7.x version to 7.4 in CentOS. How to Update PHP from 7.X to 7.4 in CentOS Update operating system packages. yum update -y Check your PHP version in CentOS. php -v Prints a list of

Displays PHP configuration information on localhost

Publish Date:2025/04/13 Views:107 Category:PHP

phpinfo() is a built-in function in PHP which outputs all the information of PHP configuration on the local host. We have to phpinfo() create a PHP file with a simple function call. Sometimes, the file may not work properly and output a 404

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial