SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Nov 2003
    Location
    shropshire uk
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    javascript filename array from php/mysql

    Does anyone have some code which would do the following.

    I have a Content Management System written in PHP,MYSQL. In an effort to give records in the database a proper filename I have a field which allows the user to enter a filename. (ie http://www.naughtymutt.com/web-services/ecommerce.php isnt really a page, rather some cool apache configure/php/mysql)

    I need a way of putting all the filenames into an array which would then be checked once the form has been completed, so that there are no duplicate filenames in a section.

    so in a nutshell, i need to be able to build an array from mysql which I then can check the filename text field against when the form is submitted.

    Any help would be appreciated.

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The way, I see it you are asking: when a user submits an html form, how can I check the data entered in the form against names in my database. What does that have to do with javascript?

  3. #3
    SitePoint Member
    Join Date
    Nov 2003
    Location
    shropshire uk
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I want to check on the client side before the form is submitted. So a user puts some text in the filename box and when they click submit it checks against the array of filenames and prompts them to change it before it is submitted.

  4. #4
    SitePoint Member
    Join Date
    Jan 2005
    Location
    Hobart, Tasmania
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you were to do that you'd first have to use a mysql query to pull all of the values out of the database server side anyway, then put them in a javascript array client side, and do some comparisons. It doesn't really make sense, its a job for server side scripting.

    Client side all you can really do is figure out what values you expect re: length, illegal characters etc and do basic validation. Then if that passes and it gets to the server and finds its still not a kosher entry your PHP should be redirecting them to the error field in their form.

    Also JavaScript isn't enabled on about 10% of browsers so all you can really do is address the 90%, while the other 10% will pass through without the client side validation obviously. So its not like you don't have to do checking there anyway. Also, as long as your table doesn't accept duplicate entries then there's no beef. Send them back the form with red writing - filename taken, try again.

    Hope this has helped with perspective at least.

  5. #5
    SitePoint Member
    Join Date
    Nov 2003
    Location
    shropshire uk
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your opinions, but I know how I want this to work. I want the client to know BEFORE submitting that there is already a record with the same filename. yes it may not work 100% of the time, but it will be easier for a user to understand rather than thinking they have saved something only to find that it isnt.

    Forgetting what I want to do for a second....

    Is it possible to create an array in javascript and then check this array using a value from an text box on a form? how do i do that?

  6. #6
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Portsmouth UK
    Posts
    1,499
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have this which I was going to withdraw as its a bit naff but it may help

    http://homepage.ntlworld.com/vwphill...rayManager.htm

    I will do somthing for your specific requirement later


    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd"
    >

    <
    html>

    <
    head>
      <
    title></title>
    <
    script language="JavaScript" type="text/javascript">
    <!--

    var 
    MyArray=new Array();

    function 
    AddToArray(val){
    MyArray[MyArray.length]=val.value;

    HTML='MyArray = (';
     for (
    i=0;i<MyArray.length;i++){
      
    HTML+='\''+MyArray[i]+'\',';
     }
     
    HTML=HTML.substring(0,HTML.length-1)+');';
     
    document.getElementById('Display').innerHTML=HTML;
     
    val.value='';
     
    FindDuplicates();
    }

    function 
    FindDuplicates(){
     
    Duplicates='Dulicates<br>'
     
    for (i=0;i<MyArray.length;i++){
      
    lookfor=MyArray[i];
      for (
    i1=i;i1<MyArray.length-1;i1++){
       if (
    MyArray[i1+1]==lookfor){
        
    Duplicates+='field '+i+' = field '+(i1+1)+'= '+lookfor+'<br>';
       }
      }
     }
     if (
    Duplicates=='Dulicates<br>'){
      
    Duplicates='Dulicates<br>None'
     
    }

     
    document.getElementById('DDisplay').innerHTML=Duplicates;
    }

    //-->
    </script></head>

    <body>
    Enter a value to be entered in to Array 'MyArray'<input id="MyValue" onblur="javascript:AddToArray(this);" > then click outside the box

    <div id=Display ></div>
    <div id=DDisplay ></div>

    </body>

    </html> 
    the rest of what you want should be in the link above

    I am starting to take an interest in PHP so would like to know how you are going to put the PHP entries in the JS Array
    Last edited by vwphillips; Jan 15, 2005 at 06:26.

  7. #7
    SitePoint Member
    Join Date
    Nov 2003
    Location
    shropshire uk
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <script>

    function checkFilenames() {
    <?
    $sql="Select * from contentpages where section=$getSec";
    $result = mysql_query($sql) or die("Error - Could not connect to the database.");
    $file_name="";
    while ($row = mysql_fetch_array($result)) {
    $files_name=$row["filename"];

    $file_name.="\"$files_name\",";

    }
    ?>
    var famname = new Array(<?php echo rtrim($file_name,',');?>)

    for (i=0; i<famname.length; i++)
    {
    if (document.frmediting.filename_c.value==famname[i]) {
    alert("Filename Already Exists, choose another name")
    document.frmediting.filename_c.focus();
    return false;
    }
    }
    }

    // --></script>

    and then have this to start the form....

    <form method=POST action="add-content-page.php" onSubmit="return checkFilenames()" name="frmediting">


    its not the greatest solution, but it works for what I want to do.


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
  •