ajax 옳게 사용했는지... 질문드립니다

매출이 오르면 내리는 수수료! 지금 수수료센터에서 전자결제(PG)수수료 비교견적 신청해 보세요!
ajax 옳게 사용했는지... 질문드립니다

QA

ajax 옳게 사용했는지... 질문드립니다

본문

전에 자바스크립트에서 php코드를 실행시킬수 있는지 질문 작성했던 사람입니다

많은 분들이 댓글에 ajax를 사용하라고 하셨네요

좀 찾아보다가 

https://sir.kr/qa/330419 에 왕계란 님이 댓글 참고해서 작성해보았습니다

 

click 이벤트는 예제들도 몇개 보이길래 체크박스 이벤트를 끼워넣었는데 실행이 안되네요... ㅜㅜ

 

스크립트 코드는 html/bbs/submit.php 에 있고

불러올 페이지도 html/bbs/checking.php 로 둘다 같은 위치에 있습니다

 


<script>
$(function() {
    $("#checkBoxId").change(function() {
        if($("#checkBoxId").is(":checked")) {
            $.ajax({
                url : "checking.php",
                type : "post",
                data : {
                    tbl : "g5_submit",
                    submit_id : 4,
                    test : "upload_time"
                },
                success : function(res) {
                    if(res) {
                        $("#result").text(res);
                    }
                }
            });
        }
        else{
            $.ajax({
                url : "checking.php",
                type : "post",
                data : {
                    tbl : "g5_submit",
                    submit_id : 4,
                    test : "tasker_id"
                },
                success : function(res) {
                    if(res) {
                        $("#result").text(res);
                    }
                }
            });            
        }
    });
});
</script>

 


<?php
include_once "./_common.php";
$table = $_POST['tbl'];
$submit_id = $_POST['submit_id'];
$test = $_POST['test'];
// $sql = "update '$table' set checking = 1 where submit_id = '$submit_id' ";
$sql = "select '$test' from '$table' where submit_id = '$submit_id' ";
$result = sql_fetch($sql);
echo $result;
?>

 

 

1. 첫번째 코드처럼 체크박스 이벤트일때 저런식으로 사용하는 것이 맞나요?? 아니라면 방법좀 

알려주세요 ㅠㅠ

 

2. checking.php 랑 submit.php 의 위치를 동일한 곳에 두었는데

$.ajax({
                url : "checking.php",

url을 잘못 설정한것은 아니겠죠?

 

3. 전체 코드중에서 잘못된 부분이 있을까요?

 

4. 검색결과 ajax 사용법이 다 다르던데(생활코딩에 있던 문법과 많이 다르네요 ㄷㄷ) 

ajax 명확하게 설명된 사이트가 있을까요??

이 질문에 댓글 쓰기 :

답변 2

<script>
$(function() {
    $("#checkBoxId").change(function() {
      var param="";
      //if문으로 파라미터 값을 변경할 수 있도록 하시면 소스가 줄어들어요
      if($("#checkBoxId").is(":checked")) {
        param="upload_time";
      }else{
        param="tasker_id";
      }
      //ajax를 쓸 경우 하나만 쓰시면 좋습니다. if else 안에 넣으면 두번 손봐야 하기 때문에 공통된 부분은 한 곳에 놔두는게 좋아요
      $.ajax({
          url : "checking.php",
          type : "post",
          dataType:"html",//리턴받는 데이터타입을 넣어주셔야 해요
          data : {
              tbl : "g5_submit",
              submit_id : 4,
              test : param
          },
          success : function(res) {
              if(res) {
                  $("#result").text(res);
              }
          }
      });

    });
});
</script> 

php부분

<?php
include_once "./_common.php";
$table = $_POST['tbl'];
$submit_id = $_POST['submit_id'];
$test = $_POST['test'];
// $sql = "update '$table' set checking = 1 where submit_id = '$submit_id' ";
$sql = "select '$test' from '$table' where submit_id = '$submit_id' ";
$result = sql_fetch($sql);
if($result[필드명]){
  echo "성공";
}
?>

일단 답변 감사드립니다... 우선 실행은 안되네용 ㅠㅠ [필드명] 주신곳에 체크했을시 받은 파라미터인 'upload_time'을 넣었고 url : "checking.php", 은 도메인주소/bbs/ 를 추가해서 다시 넣었습니다(두 파일다 bbs 밑에 있어요).

이렇게 해도 안되네요... 덕분에 ajax 사용법은 어느정도 파악되었네요

ㄷㄷ;; 오류 찾았습니다 ㅜㅜ
$sql = "select '$test' from '$table' where submit_id = '$submit_id' "; 에서
$sql = "select $test from $table where submit_id = '$submit_id' "; 로 바꿔주면 되네요!!

경로만 설명드릴게요.

url : "checking.php",

url : "/checking.php",

url : "./checking.php",

url : g5_bbs_url+"/checking.php",

 

이렇게 하셔도 될겁니다.

답변을 작성하시기 전에 로그인 해주세요.
전체 0
QA 내용 검색
  • 개별 목록 구성 제목 답변작성자조회작성일
  • 질문이 없습니다.

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT