테스트 사이트 - 개발 중인 베타 버전입니다

자바스크립트로 추가된 자료 php로 mysql 저장하기 채택완료

많은도움필요 3년 전 조회 2,533

제목이 알아듣기 힘든점 죄송합니다 ㅠ 

 

php엑셀로 자료를 받아온 후에 table로 표에 뿌려주었습니다.

 

그리고 자바스크립트로 해당 표 위쪽에 자유롭게 데이터를 추가 할 수 있는 코드를 추가해 주었습니다.

 

이 자료들은 전부다 Id와 password 인데 이자료들을 name = id[<?php echo count ?>] 이런식으로 

 

add.php로 넘겨서 mysql 에 저장하는 식입니다.. 

 

이경우에 기존에 excel로 추가된 자료들과 단 하나의 js로 추가된 자료가 db에 저장되는데 

 

안되는 제가 생각하는 이유는

 

.js는 브라우저 에서 동작하기 때문에 create element로 생성된 tr td input 요소의 count가 ++ 되지 않아서

 

같은데.. 이런 경우에는 어떻게 값을 넘겨서 모두 db에 추가 할 수 있을까요..? 

 

 

엘리먼트를 추가하는 코드는  아래이구 

</p>

<p>var trTag = document.createElement("tr");</p>

<p>                        var table = document.querySelectorAll("[id='main_form']")[0].querySelector("tbody")</p>

<p>                        var i=10000;</p>

<p>                        table.insertBefore(trTag, table.childNodes[1]);</p>

<p> </p>

<p>                        //var tdTag = document.createElement("td");</p>

<p> </p>

<p>                        document.querySelectorAll("[id='main_form']")[0].querySelectorAll("tbody tr")[1].appendChild(document.createElement("td"))</p>

<p>                        document.querySelectorAll("[id='main_form']")[0].querySelectorAll("tbody tr")[1].appendChild(document.createElement("td"))</p>

<p>                        document.querySelectorAll("[id='main_form']")[0].querySelectorAll("tbody tr")[1].appendChild(document.createElement("td"))</p>

<p> </p>

<p>                        document.querySelectorAll("[id='main_form']")[0].querySelectorAll("tbody tr")[1].querySelectorAll("td")[0].setAttribute("class", "hi")</p>

<p>

 </p>

<p>                        var input = document.createElement("input")</p>

<p>                        input.setAttribute("type", "checkbox")</p>

<p>                        input.setAttribute("class", "check")</p>

<p>

 </p>

<p>                        document.querySelectorAll("[id='main_form']")[0].querySelectorAll("tbody tr")[1].querySelectorAll("td")[0].appendChild(input)</p>

<p>

 </p>

<p>                        var input1 = document.createElement("input")</p>

<p>                        input1.setAttribute("type", "text")</p>

<p>                        input1.setAttribute("class", "inputtag")</p>

<p>                        input1.setAttribute("name", "keyword[i]")</p>

<p> </p>

<p>                        var input2 = document.createElement("input")</p>

<p>                        input2.setAttribute("type", "text")</p>

<p>                        input2.setAttribute("class", "inputtag")</p>

<p>                        input2.setAttribute("name", "number[i]")</p>

<p>                        console.log(input2);</p>

<p>                       </p>

<p> </p>

<p>                        document.querySelectorAll("[id='main_form']")[0].querySelectorAll("tbody tr")[1].querySelectorAll("td")[1].appendChild(input1)</p>

<p>                        document.querySelectorAll("[id='main_form']")[0].querySelectorAll("tbody tr")[1].querySelectorAll("td")[2].appendChild(input2)</p>

<p> </p>

<p>

 

폼에서 php로 넘어가는 코드는 

</p>

<p><tr></p>

<p>                        <td class="checkform"><input type="checkbox" class="check"></td></p>

<p>                        <td><input type="text" class="inputtag" name="keyword[<?php echo "$count" ?>]" value=<?php echo $row['A'] ?>></td></p>

<p>                        <td><input type="text" class="inputtag" name="number[<?php echo "$count" ?>]" value=<?php echo $row['B'] ?>></td></p>

<p> </p>

<p>                    </tr></p>

<p> </p>

<p>                <?php</p>

<p>                    $count++;</p>

<p>                }</p>

<p> </p>

<p>                ?></p>

<p>

 

이렇습니다... 도움 부탁드립니다...ㅠㅠ 감을 못잡겠어서 .. 

댓글을 작성하려면 로그인이 필요합니다.

답변 2개

채택된 답변
+20 포인트
복스리
3년 전

코드와 내용을 확인하였을때 개인적인 생각으로 좀 모~~ 하다는 생각이 드네요.!

저의 생각이 맞는지는 모르겠습니다.만 현 tag안의 input형식의 값을  mysql로  update 내지는 insert하신다는 말씀이신지요? 혹시 아니면 js로 실시간으로  이 input 값이 들쑥 날쑥형식인가요?  조금 어렵게 개념을 잡으시는거 같아서요.

 

</p>

<p>        var pf = document.write;</p>

<p>        var input_data = $(pf).serialize();

        var save_result = "";

        $.ajax({

            type: "POST",

            data: input_data,

            url: g5_url+"[입력받을 주소]",

            cache: false,

            async: false,

            success: function(data) {

                save_result = data;

            }

        });

 </p>

<p>

 

위에 코드를 활용해보시면 입력값[tag]이 얼마가 많던 적던 ajax방식으로 입력페이지에서 받을수 있습니다.

이런식으로 하시면 도움이 되지 않을까 생각하면 구지 loop를 활용해서 하시는것보다 tr 안쪽에 input tag를 생성후에 넘겨주는 방식이 어떨까 생각해 보아요.

 

 

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

많은도움필요
3년 전
일단 도움 감사드립니다 ㅠ

웹 페이지는 excel 에 있는 id, pw 자료들을 테이블로 뿌려주고

개별적으로도 id , pw 를 추가하기 위해 칸을 추가 할 수 있습니다.

그리고 엑셀에서 잘 못된 정보들이 있을 수 있기 때문에 사용자가 검수 후 체크박스를 클릭하여

데이터들을 삭제할 수 있습니다. 그러고 마지막에 db 에 넣을 자료들을 간추린 후

db에 넣습니다.

이런 경우에도 해당 코드를 활용 할 수 있을까요 ..? 그리고 ajax를 쓰지 않는 방법도 있을까요..?

정보들을 저장한 후에 바로 다른 페이지로 이동이 되어야 해서요..

댓글을 작성하려면 로그인이 필요합니다.

3년 전

create element로 생성된 tr td input 요소의 count가 ++ 되지 않아서<---카운트 여부에 상관없이

form 내부에 추가했다면 당연히 add.php로 넘어 갑니다

add.php에서 print_r($_POST)해서 넘어온 값들을 보고 처리 방법을 찾으세요

 

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인