Can someone help me with a javascript?

Discussion in 'JavaScript' started by goknicks, Aug 14, 2012.

  1. #1
    My site has a javascript that creates an image. I would like to know what images are created on my site, by saving the file names on my server. I tried making the javascript create a hidden element and then saving the file through PHP, but it's not working. Can someone help me with a javascript that saves the new image's file name on the server?
     
    goknicks, Aug 14, 2012 IP
  2. veganin

    veganin Greenhorn

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #2
    It is not possible to achieve in Javascript. Javascript executes on the client site (in the browser) and so it does not have access to the server resources, i.e. file system. You will need to use some server side programming to achieve that (PHP, ASP.NET, etc)
     
    veganin, Aug 14, 2012 IP
  3. goknicks

    goknicks Greenhorn

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #3
    W/E, anything that can work with the javascript. The PHP that I thought would work isn't, so I need to figure out some other way.
     
    goknicks, Aug 14, 2012 IP
  4. Sitesupplier

    Sitesupplier Peon

    Messages:
    16
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    0
    #4
    You'll need to use some form of submission to achieve this. Are you looking to save the images themselves to the database, or just tag names/IDs, etc.?

    Either way, you can submit the correct value using an HTML form or you can use AJAX. Personally, I'd use AJAX because it allows for a smoother user experience, speeds up the time it takes to achieve the desired result and is probably less code in the end. An AJAX function along the lines of this should work:

    
    [COLOR=#003366][B]function[/B][/COLOR] do_something[COLOR=#009900]([/COLOR][COLOR=#009900])
    [/COLOR][COLOR=#009900]{[/COLOR]    
    [COLOR=#006600][I]    // Get the value of the "something".[/I][/COLOR]    
    [COLOR=#003366][B]    var[/B][/COLOR] something[COLOR=#339933]=[/COLOR] "something you want to send to PHP"[COLOR=#339933];[/COLOR]    
    [COLOR=#003366][B]    var[/B][/COLOR] xmlhttp [COLOR=#339933]=[/COLOR] [COLOR=#3366CC]""[/COLOR][COLOR=#339933];[/COLOR]    
    [COLOR=#006600][I]
        // IE7+, Firefox, Chrome, Opera, Safari.[/I][/COLOR]   
    [COLOR=#000066][B]    if[/B][/COLOR] [COLOR=#009900]([/COLOR]window.[COLOR=#660066]XMLHttpRequest[/COLOR][COLOR=#009900])[/COLOR]        
            xmlhttp [COLOR=#339933]=[/COLOR] [COLOR=#003366][B]new[/B][/COLOR] XMLHttpRequest[COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]    
    [COLOR=#006600][I]    // IE6, IE5.  [/I][/COLOR]    
    [COLOR=#000066][B]    else[/B][/COLOR]        
            xmlhttp [COLOR=#339933]=[/COLOR] [COLOR=#003366][B]new[/B][/COLOR] ActiveXObject[COLOR=#009900]([/COLOR][COLOR=#3366CC]"Microsoft.XMLHTTP"[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]     
    
        xmlhttp.[COLOR=#660066]onreadystatechange[/COLOR] [COLOR=#339933]=[/COLOR] [COLOR=#003366][B]function[/B][/COLOR][COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR]    
    [COLOR=#009900]    {[/COLOR]        
    [COLOR=#000066][B]        if[/B][/COLOR][COLOR=#009900]([/COLOR]xmlhttp.[COLOR=#660066]readyState[/COLOR] [COLOR=#339933]==[/COLOR] [COLOR=#CC0000]4[/COLOR] [COLOR=#339933]&&[/COLOR] xmlhttp.[COLOR=#000066]status[/COLOR] [COLOR=#339933]==[/COLOR] [COLOR=#CC0000]200[/COLOR][COLOR=#009900])[/COLOR]        
    [COLOR=#009900]        {[/COLOR]           
    [COLOR=#006600][I]            // Handle HTTP response here with "xmlhttp.responseText;".[/I][/COLOR]        
    [COLOR=#009900]        }[/COLOR]    
    [COLOR=#009900]    }[/COLOR]     
    [COLOR=#006600][I]
        // Note here a URI parameter exists (something). This will be passed to PHP.[/I][/COLOR]    
        xmlhttp.[COLOR=#000066]open[/COLOR][COLOR=#009900]([/COLOR][COLOR=#3366CC]"GET"[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#3366CC]"some_page.php?something="[/COLOR] [COLOR=#339933]+[/COLOR] something[COLOR=#339933],[/COLOR] [COLOR=#003366][B]true[/B][/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]    
        xmlhttp.[COLOR=#660066]send[/COLOR][COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];
    [/COLOR][COLOR=#009900]}
    [/COLOR]
    Code (markup):
    Now in your PHP file (some_page.php), you'll need to use the $_GET superglobal to retrieve the "something" parameter, whereupon you can store it in the database.

    I wrote a tutorial on how to check username availability with AJAX a while ago, all the code necessary to send HTML data to PHP via JavaScript is explained, but you'll have to work the code to your needs. If you're interested, check username availability with AJAX.
     
    Sitesupplier, Aug 15, 2012 IP
  5. goknicks

    goknicks Greenhorn

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #5
    I think this is going to where I want to go. Suppose I just want to save all the names in one file so that I can review them at a later date. How do I write to the file and then close the file?
     
    goknicks, Aug 15, 2012 IP
  6. kdb_bdk

    kdb_bdk Active Member

    Messages:
    163
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    55
    #6
    I need a solution for this myself
     
    kdb_bdk, Aug 21, 2012 IP
  7. infotripro

    infotripro Member

    Messages:
    26
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    43
    #7
    with jQuery is easier

    /* in your javascript */
    $.ajax({
    type:"POST",
    url: "/youractionscript.php",
    data: {picname: "your pic name"},
    async: false,
    success: function(data){
    return;
    }
    });


    /* in youractionscript.php */
    if(isset($_POST['picname'])){
    do whatever you want with $_POST['picname'];
    }
     
    infotripro, Aug 29, 2012 IP