2013-09-21 3 views
0

나는 자바 스크립트 document.write를()

echo ' 
     <html lang="en-US"> 
     <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
     <title>Story Submission Page</title> 
     <link rel="stylesheet" type="text/css" href="petercort.css" /> 
     <script type="text/javascript" src="javascript.js"> 
     </script> 
     </head> 
     '; 

    echo ' 
     <div id="submissionform"> 
     <p><u><strong>Post Submission</strong></u></p> 
     <form action="upload_file.php" method="post" enctype="multipart/form-data"> 

     <label for="title">Title: </label> 
     <input type="text" name="title" id="title"><br> 
     <br> 
     <label for="summary">Summary: [TXT FILE]</label> 
     <input type="file" name="summary" id="summary"><br> 
     <br> 
     <label for="story">Story: [TXT FILE]</label> 
     <input type="file" name="story" id="story"><br> 
     <br> 
     <label for="previewpicture">Preview Picture: [PNG FILE]</label> 
     <input type="file" name="previewpicture" id="previewpicture"><br> 
     <br> 
     <label for="contentpicture">Content Pic: [PNG File]</label> 
     <input type="file" name="contentpicture" id="contentpicture"><br> 

     <button onclick="addPictureLine()">Add Another Picture</button> 

     <br> 
     <input type="submit" name="submit" value="submit"><input type="reset" name="reset" value="reset"> 

     </form> 

     </div> 

     </body> 
     </html> 
    '; 
    } 
} 
else { 
    header('Location: adminpanel.html'); 
} 

PHP

에 의해 생성 된 것 페이지 및 작동 관련 자바 스크립트 코드

function addPictureLine() { 
    document.write("<label for='contentpicture'>Content Pic: [PNG File]</label> <input type='file' name='contentpicture' id='contentpicture'><br>") 
} 

하지만,이 난에있는 버튼을 클릭하면 사용자가 다른 그림을 제출물에 추가 할 다른 줄을 추가하고 그 안에 document.write() 정보가있는 빈 페이지로 나를 보냅니다. 여러 페이지를 업로드 할 수 있도록 기존 페이지에 다른 라인을 추가하고 싶습니다.

답변

3

웹 페이지에 이미 콘텐츠가 포함되어있는 경우 document.write은 해당 콘텐츠를 덮어 씁니다.

innerHTML 속성을 사용하여 DOM에 텍스트를 삽입해야합니다. 다음은 그 예입니다.

var div = document.createElement('div'); 
div.innerHTML = 'My content'; 
document.appendChild(div); 
+1

감사합니다. 내가 바꾸어야했던 유일한 것은 마지막 줄이었고,'document.getElementById ('test'). insertBefore (div);'를 수행하고'

'을 생성 한 다음 이전에 삽입했지만 appendChild도 작동합니다. 감사합니다! – pcort