Need some help with AJAX

Discussion in 'JavaScript' started by crazyryan, Aug 28, 2007.

  1. #1
    I came accross this:
    http://www.webtoolkit.info/ajax-file-upload.html

    But have no idea on how to implement it into this:
    
    <?php
    session_start();
    #########################################
    ##          PHP UPLOADER v2.0          ##
    #########################################
    
    // Change these variables:
    
    // Admin Username and Password.
    $admin_username = "admin";
    $admin_password = "admin";
    // Where your files will be uploaded to WITH trailing slash.
    $target_path = "uploads/";
    // The FULL URL to your uploads directory WITH training slash.
    $uploads_url = "http://www.phpmediascript.com/";
    ?>
    <html>
    <head>
    <title>Uploader</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    a, a:link, a:visited {
            color:                #FFF;
            text-decoration:        none;
    }
    a:hover {
            border-bottom:            1px dotted #FFF;
            cursor:                default;
    }
    body {
            background-color:         #333333;
            color:                #FFF;
            font-family:            Verdana, Arial, Helvetica, sans-serif;
            font-size:            10pt;
            cursor:                default;
    }
    h1 {
            margin-top:             0;
    }
    input {
            background-color:        #CCCCCC;
            font-size:             8pt;
            font-weight:             bold;
            color:                 #363636;
            border:             1px solid silver;
            padding:             0.2em;
            font-family:             Verdana, Arial, Helvetica, sans-serif;
    }
    input:hover {
            border:             1px solid #999;
    }
    input:focus {
            border:                1px solid #999;
            background-color:        #DDD;
    }
    p {
            margin:             0;
            margin-bottom:             1em;
    }
    div#contain {
            background-color:         #666666;
            border:             1px dotted #DDD;
            padding:             0em;
            margin:             1em;
    }
    div#main {
            margin:             1em;
    }
    </style>
    </head>
    <body>
    <?php
    if (!is_writable($target_path))
    {
        die("Your upload directory ($target_path) is <strong>not</strong> writeable - please CHMOD it to 0777.");
    }
    
    
        if ($_POST['1submit'])
        {
            $needed = $_POST['needed'];
            if ($needed > 1)
            {
                $f = "files";
            }
            else
            {
                $f = "file";
            }
            echo '<form name="form1" enctype="multipart/form-data" method="post">
    <h1>Find your ' . $f . '.</h1>
    <p>Find your ' . $f . ' and click submit!</p>';
            for ($x = 0; $x < $needed; $x++)
            {
                echo '
    <p><input name="uploadFile' . $x . '" type="file" id="file' . $x . '" /></p>';
            }
            echo '
    <p><label for="html">Show HTML Tags?</label> <input name="html" id="html" type="checkbox" value="html" /></p>
    <p><label for="bb">Show BBCODE?</label> <input name="bb" id="bb" type="checkbox" value="bb" /></p>
    <p><input name="needed" type="hidden" value="' . $needed . '" /></p>
    <p><input type="submit" name="upload" value="Upload" /></form></p>';
        }
        else
        {
            if ($_POST['upload'])
            {
                $needed = $_POST['needed'];
                if ($needed > 1)
                {
                    $f = "files";
                }
                else
                {
                    $f = "file";
                }
                $html = $_POST['html'];
                $bb = $_POST['bb'];
                echo "<h1>Uploading " . $needed . " " . $f . ".</h1>
    ";
                for ($x = 0; $x < $needed; $x++)
                {
                    $rand = rand(0, 999);
                    $file_name = $_FILES['uploadFile' . $x]['name'];
                    $file_name = stripslashes($file_name);
                    $file_name_only = str_replace("'", "", $file_name);
                    $file_name = "$rand$file_name_only";
                    $file_name = $target_path . $file_name;
                    $copy = copy($_FILES['uploadFile' . $x]['tmp_name'], $file_name);
                    if ($copy)
                    {
                        echo "<p><strong>" . $_FILES['uploadFile' . $x]['name'] .
                            "</strong> has been successfully uploaded. 
    The URL of this file is <strong><a href=\"" . $uploads_url . "$file_name\">" . $uploads_url .
                            "$file_name</a></strong>.<br />";
                        if ($bb == bb)
                        {
                            echo '
    BBCODE: <input type="text" size="60" value="[URL=' . $uploads_url . '' . $file_name .
                                '][IMG]' . $uploads_url . '' . $file_name . '[/IMG][/URL]" />';
                        }
                        if ($html == html)
                        {
                            echo '
    <br />
    HTML Tags: <input type="text" size="60" value=\'<a href="' . $uploads_url . '' .
                                $file_name . '"><img src="' . $uploads_url . '' . $file_name . '" /></a>\' />';
                        }
                        echo "</p>
    ";
                    }
                    else
                    {
                        echo "<p>" . $_FILES['uploadFile' . $x]['name'] . " could not be uploaded.</p>";
                    }
                }
            }
            else
            {
                echo '<form name="amount_of_files" method="post">
    <h1>Upload Files</h1>
    <p>How many files do you wish to upload?</p>
    <p><input name="needed" type="text" id="needed" /></p>
    <p><input name="1submit" type="submit" value="Submit" /></form></p>';
            }
        }
    
    
    ?> 
    </body>
    </html>
    PHP:
    Can anyone help me with it, I'd be willing to give a little cash payment as a token of my appreciation :D

    Also, I have a form asking how many files they want to upload, I wanted to AJAX that so when they enter 5, it automatically displays 5 forms without reloading the page.
     
    crazyryan, Aug 28, 2007 IP
  2. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #2
    Is that a working example or am I gonna have to add more code? Thanks
     
    crazyryan, Aug 28, 2007 IP