redrabbit Posted May 3, 2010 Share Posted May 3, 2010 Ok this is the code below for validating the first name and last name of someone who enters data into the form, lets say they are both required, so the user needs to be informed when he leaves one blank, how do I echo the error message to the user in the HTML next to the input field saying firstname or lastname was left blank? if (empty($fname)) { echo 'Please enter your firstname'; $error = true; $output_form = true; } if (empty($lname)) { echo 'Please enter your surname'; $error = true; $output_form = true; } The html part of the form is: <tr> <th>First Name:</th> <td> <input type="text" name="firstname" class="medium" value= "<?php if (!empty($fname)) echo $fname; ?>" /> </td> </tr> <tr> <th>Surname:</th> <td> <input type="text" name="surname" class="large" value="<?php if (!empty($lname)) echo $lname; ?>" /> </td> </tr> Link to comment https://forums.phpfreaks.com/topic/200529-how-to-echo-an-error-message-next-to-a-form-field/ Share on other sites More sharing options...
TeddyKiller Posted May 3, 2010 Share Posted May 3, 2010 I usually would do this... if (empty($fname)) { $errors['firstname'] = 'Please enter your firstname'; $error = true; $output_form = true; } if (empty($lname)) { $errors['surname'] = 'Please enter your surname'; $error = true; $output_form = true; } <tr> <th>First Name: <?php $errors['firstname'] ?></th> <td> <input type="text" name="firstname" class="medium" value= "<?php if (!empty($fname)) echo $fname; ?>" /> </td> </tr> <tr> <th>Surname: <?php $errors['surname'] ?></th> <td> <input type="text" name="surname" class="large" value="<?php if (!empty($lname)) echo $lname; ?>" /> </td> </tr> Or alternatively, is putting it on the end of the field itself. ex: <input type="text" name="surname" class="large" value="<?php if (!empty($lname)) echo $lname; ?>" /><?php $errors['surname'] ?> Although the if !empty part, you don't really need, because if its empty.. nothing will get outputted in. It'll issue a warning, but with errors turned off you won't see it. Link to comment https://forums.phpfreaks.com/topic/200529-how-to-echo-an-error-message-next-to-a-form-field/#findComment-1052256 Share on other sites More sharing options...
redrabbit Posted May 3, 2010 Author Share Posted May 3, 2010 Thank you soooo much TeddyKiller I actually did it that way but I was using numbers instead of words for the array, it makes things very confusing Plus thanks for showing me how to display the error_array in the html form. Link to comment https://forums.phpfreaks.com/topic/200529-how-to-echo-an-error-message-next-to-a-form-field/#findComment-1052266 Share on other sites More sharing options...
TeddyKiller Posted May 3, 2010 Share Posted May 3, 2010 You mean $error[1] It'll work that way.. for ex: $errors[0] = 'Oh.. ERORRRRR!'; echo $errors[0]; Link to comment https://forums.phpfreaks.com/topic/200529-how-to-echo-an-error-message-next-to-a-form-field/#findComment-1052269 Share on other sites More sharing options...
redrabbit Posted May 3, 2010 Author Share Posted May 3, 2010 Yes I was using that way: my main obstacle here was not knowing the syntax to display the message in the html part You have clearly highlighted that. Once again thank you so much it was very much appreciated Link to comment https://forums.phpfreaks.com/topic/200529-how-to-echo-an-error-message-next-to-a-form-field/#findComment-1052277 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.