Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Feb 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    File Checker HELP

    I have a script that generates a filename from form elements.
    I need to add some error correcting to it.

    The script concatonates (spelled right?) using 3 variables from the form and 2 strings of text to form a path. I need to check the results of that against a folder to make sure the filename created actually exists before directing the browser to display the file. If the file does not exist I want a pop-up message to appear saying so and refreshing the page so the user can re-enter the request.

    Here is my original script:

    ---------BEGIN CODE HERE--------------
    <script language="javascript">
    function buildLink()
    {
    var pdfLink
    pdfLink = '../menus/'
    pdfLink += document.form1.mnuMeal.value;
    pdfLink += document.form1.mnuDay.value;
    pdfLink += document.form1.mnuMonth.value;
    pdfLink += '.pdf',

    document.form1.action = pdfLink;
    document.form1.submit();
    }
    </script>

    <form name=form1>
    <select name=mnuMeal>
    <option value="Bre">Breakfast</option>
    <option value="Lun">Lunch</option>
    </select>
    <select name=mnuDay>
    <option value="Wek">Weekday</option>
    <option value="End">Weekend</option>
    </select>
    <select name=mnuMonth>
    <option value="Jan">Jan</option>
    <option value="Feb">Feb</option>
    <option value="Mar">Mar</option>
    ...
    </select>
    <input type=button value="View Menu" onclick="buildLink();">
    </form>
    ---------END CODE HERE--------------

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    You have to send request to the server to check if the file exists unless you have already loaded the list to javascript. But you can also do it purely client-side by using the XMLHTTP object.
    Since you only need to check if the file exists, just change the line in that script:

    return oxmlhttp.responseText;

    to:

    return (oxmlhttp.status==200) ? true:false;

    which means the function returns true if the file exists and false otherwise.

    And I noticed that you're accessing the value of the comboboxes using comboboxRef.value. Only few browsers support that. This is the cross-browser way:

    var pdfLink = '../menus/';
    var f = document.form1;
    pdfLink += f.mnuMeal.options[f.mnuMeal.selectedIndex].value;
    pdfLink += f.mnuDay.options[f.mnuDay.selectedIndex].value;
    pdfLink += f.mnuMonth.options[f.mnuMonth.selectedIndex].value;

    or better, pass the form reference to the function

    function buildLink(f)
    {
    var pdfLink = '../menus/';
    pdfLink += f.mnuMeal.options[f.mnuMeal.selectedIndex].value;
    pdfLink += f.mnuDay.options[f.mnuDay.selectedIndex].value;
    pdfLink += f.mnuMonth.options[f.mnuMonth.selectedIndex].value;
    ...
    }
    ...
    <input type="button" value="View Menu" onclick="buildLink(this.form);">

  • #3
    New to the CF scene
    Join Date
    Feb 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Glenn,

    Please forgive my ignorance.
    How does the page reference the XMLHTTP object? I've never used that before. (Just now learning XML)

    I like the function idea. I'll make that change in the morning.

    I'm real new to javascript and need a little more clarity.

    Thank you very much for your time.
    Ron.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    You don't have to know XML as the name implies. Actually, you can access any type of file (including pdf files). Just use the function from the link I posted and modify the last line as I previously said.

  • #5
    New to the CF scene
    Join Date
    Feb 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Glenn,

    OK. I have the function on the page. How do I use the result?

    I can now check to see if the file exists. I need to display a message if the result is false.

    Thank you for your patience.

    Ron.

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Code:
    function buildLink(f)
    {
      var pdfLink = '../menus/';
      pdfLink += f.mnuMeal.options[f.mnuMeal.selectedIndex].value;
      pdfLink += f.mnuDay.options[f.mnuDay.selectedIndex].value;
      pdfLink += f.mnuMonth.options[f.mnuMonth.selectedIndex].value;
      var exists = getFile(pdfLink);
      if (exists){
        location.href=pdfLink;
      }
      else alert('File "' + pdfLink + '" does not exist.');
    }
    ...
    <input type="button" value="View Menu" onclick="buildLink(this.form);">

  • #7
    New to the CF scene
    Join Date
    Feb 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Glenn,
    Here is my final text. I keep getting a false on the getFile function.
    Any ideas?
    PS: the filename and path are correct.

    ---- SCRIPT ----
    <script language="javascript">
    function buildLink(f)
    {
    var pdfLink = '../menus/';
    pdfLink += f.mnuLevel.options[f.mnuLevel.selectedIndex].value;
    pdfLink += f.mnuMeal.options[f.mnuMeal.selectedIndex].value;
    pdfLink += f.mnuMonth.options[f.mnuMonth.selectedIndex].value;
    pdfLink += '.pdf';
    var exists = getFile(pdfLink);
    if (exists){
    location.href=pdfLink;
    }
    else alert('Menu not available yet, please try again later."' + pdfLink);
    }

    function getFile(filename)
    { oxmlhttp = null;
    try
    { oxmlhttp = new XMLHttpRequest();
    oxmlhttp.overrideMimeType("text/xml");
    }
    catch(e)
    { try
    { oxmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    { return null;
    }
    }
    if(!oxmlhttp) return null;
    try
    { oxmlhttp.open("GET",filename,false);
    oxmlhttp.send(null);
    }
    catch(e)
    { return null;
    }
    return (oxmlhttp.status==200) ? true:false;
    }

    </script>

    ---- FORM SUBMIT LINE ----

    <input type="button" value="View Menu" onclick="buildLink(this.form);">
    Last edited by Capt_Ron; 02-05-2004 at 08:35 PM.

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    try putting absolute url

    var pdfLink = 'http://mydomain.com/menus/';

    Also try inserting alerts at some points in the function to verify values.

    Can you tell me what does oxmlhttp.status return?


  •  

    Posting Permissions

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