자바 스크립트를 사용하여 입력 필드를 채우려 고하는데 모든 스크립트가 작동하지 않습니다. 입력 필드를 채우려는 자바 스크립트 부분이 아닌 PHP의 에코 만 표시됩니다.
이것은 내 코드입니다
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$card = $_POST['card'];
echo json_encode(array('card_response' => get_cc_type($card)));
//exit;
}
그리고 이것은 내 스크립트이지만 왜 작동하지 않는지 모르겠습니다.
<script type="text/javascript">
document.getElementById('button').addEventListener('click', function(){
var card_value = document.getElementById('card').value; // get the input value
var xmlhttp = new XMLHttpRequest(); // create the xmlhttprequest object
var params = 'card=' + card_value; // create the query string
var php_url = document.URL; // this is the URL of the PHP, in this example, i'll just setup it the PHP on the same page.
xmlhttp.open('POST', php_url, true); // set as POST
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
// handle the response from server here
var response = xmlhttp.responseText;
response = JSON.parse(response);
document.getElementById('cardtype').value = response.card_response; // set the value
}
}
xmlhttp.send(params); // execute
});
</script>
내 페이지에만이 항목이 표시됩니다. --- { "card_response": "VIP"}
jquery에서 먼저 경고로 확인하십시오.
alert(xmlhttp.toSource());
alert(response.toSource());
alert(response.card_response);
이 경고는 정확히 어떤 값을 얻고 있는지 알려줍니다. xmlhttp.status가 200인지 여부입니다.
또한 Firebug를 확인하십시오. jquery가 오작동합니까?
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다