I have a page to create a new user. I have a php script that does various checks on the data entered in text fields. When it finds non-conforming entries, I need to provide feedback to the user to state why the process has been terminated. My thought was to simply have an empty div that I would populate with a message specific to the failing condition.
However, it looks like this might be more difficult than I had thought. I'd prefer to to this in my php script. I'm pretty new to web development. I'm open to suggestions about the best / easiest way to accomplish this. Thanks.
<div id="page">
<img id="mainpic" src="images/banner.png" height="390" width="982">
<div id="stylized" class="leftsidebox"></div>
<div id="stylized" class="myform">
<form id="form" name="form" method="post" action="scripts/create_admin.php">
<label>Security Code
<span class="small">Enter the security code provided to you</span>
</label>
<input type="text" name="securitycode" id="name" />
<button type="submit">Create Administrator</button>
</form>
</div>
<div id="stylized" class="rightsidebox">
<div id="stylized" class="feedback">
<h1>this is where I want to add dynamic text from php</h1>
</div>
</div>
</div>
<?php
include('connection.php');
//retrieve input values from the form
$security_code = $_REQUEST['securitycode'];
if (strlen($security_code) == 0) {
//Add the text "Please enter a security code." to the div class="feedback"
die();
}
?>
You could put that script inside the div tag like this:
<div id="stylized" class="rightsidebox">
<div id="stylized" class="feedback">
<h1>
<?php
include('connection.php');
//retrieve input values from the form
$security_code = $_REQUEST['securitycode'];
if (strlen($security_code) == 0) {
echo "Please enter a security code.";
}
?>
</h1>
</div>
</div>
You need to reload the page. If you detect an error in your security code, just create a variable with your error message and display the page again. The page will then check on each display if the message is here. If it does then it displays it.
if (strlen($security_code) == 0) {
$message = "Security code was wrong";
require(page.php) //Call your view
}
Then in your page :
<div id="stylized" class="leftsidebox">
<?php if(isset($message)){ echo $message; } ?>
</div>
If you want to avoid reloading the page, I would advise looking into AJAX. Instead you would have an ajax request on submit of your form (with error in return if any) and your error message would be displayed asynchronously by manipulating the DOM with javascript.
Here you go add php code before html
<?php
include('connection.php');
//retrieve input values from the form
$security_code = $_REQUEST['securitycode'];
if (strlen($security_code) == 0) {
$error="Please enter a security code.";
//Add the text "Please enter a security code." to the div class="feedback"
die();
}
?>
<div id="stylized" class="feedback">
<h1><?php if(isset($error)){ echo $error; } ?></h1>
</div>