SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    masquerading Nick's Avatar
    Join Date
    Jun 2003
    Location
    East Coast
    Posts
    2,215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    adding text to a var

    I am trying to take the value of a variable and add text to the end of it, but it is not working. It is giving me "undefined,6" or whatever the file_id is.

    Code:
    <script language="javascript">
        if ( files == "" )
    	{
    	 files = '<?=$file_id?>';
    	}
    	else
    	{
    	 files += ',<?=$file_id?>';
    	}
        document.writeln(files);
       </script>
    This is ran everytime a user uploads a file. The file_id is set. It does not seem to be adding the new file_id to the end of the var files. Here is the whole source, if it helps:

    Code:
    <?php
     session_start();
     include("../config.php");
     include("../functions.php");
      // Make sure user is logged in
      checkUser();
    ?>
    
    <script language="javascript">
     var files;
    </script>
    
    <table width="94%" align="center" cellpadding="3" cellspacing="0">
     <tr>
      <td>
       <b>Upload Files</b> <br />
       <form action="upload.php?do=process" method="post" ENCTYPE="multipart/form-data">
        <input type="file" size="30" name="userfile" /> <br />
        <center><input type="submit" value="Upload File" /></center>
       </form>
      </td>
     </tr>
    </table>
    
    <?php
     $do = mysql_escape_string($_GET['do']);
     if($do=="process")
     {
      $userfile = $_FILES['userfile']['tmp_name'];
      $userfile_name = $_FILES['userfile']['name'];
      copy($userfile, '../uploads/'.$userfile_name.'');
      unlink($userfile);
       $dateline = date("U");
      $Query = "INSERT INTO uploads VALUES('0', '$userfile_name', '$dateline', '$_SESSION[user_id]')";
      $Result = mysql_query($Query)or die("Could not add file to the database: " . mysql_error());
       echo 'File has been uploaded successfully.';
      // Get the upload id of file
       $Query = "SELECT id FROM uploads WHERE name = '$userfile_name' AND dateline = '$dateline' AND user_id = '$_SESSION[user_id]'";
       $Result = mysql_query($Query)or die("Could not select uploaded file ID: " . mysql_error());
       while($Row = mysql_fetch_array($Result)) {
    	   $file_id = $Row['id'];
       }
    ?>
       <script language="javascript">
        if ( files == "" )
    	{
    	 files = '<?=$file_id?>';
    	}
    	else
    	{
    	 files += ',<?=$file_id?>';
    	}
        document.writeln(files);
       </script>
    <?
     }
    ?>
    Anyone see what I am doing wrong?
    Nick . all that we see or seem, is but a dream within a dream
    Show someone you care, send them a virtual flower.
    Good deals on men's watches

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Variables declared (var files;) without being assigned are automatically given the special JS undefined value.
    Code:
    var files = '';
    ::: certified wild guess :::

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    It's been too long since I did any php, but the problem with your document.writeln(files) statement is that you have to write valid html, and

    <?=$file_id?>

    is not valid html. The browser has no idea what that is, so it just skips it. If you run just your javascript and include this as the first statement:

    var files="";

    and remove the '<' and '>' from your if-then-else loop, you script will display:

    ?$file_id?

    Or, if you start with:

    var files="test";

    your script will display:

    test, ?$file_id?


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
  •