2015-01-23 2 views
0

PHP로 온라인 검사 응용 프로그램을 만들고 AJAX 호출에 문제가 있습니다.jquery에서 ajax 함수 호출에서 매개 변수를 전송하는 방법

오른쪽 버튼 중 하나를 클릭하면 AJAX 호출을 사용하여 질문을 가져오고 (div를 채우는 데 사용됩니다.) AJAX 호출을 사용하여 질문을 가져오고 싶습니다. 이 단추는 고정되어 있지 않습니다. 그들은 PHP를 사용하여 서버에서 생성됩니다.

Here is interface of front end

내가 AJAX 호출을 찾고 있어요는 다음과 같이 할 수 있습니다 :

functionname=myfunction(some_id){ 
ajax code 
success: 
html to question output div 
} 

및 버튼과 같은 기능을 호출해야합니다 :

<button class="abc" onclick="myfunction(<?php echo $question->q_id ?>)"> 

을 제안하십시오 이 일을 할 수있는 AJAX 호출

+0

, 내가 정확한 JQuery와 –

답변

1

잘못하고있다. 방법. jQuery에는 이와 같은 연산자가 내장되어 있습니다. 당신이 버튼을 생성 할 때

첫째, 난 당신이 만드는 것과 같은 방법으로 생성 좋을 것 :

jQuery(document).on('click', 'button#abc', function(e){ 
    e.preventDefault(); 
    var q_id = jQuery(this).data('question-id'); // the id 

    // run the ajax here. 
}); 
+1

전능하신 $>을 모르는, 그것을 할 방법을 알고 있지만 jQuery를 –

+0

@CoryDanielson 바로 그 순간에 버릇이 생길 것입니다. $ :(대신 jQuery를 사용하여 고정 – Darren

1

내가 당신을 제안 : 이제

<button id="abc" data-question-id="<?php echo $question->q_id; ?>"> 

버튼에 리스너/바인드를 만들 다음과 같이 버튼을 생성하십시오.

<button class="question" data-qid="<?php echo $question->q_id ?>"> 

그리고 이벤트 수신기는 다음과 같은 일이 :

$("button.question").click(function(e) { 
    var button = $(e.target); 
    var questionID = button.data('qid'); 
    var url = "http://somewhere.com"; 
    $.ajax({ method: "GET", url: url, success: function(data) { 
    $("div#question-container").html(data); 
    }); 
}); 
1

HTML

<button class="abc" questionId="<?php echo $question->q_id ?>"> 

스크립트

$('.abc').click(function() { 
var qID = $(this).attr('questionId'); 
$.ajax({ 
    type: "POST", 
    url: "questions.php", //Your required php page 
    data: "id=" + qID, //pass your required data here 
    success: function (response) { //You obtain the response that you echo from your controller 
     $('#Listbox').html(response); //The response is being printed inside the Listbox div that should have in your html page. Here you will have the content of $questions variable available 
    }, 
    error: function() { 
     alert("Failed to get the members"); 
    } 
}); 

})

유형 수 변수 e는 브라우저에 PHP 문서로 보내려는 호출 유형을 알려줍니다. 여기서는 양식으로 작업하는 것처럼 GET 또는 POST를 선택할 수 있습니다.

데이터은 양식에 전달되는 정보입니다.

성공은 PHP 파일 호출이 성공하면 jQuery가 수행 할 작업입니다.아약스에

here

PHP 내가 이러한 코드를 찾기 위해 노력

$id = gethostbyname($_POST['id']); 
//$questions= query to get the data from the database based on id 
return $questions; 
관련 문제