Check textarea with php - php

I have a textarea field and want to add a text or php code when the field is empty and if the field is not empty to add another php code. For example:
if the field is empty>do something.....else>do something...
I'm not good at php, so I'll be glad for any help.
Thanks.
p.s.Sorry for my english.

Assuming you're posting this from a form then you can do the following.
I'll write this out the long way so it's easier to understand:
if (!empty($_POST['TEXTAREA_NAME']){
// If the textarea is set then do something here
} else {
// If it is NOT set it then it will do the code here
}

<?php echo isset($_POST['texarea_name']) && !empty($_POST['texarea_name']) ? 'textarea not empty' : 'texarea is empty'; ?>

To test if outside variable is empty or not, you can just compare it with empty string
if ( $_POST['text'] === '') {
echo 'there was something';
} else {
echo 'nothing to say';
}

Related

If text box content is empty, do

I got a problem with my code.
I want to figure out if there is a specific number in the text box and if it's empty it should say something like "There's nothing inside".
I did so but got a problem if the text box is empty.
If it's empty it skips the code for checking if it's empty, proceeds with the function after it.
That's what I got so far.
<?php
if(isset($_POST["submit"])){
$name = $_POST['winner'];
if(strpos($name,'123456789') !== false){
echo "<br><br>".$name." was the correct answer! Congratulations!";
}elseif($name !== ""){
echo "<br><br>You haven't typed in a number.";
}else{
echo "<br><br>".$name." wasn't correct. Better luck next time.";
}
}
?>
Anyone know what the error is?
This statement:
}elseif($name !== ""){
Should be:
}elseif ($name === ""){
Or:
}elseif (!strlen($name)){
I think you meant the opposite.
Should be elseif($name === "") not elseif($name !== "")
or also elseif(empty($name))
you want that if user leave text field empty it again ask for that ...
give me your email I'll mail you an example. ....

Case sensitive input value in textbox with validation form

I have script when I will input data from a textbox.
For example: In the textbox, we may input anything string excepting(admin, Admin, ADMIN, AdmiN). I am using this script, but i think my script is so long
Can you show me other ways, regex possibly, to do this?
<?php
$nama=$_POST['name'];
$pesan=$_POST['pesan'];
if(isset($_POST['submit'])){
if($nama== ("admin") || $nama==("Admin") || $nama==("AdmiN") || $nama=("ADMIN"))
{
echo "Masukkan nama yang lain";
}else{
$sql="insert into table1(nama,pesan)values('$nama','$pesan')";
$result_sql=mysql_query($sql);
header('Location:index.php');
}
}
?>
Convert the string to lower case:
if (strtolower($nama) === 'admin')
Try something like:
//make an array of your restricted words, all in lowercase
$restrictedWordsArr = array("admin"); //and more you want
//then check
if(in_array(strtolower($yourWordToCheck), $restrictedWordsArr)) {
//restricted word
}
else {
//valid
}
No need for regex, simply: strtolower($nama) == "admin"

use php to change a html elements inner text

I have a basic form, which i need to put some validation into, I have a span area and I want on pressing of the submit button, for a predefined message to show in that box if a field is empty.
Something like
if ($mytextfield = null) {
//My custom error text to appear in the spcificed #logggingerror field
}
I know i can do this with jquery (document.getElementbyId('#errorlogging').innerHTML = "Text Here"), but how can I do this with PHP?
Bit of a new thing for me with php, any help greatly appreciated :)
Thanks
You could do it it a couple of ways. You can create a $error variable. Make it so that the $error is always created (even if everything checks out OK) but it needs to be empty if there is no error, or else the value must be the error.
Do it like this:
<?php
if(isset($_POST['submit'])){
if(empty($_POST['somevar'])){
$error = "Somevar was empty!";
}
}
?>
<h2>FORM</h2>
<form method="post">
<input type="text" name="somevar" />
<?php
if(isset($error) && !empty($error)){
?>
<span class="error"><?= $error; ?></span>
<?php
}
?>
</form>
If you want change it dynamically in client-side, there is no way but ajax. PHP works at server-side and you have to use post/get requests.
Form fields sent to php in a $_REQUEST, $_GET or $_POST variables...
For validate the field param you may write like this:
if(strlen($_REQUEST['username']) < 6){
echo 'false';
}
else{
echo 'true';
}
You can't do anything client-side with PHP. You need Javascript for that. If you really need PHP (for instance to do a check to the database or something), you can use Javascript to do an Ajax call, and put the return value inside a div on the page.

php - check an empty string with === '' does not work

When I test to see if the textarea in my form is empty to do a redirect so it doesn't submit it in php, it doesn't work.
The textarea is named $_POST['message'], I know the variable exists because if I do this statement;
if (isset($_POST['message'])) {
header('Location:/');
exit();
}
Then it always redirects back to the index page so the variable must exist, although if I do this;
if (empty($_POST['message'])) {
header('Location:/');
exit();
}
It does not work, also tried with all three combos of =/==/===
if ($_POST['message'] === '') {
header('Location:/');
exit();
}
And also...
if (empty(trim($_POST['message']))) {
header('Location:/');
exit();
}
Any ideas why this is happening? And how I can prevent it, I really need to stop it as I do not want empty values in my mysql table.
I did some research and it seems some other people have had this problem, but I have seen no answer as of yet.
You probably have some whitespaces in the string, which isn't stripped by trim().
Do a strlen() on it to see what's up, and then log it byte by byte (http://stackoverflow.com/questions/591446/how-do-i-get-the-byte-values-of-a-string-in-php).
One thing you could think about is to make sure your textarea doesn't have any content in the markup (spaces, linkebreaks, whatever), like this:
<textarea></textarea>
I'm pretty sure your last try would work if you'd do it correctly, this:
if (empty(trim($_POST['message']))) {
// ...
}
...is a syntax error. empty is a language construct and does not accept expressions. Try:
$message = isset($_POST['message']) ? trim($_POST['message']) : '';
if (empty($message)) {
// $_POST['message'] is empty
}
This will ignore all input lengths below 3 chars:
if (isset($_POST['message']) && strlen(trim($_POST['message'])) < 3) {
header('Location:/');
exit();
}
However, if you just want to check, if a form was submitted, ask for the submit-button:
<input type="submit" name="submit" value="send" />
the php code would be
if (!array_key_exists('submit', $_POST)) {
header('Location:/');
exit();
}

How to check if the user has entered or not entered data to Mysql using PHP?

I'm trying to see if the user has entered a website URL into the database with the following code. If the user did not enter their website's URL, do not display anything. If the user did enter their website, display the website.
I think I'm doing it wrong.
<?php
if (!empty($url))
{
echo'';
} else {
echo'p>Website: <?php echo "http://","$url"; ?></p>';
}
?>
Apart from the wrong order (you need to remove ! before the empty()), you also have some errors in the echo part: the < is missing for the < p > tag and you are including php tags in your string.
It should read something like:
echo '<p>Website: http://' . $url . '</p>';
what you've got there is that you're checking if it's NOT empty (that is, there is some data), you're echoing an empty string. If it IS empty, then you're echoing it out.
Remove the ! in the first line and you should be right.
You're simple mismatching the meaning of "!" :
<?php
if (!empty($url))
{
echo'p>Website: <?php echo "http://","$url"; ?></p>';
} else {
echo'';
}
?>

Categories