Current location: Hot Scripts Forums » General Web Coding » JavaScript » Hello, noobie question, but please help!


Hello, noobie question, but please help!

Reply
  #1 (permalink)  
Old 09-13-07, 07:17 AM
DeeDee DeeDee is offline
Newbie Coder
 
Join Date: Sep 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Hello, noobie question, but please help!

Ok, so I managed to create my own site now. The problem is, for example, if a customer buys something, he receives a tracking number.
Then he can insert the tracking number to see the status of his transaction.

What I want to do is the following. I want when he press track, even if he didn't enter nothing, or a wrong track number, to avoid the display of the 404 page not found. I would like to show, when generally wrong track number, a similar page like... error, the tracking number is wrong or something like this.

Do you think guys you can help? Because I create the tracking numbers with dreamweaver, and they are .html

Do I need a php script or something like this? Please shed some light please, I m totally lost and I don't like this with a blank page

What I have is this

Javascript Code:
  1. <script language="JavaScript">function PasswordLogin()
  2. { document.location.href = document.formlogin.trackingnr.value + ".html";
  3. return false;
  4. }
  5. function CheckEnter(event)
  6. {
  7. var NS4 = (document.layers) ? true : false;
  8. var code = 0;
  9. if (NS4)
  10. code = event.which;
  11. else
  12. code = event.keyCode;
  13. if (code==13)
  14. { PasswordLogin();
  15. event.returnValue = false;
  16. }
  17. }</script><br>
  18.  
  19.  
  20. ////then here some htlm and then/////
  21.  
  22.  
  23. <input name="trackingnr" id="trackingnr" onkeypress="CheckEnter(event)" size="29" type="text"> <input name="button" onclick="PasswordLogin()" value="Lookup" type="button">


So I would like, something like, if false, to return a page with a customize error or something....

It's not difficult, but I couldn't find what I want, because simply I don't know what is called....

So I am uploading the traking number myself, like FFFGS23.html
and if the customer types it wrong, or simply press submit without filling anything in the space, it comes the 404 Error. AND I DON"T want that to happen.

I detalied it, hope I can get some help tho...

Last edited by UnrealEd; 09-13-07 at 07:20 AM. Reason: please use the [highlight=Javascript] tag when posting Javascript code
Reply With Quote
  #2 (permalink)  
Old 09-13-07, 07:55 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
You could use AJAX to get the HTTP status code to verify if the file exists, and if so, redirect to it, or otherwise throw an error.
javascript Code:
  1. <script type="text/javascript">
  2.  
  3. function PasswordLogin()
  4. {
  5.     var filename = document.formlogin.trackingnr.value.toUpperCase() + ".html";
  6.     var http = window.XMLHttpRequest
  7.         ? new XMLHttpRequest()
  8.         : new ActiveXObject('MSXML2.XMLHTTP.3.0')
  9.  
  10.     if (!http)
  11.     {
  12.         window.location = filename;
  13.         return false;
  14.     }   
  15.  
  16.     http.open('GET', filename, true);
  17.     http.onreadystatechange = function()
  18.     {
  19.         if (http.readyState == 4)
  20.         {
  21.             if (http.status == 200)
  22.             {
  23.                 window.location = filename;
  24.             }
  25.             else
  26.             {
  27.                 alert('Invalid tracking number.');
  28.                 return false;
  29.             }
  30.         }
  31.     }
  32.     http.send(null);   
  33.     return false;
  34. }
  35.  
  36.  
  37. </script>
  38.  
  39. <form action="" method="get" name="formlogin" onsubmit="return PasswordLogin();">
  40.     <input name="trackingnr" id="trackingnr" size="29" type="text">
  41.     <input name="button" value="Lookup" type="submit">
  42. </form>


EDIT:

You could also use htaccess to create your custom 404 pages.

Last edited by Nico; 09-13-07 at 07:57 AM.
Reply With Quote
  #3 (permalink)  
Old 09-13-07, 08:03 AM
DeeDee DeeDee is offline
Newbie Coder
 
Join Date: Sep 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
I really love you! I will try this AJAX thing, though I am not sure if my website support it. I am reeeally noob but, if this fail I will try the alternative with customizing the 404 page.

I thank you a million!!! Thank You! I will post my feedback and rep you!!! I love you!

LE: ok I put the code you suggested but when I click submit, it doesn't do a thing? What can I do?

Edit: Ok, so when I press with a blank, it simply refresh and do nothing. This is good. But When I enter the good tracking no, it still refreshes and nothing more.

Last edited by DeeDee; 09-13-07 at 08:10 AM.
Reply With Quote
  #4 (permalink)  
Old 09-13-07, 08:07 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
AJAX is a browser based system which works on most browsers. It has nothing to do with your server or website.
Reply With Quote
  #5 (permalink)  
Old 09-13-07, 08:12 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Regarding your edits: Replace your HTML form with the one I gave you above. The script is tested and it works.
Reply With Quote
  #6 (permalink)  
Old 09-13-07, 08:21 AM
DeeDee DeeDee is offline
Newbie Coder
 
Join Date: Sep 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Ok, here's the problem. When I enter the GOOD tracking number now, it gives me the pop up Error. (which I edited for french). That's good.

But how do I make it to give this error when actually the track is wrong, or nothing is filled? I really appreciate your replies, you're the first one who help me, even tough I posted on several forums. Thank you again, I see there's no rep feature here, sorry.
Reply With Quote
  #7 (permalink)  
Old 09-13-07, 08:31 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
I added something to the code which may be causing the problem. I added a function that converts the tracking number to upper case (because I took your example above and thought it would be useful). If your files are lowercase or something, then remove this piece of code:
Code:
.toUpperCase()
Are you testing the script locally or on your actual web server? It does work for me. If the file doesn't exist (which would be the case if the field is empty) it throws the pop up, or otherwise it redirects me to the file.
Reply With Quote
  #8 (permalink)  
Old 09-13-07, 08:40 AM
DeeDee DeeDee is offline
Newbie Coder
 
Join Date: Sep 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Well, I have the files of my web in a folder on my pc, so I test them from there. Could that be the problem? Should I make the changes on the website directly? Because I thought it should work here as well.

And yes, the tracking is uppercase. So there is no error with that.

I told you what happen. When I left it blank or throw in a wrong tracking number, it just refreshes the page.

But when I enter the good number, it gives me the error. And I don't understand why.

You posted over the other forum a php code. Should I try that one? Should I create a new file called strack.php (my html file is strack.htm).

I am sorry for my noobiness I really want this work. Thank you. Alternatively, I could upload the files on the server to try that.
Reply With Quote
  #9 (permalink)  
Old 09-13-07, 08:45 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Quote:
Originally Posted by DeeDee View Post
Well, I have the files of my web in a folder on my pc, so I test them from there. Could that be the problem? Should I make the changes on the website directly?
Yes, test it on your server. Your PC won't return HTTP headers to the script which are needed.

You could try the PHP script as well if you want...
Reply With Quote
  #10 (permalink)  
Old 09-13-07, 08:51 AM
DeeDee DeeDee is offline
Newbie Coder
 
Join Date: Sep 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Hey it works! I uploaded them on the server and it works! I really am very very very grateful for everything you done.

Thank you thank you thank you!!!!!

Eres grande tio!!!!!!!!!! Un abrazo!!!!

Thread Closed!!!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about MySQL. drewhiggins Database 2 01-25-07 10:14 PM
Injecting a string into an If Statement ? nova912 PHP 4 07-21-06 02:04 PM
Posting a question / answer on site markcody PHP 2 11-23-04 01:58 PM
[PHP] Array question UmiSal Script Requests 1 04-05-04 01:52 PM
question and answer software jaydifox C/C++ 0 02-21-04 09:26 AM


All times are GMT -5. The time now is 11:31 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.