问题
I am using a text editor on certain form fields, TinyMCE. it works ok.
However the TinyMCE editor returns a HTML body tag for each field in the form. This is perfectly fine if a user completes that form field, i.e;
'description' => string '<!DOCTYPE html>
<html>
<head>
</head>
<body>
a users response
</body>
</html>'
However, if a user does not complete the field TinyMCE STILL returns a string containing an empty html body, i.e the code below:
'description' => string '<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>'
What I want to do is to test if this returned 'form field' contains just empty HTML field, i.e no values within the body of the html field.
In summary how can I test that the above code just contains empty body tags?
回答1:
its not a very ellegant solution but it works. i simply remove the tags from the document using the strip_tag(), then test if the variable is empty. If its not empty i submit the post variable.
if (isset($_POST['description']))
{
$preValidated = dsf_db_prepare_input($_POST['description']);
$testValue = trim(strip_tags($preValidated));
if(!strlen($testValue) >= 1)
{
$description ='';
}
else
{
$description ="$preValidated";
}
}
回答2:
you can do it on the client side using jquery
if($('<html><body></body></html>').find('body').children().length < 1) {
console.log('empty')
} else {
console.log('not empty');
}
you can validate and send appropriate response to the server
来源:https://stackoverflow.com/questions/31512309/how-to-test-if-a-html-body-contains-empty-tags