PDA

View Full Version : Redirect script with error checking



ju33
Mar 16th, 2007, 08:21 PM
I need to create a script to do the following:

A) Put a box on the screen to allow a user to enter a word or phrase.

B) Error check to only allow a-z, A-Z, 0-9 and spaces. If error ask for new entry and recheck. Max length of 40 characters. Convert spaces to -

C) Forward to the page name blah blah blah.htm

IE enter chicken and be directed to www.site.com/chicken.htm

IE enter blah blah blah and be directed to www.site.com/blah-blah-blah.htm

If page doesn't exist then I have a 404 redirect in place.

I'm guessing it wont be rocket science to do this, but I don't have a clue where to start.

ju33
Mar 16th, 2007, 09:47 PM
Page 1:

caller answer.htm




<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.style9 {
color: #FFFFFF;
font-family: Verdana, Arial, Helvetica, sans-serif;
}
.style10 {
font-size: 16;
color: #FFFFFF;
}
.style11 {color: #FFFFFF; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 16; }
.style12 {
font-size: 36px;
color: #FFFFFF;
}
body,td,th {
color: #FFFFFF;
}
body {
background-color: #000000;
}
.style13 {color: #FFFF00}
.style17 {
color: #000000;
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 36px;
}
.style20 {color: #000099; font-family: Georgia, "Times New Roman", Times, serif; font-size: 18px; }
.style21 {color: #000000}
.style22 {font-size: 18px; font-family: Georgia, "Times New Roman", Times, serif;}
.style23 {color: #000000; font-family: Georgia, "Times New Roman", Times, serif; font-size: 18px; }
.style24 {
color: #0000FF;
font-size: 45px;
}
.style25 {font-size: 16}
.style26 {
color: #0000FF;
font-weight: bold;
}
.style29 {
color: #000000;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 16;
font-weight: bold;
}
.style30 {
color: #000000;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-weight: bold;
}
.style31 {color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif; }
.style32 {color: #FFFF00; font-family: Verdana, Arial, Helvetica, sans-serif; }
.style35 {color: #0000FF; font-family: Verdana, Arial, Helvetica, sans-serif; }
.style36 {color: #FF0000}
-->
</style>
</head>

<body>


<?php

function clean($name, $max) {
$name = ereg_replace("[[:space:]]", '-', $name);
$name = ereg_replace('&', '&amp;', $name);
$name = ereg_replace('<', '&lt;', $name);
$name = ereg_replace('>', '&gt;', $name);
$name = ereg_replace(' ', '-', $name);
$name = ereg_replace(' ', '-', $name);
$name = ereg_replace('[^A-Za-z0-9,-,_]', '-', $name);
$name = ereg_replace('^-', '', $name);
$name = ereg_replace('^_', '', $name);
$name = ereg_replace('-$', '', $name);
$name = ereg_replace('_$', '', $name);
$name = substr($name, 0, $max);
return $name;
}

function clean1($name, $max) {

# $name = ereg_replace('[^A-Za-z0-9,-,_]', '-', $name);
$name = ereg_replace('^-', '', $name);
$name = ereg_replace('^_', '', $name);
$name = ereg_replace('-$', '', $name);
$name = ereg_replace('_$', '', $name);
$name = substr($name, 0, $max);
return $name;
}
?>

<div align="center">
<p class="style9 style10">&nbsp;</p>
<p><script type='text/javascript'>

function formValidator(){
var firstname = document.getElementById('name');


if(isAlphabet(firstname, "Please enter only letters")){
if(lengthRestriction(firstname, 1, 40)){
return true;
}
}
return false;


}
function isEmpty(elem, helperMsg){
if(elem.value.length == 0){
alert(helperMsg);
elem.focus(); // set the focus to this input
return true;
}
return false;
}

function isNumeric(elem, helperMsg){
var numericExpression = /^[0-9]+$/;
if(elem.value.match(numericExpression)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function isAlphabet(elem, helperMsg){
var alphaExp = /^[a-z\sA-Z0-9\-_]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function isAlphanumeric(elem, helperMsg){
var alphaExp = /^[0-9a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function lengthRestriction(elem, min, max){
var uInput = elem.value;
if(uInput.length >= min && uInput.length <= max){
return true;
}else{
alert("Please enter between " +min+ " and " +max+ " characters");
elem.focus();
return false;
}
}




</script>

<form onsubmit='return formValidator()' action='checkanswer.php' method='POST' >

Enter Answer:
<input type='text' id='name' name='name'/><br />
<input type='submit' value='Check Answer' />

</form>
<p class="style9">&nbsp;</p>
</div>
</body>
</html>






Page 2 - checkanswer.php




<html>

<head>
<title>Checking Your Answer</title>
<noscript>
<meta http-equiv="refresh" content="2; URL=redirectiontarget.htm">
</noscript>
<script language="JavaScript">
<!--
var sTargetURL = "<?php echo clean($_POST['name'], 40); ?>.htm";

function doRedirect()
{
setTimeout( "window.location.href = sTargetURL", 2*1000 );
}

//-->
</script>

<script language="JavaScript1.1">
<!--
function doRedirect()
{
window.location.replace( sTargetURL );
}

doRedirect();

//-->
</script>

</head>

<body onLoad="doRedirect()">

Checking <a href="<?php echo clean($_POST['name'], 40); ?>.htm"><?php echo clean($_POST['name'], 40); ?></a></p>
<p>&nbsp;</p>

</body>

</html>





This works if I enter one word answers and redirects ok. If I enter "1 2 3" it doesn't redirect to 1-2-3.php

Testing is on http://www.carbondioxide.name/answer.htm

(ignore current 404 page.)