Page 1 of 4 123 ... LastLast
Results 1 to 10 of 32

Thread: Error Redirection Question

  1. #1
    Join Date
    Jan 2012
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Error Redirection Question

    I am using this script which redirects a user to a page a 5-digit number is entered into a form box. The script works fine when the target page's filename exists on my site. If the file does not exist, the 404 error page is returned.

    My question is, how do I automatically redirect the user to the previous page, or the starting page instead of the 404 error page if the input entered is anything other than a 5-digit number?

    Thanks in advance.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Test the value of (validate) the form box before sending them off to the page. Branch from there.

    If you want more help, please include a link to the page on your site that contains the problematic code or include the script in a post so we can check it out.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jan 2012
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Question About Error Redirection

    I am using this simple script which directs a user to an html page when he enters a 5-digit number into a form box. The script works fine when the target page's filename exists on my site. If the file does not exist, the 404 error page is returned.

    My question is, how do I automatically redirect the user to the previous page, or the starting page instead of the 404 error page if the input entered is anything other than a 5-digit number?

    Thanks in advance.
    Code:
    The script:-
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"  "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="language" content="english"> 
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <title></title>
    <style type="text/css"></style>
    
    <script type="text/javascript">
    function init(){
       df=document.forms[0];
       df[0].focus();
       df[1].onclick=function() {
    if(df[0].value=='') {
       df[0].focus();
       return; }
       modifiedUrl='http://www.example.com/?=event_action=yaddayaddayadda&amp;desiredname='+df[0].value;
       alert(modifiedUrl);
       location.href=modifiedUrl; 
      } }
    if(window.addEventListener){
       window.addEventListener('load',init,false);
     }
    else { 
    if(window.attachEvent){
       window.attachEvent('onload',init);
      } }
    </script>
    
    </head>
    <body>
    
    <form action="#">
    <div>
     <input type="text">
     <input type="button" value="add text to a url">
    </div>
    </form>

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Are you sure you have the 90000 or whatever it is 5 digit number pages (00000 to 99999) required for this? If so:

    Code:
    <script type="text/javascript">
    function init(){
       var df=document.forms[0];
       df[0].focus();
       df[1].onclick=function() {
    if(!/^\d{5}$/.test(df[0].value)) { // requires a 5 digit number, otherwise:
       history.go(-1); // returns to previous page (if any)
       df[0].focus(); // if no previous page, focus on the text input again
       return; }
       // if it was a 5 digit number:
       modifiedUrl='http://www.example.com/?=event_action=yaddayaddayadda&amp;desiredname='+df[0].value;
       alert(modifiedUrl);
       location.href=modifiedUrl; 
      } }
    if(window.addEventListener){
       window.addEventListener('load',init,false);
     }
    else { 
    if(window.attachEvent){
       window.attachEvent('onload',init);
      } }
    </script>
    If on the other hand you only want to allow a range, or certain numbers, that can be arranged. The above will allow any 5 digit number. If there aren't all that many pages, a select element that lists the only valid choices is best. Even with - say 90000 choices, you could still have a select. Instead of writing out each possible number, you could have javascript or server side code do that for you. With a select, no validation is required, because only the acceptable choices are available.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jan 2012
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    First of all John, thank you for you helpfulness.

    I am working on a naming system for my pages, which will always be 5-digit (eg 14001.html). It is meant to be an identificatioin number for the visitor to call up his page according to the identification number I allocate. viz. 14001-14999 (1000 file names reserved for year 2014), 15001-15999 (For 2015), and so on to the future, but probably much less files will be used in practice and not necessarily in numeric sequence. There is no requirement for strict and secure exclusion, just a simple redirection back to the previous form page for re-entry of the correct identification will do.

    What is your recommendation? Thank you again for your kind help.

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    First of all, in the code you provide:

    Code:
    modifiedUrl='http://www.example.com/?=event_action=yaddayaddayadda&amp;desiredname='+df[0].value;
    As long as that page exists for one desiredname, that page will always exist for any or all and even for no desiredname. So if that's how your setting this up, you will never have a 404, so you will need some other way to determine if the desiredname field is valid. If you are instead doing something more like so:

    Code:
    modifiedUrl=df[0].value + '.htm';
    Then any resulting url will either exist or will not and you can redirect on the basis of whether it's there or not.

    Doing it like that, if you have a server side language like PHP, a simple PHP script on an intervening page could be made to do that (check if the page is there, go there if it is, return to the form if not). Otherwise you could put the allowed numbers in a javascript on an intermediate page, or even on the same page, and have it weed them out.

    Just to be sure, when you say " just a simple redirection back to the previous form page for re-entry of the correct identification will do." what page is that? Is that the page in your previous post, the one with the input for the number on it? Or is it some other page?

    If it is the page with the number input on it, you shouldn't have to leave that page to know if it's a valid number, but you could. It would be a little more complicated, but probably best to remain on that page when an invalid entry is made.

    Another thing of interest here is weather or not all of these pages are on the same domain. If so, that would make determining their existence easier.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Jan 2012
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    ... Just to be sure, when you say " just a simple redirection back to the previous form page for re-entry of the correct identification will do." what page is that? Is that the page in your previous post, the one with the input for the number on it? Or is it some other page?

    If it is the page with the number input on it, you shouldn't have to leave that page to know if it's a valid number, but you could. It would be a little more complicated, but probably best to remain on that page when an invalid entry is made.

    Another thing of interest here is weather or not all of these pages are on the same domain. If so, that would make determining their existence easier.
    Yes, I would like the same page to reappear as long as a non-existent input is entered. All the valid pages are on the same domain.

    Presently, when a non-existent input is entered, an error page ("Page Not Found") appears. Then the user will need to click to go back.
    Will it be possible to make that same input page always appear by default as long as the entered input does not point to a page that exists?

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I think the only way to do that would be to have a custom server side 404 not found file for that folder that includes a slightly delayed redirect to the form page. I'm not sure how that would be done, but it could probably be worked out. But assuming the modifiedUrl is the actual page you are going to and not a script (you didn't really answer that question but seem to imply that it is a page), you could rather easily remain on the form page unless an existing page's number were entered into it:

    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="language" content="english"> 
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <title></title>
    <style type="text/css"></style>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
    
    <script type="text/javascript">
    function init(){
       var df=document.forms[0];
       df[0].focus();
       df[1].onclick=function() {
    if(!/^\d{5}$/.test(df[0].value)) { // requires a 5 digit number, otherwise:
       alert('Please Enter Only the Correct 5 Digit Number');
       df[0].focus(); // focus on the text input again
       return; }
       // if it was a 5 digit number:
       var modifiedUrl=df[0].value + '.htm';
       $.ajax({
       	url: modifiedUrl,
       	cache: false,
       	success: function(){ // if the page exists:
       		//alert(modifiedUrl); // this alert is optional, remove it and if the right number is entered the person will go directly to the page
       		location.href=modifiedUrl; // go to it
       	},
       	error: function(e){ // if it's a 404 or other problem
       		if(e.status === 404){
       			alert("Page not found. Please make sure you've entered the correct 5 digit number.");
       		} else {
       			alert("That page is currently unavailable. Please make sure you've entered the correct 5 digit number or try again later.");
       		}
       		df[0].focus(); // focus on the text input again
       	}
       });
      } }
    if(window.addEventListener){
       window.addEventListener('load',init,false);
     }
    else { 
    if(window.attachEvent){
       window.attachEvent('onload',init);
      } }
    </script>
    
    </head>
    <body>
    
    <form action="#">
    <div>
     <input type="text">
     <input type="button" value="add text to a url">
    </div>
    </form>
    If you want more help, please include a link to the page on your site that contains the problematic code so we can check it out.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Jan 2012
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I'll be working around what you have provided.
    Thank you John, for your help. Much appreciated.

  10. #10
    Join Date
    Jan 2012
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi John,

    I have tried your suggested code in your last post and find that it functions well when I specify a sub-directory for the 'value' field viz:-
    var modifiedUrl='subdirectory/'+df[0].value + '.html'
    Using this line, the requested file was found and displayed.

    Then I tried removing the "sub-directory/" portion hoping that the file will be searched for in all subdirectories, but it didn't work, and an error pop-up showed.
    Will it be possible to make it work that way, namely for the script to search for the file in all subdirectories? (I have many subdirectories to group the files to be searched).

    Further to that, I have another related question which I will post in a subsequent thread. Thanks for your help.
    Sorry for not including the link, as the page is still not established.

Similar Threads

  1. Question about an error message on contact form
    By windbrand in forum JavaScript
    Replies: 1
    Last Post: 01-05-2012, 05:42 PM
  2. Redirection
    By Lumpy500 in forum PHP
    Replies: 3
    Last Post: 03-29-2009, 09:49 PM
  3. Error SQL: newbie question.
    By Orange in forum PHP
    Replies: 7
    Last Post: 10-29-2008, 06:11 PM
  4. ? Redirection
    By scottjcampbell in forum Looking for such a script or service
    Replies: 2
    Last Post: 11-17-2007, 05:19 PM
  5. Replies: 1
    Last Post: 05-09-2007, 04:11 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •