Forum Moderators: coopster

Message Too Old, No Replies

Alert box

Displaying alert box with PHP

         

Adam5000

4:01 am on Sep 23, 2010 (gmt 0)

10+ Year Member



Greetings all

I'm working on a registration form and I've got it done in Javascript. But since I'm going to be using it to input data to a MySql database, I think it's a better idea to write it in PHP. I like the alert boxes to tell users things like "Alert. At least one of the fields is empty." or "Alert. Passwords don't match." and things like that.

Below is part of the form in Javascript.

Is there a way to generate an alert box with PHP?

<html>

<head>
<title>Alert box</title>
</head>

<body>
<script type="text/javascript">

function check_fields()
{
var usr_name = document.getElementById('u_name')
var passwrd = document.getElementById('pass')
var reenter_passwrd = document.getElementById('reenter_pass')

if (usr_name.value.length==0 || passwrd.value.length==0 || reenter_passwrd.value.length==0)
{
alert("At least one field is empty.")
}
}

</script>

<form>

<input type="text" id="u_name">
<br>
<input type="password" id="pass">
<br>
<input type="password" id="reenter_pass">
<br>
<input type="Submit" value= "Submit" onClick="check_fields()"

</form>

</body>
</html>

Matthew1980

7:52 am on Sep 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there adam5000,

What you are describing is just standard error checking, after $_POST data checking, run the key's /value's through filters to check for the things as you are after, and if the filters (if clauses) are used, you can just populate an array, then after all the filtering has been completed, check to see if the array is empty() if it's not, build the contents into an error string; echo to screen.

if it is empty() proceed with the processing of the data and do what you gotta do with it..

Hopefully your not too confused by my description!

//error string
$ReturnFaults = "";
$errorArray = array();

//strip unwanted data & whitspace
$_POST = array_map('strip_tags', $_POST);
$_POST = array_map('trim', $_POST);

if($_POST['name'] == ""){
$errorArray[] = "Name field is empty";
}


if($_POST['email'] == ""){
$errorArray[] = "Email field is empty";
}


if($_POST['hobby'] == ""){
$errorArray[] = "Hobby field is empty";
}

if(!empty($errorArray)){
$ReturnFaults = "<ul>";
foreach($errorArray AS $faults=>$faultsValue){
$ReturnFaults = "<li style=\"font-weight: bold;\">".$faultsValue."</li>";
}
$ReturnedFaults = "</ul>";
}

if(!empty($ReturnFaults)){
echo $ReturnFaults;
}


Psuedo code and typed OTF, but you get the idea from that, and of course SANITISE any $_POST or $_GET data before letting it into your code!

Cheers,
MRb