ajax 옳게 사용했는지... 질문드립니다 채택완료
전에 자바스크립트에서 php코드를 실행시킬수 있는지 질문 작성했던 사람입니다
많은 분들이 댓글에 ajax를 사용하라고 하셨네요
좀 찾아보다가
https://sir.kr/qa/330419">https://sir.kr/qa/330419 에 왕계란 님이 댓글 참고해서 작성해보았습니다
click 이벤트는 예제들도 몇개 보이길래 체크박스 이벤트를 끼워넣었는데 실행이 안되네요... ㅜㅜ
스크립트 코드는 html/bbs/submit.php 에 있고
불러올 페이지도 html/bbs/checking.php 로 둘다 같은 위치에 있습니다
</p>
<p><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></p>
<p>
</p>
<p><?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;
?></p>
<p>
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 "성공";
}
?>
답변에 대한 댓글 2개
$sql = "select '$test' from '$table' where submit_id = '$submit_id' "; 에서
$sql = "select $test from $table where submit_id = '$submit_id' "; 로 바꿔주면 되네요!!
댓글을 작성하려면 로그인이 필요합니다.
답변을 작성하려면 로그인이 필요합니다.
로그인
이렇게 해도 안되네요... 덕분에 ajax 사용법은 어느정도 파악되었네요