Check if $_POST exists
if( isset($_POST['fromPerson']) )
{
$fromPerson = '+from%3A'.$_POST['fromPerson'];
echo $fromPerson;
}
Is there a way to check if a $_POST exists?
For a single key:
if(isset($_POST['myKey'])) {
echo "myKey exists"
}
If you want to know if there is any POST data:
if(!empty($_POST)) {
echo "We have POST data"
}
Php if($_POST) vs if(isset($_POST))
isset
determine if a variable is set and is not NULL. $_POST
will always be set and will always be an array.
Without isset
you are just testing if the value is truthy. An empty array (which $_POST
will be if you aren't posting any data) will not be truthy.
PHP Correctly checking if a post variable is empty
This link may be useful: https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/
Use the empty
function to check if a variable contents something:
<?php
if (isset($_POST['var']) && !empty($_POST['var'])) {
echo $_POST['var'];
}
else {
echo 'Please ensure you have entered your details';
}
?>
How to check if post title already exists?
The condition is backwards, get_page_by_title( $post_title ) === null
means it doesn't exist, you just have to swap the then
and else
code blocks.
if ( get_page_by_title( $post_title ) === null ) {
$post_id = wp_insert_post( $post );
add_post_meta($post_id, 'portfolio-ids', $_POST['idsInput'], true);
} else {
echo "Already exists!";
}
PHP If Statement if (!$_POST) - What does !$_POST mean?
The !
(not) logical operator returns true
if the argument on its right-hand side is not true. It forces the argument to be evaluated as a boolean. In this case, when the $_POST
array is evaluated as a boolean it will evaluate as true
if it is not empty and false
if it is. (See converting to boolean.)
if (!$_POST) {
should be a safe way to detect whether or not anything is in $_POST
if you want to do that. empty
isn't necessary in that case because superglobals are always set, and since you aren't referring to a specific key, you don't need to worry about an undefined index notice.
I think it's also worth mentioning that if the only point of the check is to see what type of request was sent, it is probably better to just check the request method directly, because !$_POST
does not mean the request wasn't a post, since a post request can be empty.
Check exists post title
Try this code.
if( isset( $_POST['enable'] ) ) {
$url = 'api/url';
$result = file_get_contents( $url );
$resultData = json_decode( $result );
foreach ($resultData as $job) {
$post_title = sanitize_title( $job->JobTitle );
$post_id = post_exists( $post_title );
if( !$post_id ){
$data = array(
'post_type' => 'post',
'post_title' => $post_title,
'post_status' => 'publish',
'post_author' => $user_ID,
);
$post_id = wp_insert_post( $data );
}else{
//post title exist
}
}
}
Related Topics
PHP to Search Within Txt File and Echo the Whole Line
Error Message Strict Standards: Non-Static Method Should Not Be Called Statically in PHP
How to Remove Both .PHP and .Html Extensions from Url Using Nginx
How to Resize Pngs With Transparency in PHP
Utf-8 Bom Signature in PHP Files
How to Overwrite a Function in PHP
Incorrect Integer (2147483647) Is Inserted into MySQL
Filter Multidimensional Array Based on Partial Match of Search Value
How to Only Allow Certain Filetypes on Upload in PHP
What Is the Best Collation to Use For MySQL With PHP
How to Properly Escape HTML Form Input Default Values in PHP
Connecting to Ws-Security Protected Web Service With PHP
Find Highest Value in Multidimensional Array