Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
48
rated 0 times [  51] [ 3]  / answers: 1 / hits: 29411  / 13 Years ago, sun, september 25, 2011, 12:00:00

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


More From » php

 Answers
9

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>

[#89934] Friday, September 23, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daphnew

Total Points: 716
Total Questions: 113
Total Answers: 113

Location: Bonaire
Member since Sat, May 1, 2021
3 Years ago
;