buckibooster Posted August 5, 2014 Share Posted August 5, 2014 I'm trying to validate a form at submittal. The code for my web page is written in php (i.e. server-side). The php file consists of a body file (i.e., body.php) included in a header file (i.e. tool.php). The included body file contains the html code for the body with form. The following php code is inserted at the top of the body file (i.e., before the html <body> and <form> fields: <?php //Check the validity of the data entered $activity01 = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { $activity01 = $_REQUEST[activity01]; include($_SERVER['DOCUMENT_ROOT'].'included_filesincPHP1.inc'); $cxn=mysqli_connect($host,$user,$password,$database) or die ("Couldn't connect to the server."); $result = mysqli_query($cxn, "SELECT counter, Activity_Task FROM jsaheader WHERE Activity_Task = '$activity01'"); $counter = mysqli_num_rows($result); $row = mysqli_fetch_assoc($result); if ($counter == 0) { mysqli_close($cxn); } else { mysqli_close($cxn);?> <SCRIPT type="text/javascript"> window.alert("The 'Activity / Task' that you have selected is not unique. There is already another JSA with the name '<? echo $activity01 ?>.' As each 'Activity / Task' must be unique, you will now be returned to the previous page so that you can select a unique 'Activity / Task.'");</SCRIPT> <? } } ?> The problem input field is a text field located within the included body file. The input field content looks like this: <INPUT type='text' size=75 id='activity01' name='activity01' value='<? echo $activity01;?>'> When the form is submitted, the php code shown above queries a SQL database to verify that the activity is unique. I've verified that this is actually working. If it is not unique, the user is informed of this through a popup alert and returned to the form. They actually never leave the form. Because of the code within the value attribute, the previously entered activity is supposed to be shown in the input field as it was originally entered. It doesn't but when I look at the source code it shows up there as "value='Test JSA 1'." Got any ideas why that is? The only thing I can think of is that this is happening because the form (and body) is contained within a php file included in the header file. Any help you can give will be very much appreciated. Link to comment Share on other sites More sharing options...
johncc Posted August 6, 2014 Share Posted August 6, 2014 If you want to use php for validation, you will need to load separate code as the embedded php will only be executed when the page loads. Link to comment Share on other sites More sharing options...
buckibooster Posted August 6, 2014 Author Share Posted August 6, 2014 Problem solved! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now