Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

CKEDITOR- How to check empty data after submission in PHP

I am using CKEDITOR text editor for description. If I leave it blank or enter some spaces without text, the result comes out like this:

<p>&nbsp; &nbsp; &nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p>

The html is:

<form action="<?= base_url('Blogs/submit_post'); ?>" method="post">
    <label>Description</label>
    <textarea id="des" name="descr"></textarea>
</form>

PHP script is (CI4)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$data = $this->request->getVar();
$data['descr'] = htmlentities($data['descr']);
print_r($data);

Now I need that if this textarea submitted without text or white spaces like – <p>&nbsp; &nbsp; &nbsp;</p>

then the value of $data['descr'] should null

else the value of $data['descr'] = htmlentities($data['descr'])

How can I check that submitted textarea value is empty?

>Solution :

If you want to check if the submitted textarea value contains only non-breaking spaces and HTML tags and treat it as empty, you can also use the strip_tags function in combination with str_replace to remove HTML tags and non-breaking spaces, and then check if the resulting string is empty.

$data = $this->request->getVar();
$cleanedDescr = str_replace('&nbsp;', ' ', strip_tags($data['descr']));

if (empty(trim($cleanedDescr))) {
    $data['descr'] = null;
} else {
    $data['descr'] = htmlentities($data['descr']);
}

print_r($data);

This approach should also work to handle cases where the value contains only non-breaking spaces and HTML tags, treating it as empty and setting $data['descr'] to null.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading