2016-08-01 2 views
1

초보 프로그래머이므로 테이블의 레코드 수를 계산하고 싶습니다. 코드의 추출물을 많이 보았지만 자바 스크립트 코드로 PHP 결과를 전송하기 위해 함께 묶는 것 같지 않습니다. 여기에 내가 함께 결국 한 코드입니다 :AJAX (jQuery 제외) 및 PHP를 사용하여 레코드 수를 계산하는 방법

showscan.php

<php 
header("Access-Control-Allow-Origin: *"); 
header("Content-Type: application/json; charset=UTF-8"); 
error_reporting(E_ERROR); 
try{ 
    $conn = new mysqli("localhost", "root", "root", "bencoolen"); 

    $query = "SELECT COUNT(*) FROM discountcode"; 
    $result = $conn->query($query); 
    echo mysql_num_rows($result); 

    $conn->close(); 
} 
catch(Exception $e) { 
    echo "error"; 

} 
?> 

index.html을

<head> 
<!-- The policy below is cancelled out so the project can run on my android device's version -->  
<!--<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">--> 
<meta name="msapplication-tap-highlight" content="no"> 
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width"> 
<link rel="stylesheet" type="text/css" href="css/index.css"> 
<link rel="stylesheet" href="js/jquery.mobile-1.4.5.min.css"> 
<link rel="stylesheet" href="css/jquery.mobile.datepicker.css" /> 
<script src="js/jquery-1.11.3.min.js"></script> 
<script src="js/jquery.mobile-1.4.5.min.js"></script> 
<script src="js/jquery.ui.datepicker.js"></script> 
<script id="mobile-datepicker" src="js/jquery.mobile.datepicker.js"></script> 
<script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js"></script> 
<script type="text/javascript" src="barcodescanner.js"></script> 
<script type="text/javascript" src="cordova.js"></script> 
<script src="common.js"></script> 

<script type="text/javascript"> 
    function showscan(){ 
     var xmlhttp = new XMLHttpRequest(); 
     var url = serverURL() + "/showscan.php"; 
     xmlhttp.onreadystatechange = function() { 
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 

       //The alert below was alerted       
       alert("readystate and status OK"); 
       document.getElementById("noscan").innerHTML = xmlHttp.responseText; 

       //The alert below was not alerted 
       alert(document.getElementById("noscan").innerHTML);    
      } 
     }; 
     xmlhttp.open("GET", url, true); 
     xmlhttp.send(); 
    } 
</script> 
</head> 

<body> 
    <div data-role="page" id="pageone"> 
     <div data-role="content" class="ui-content" id="loginsection"> 
      <form name="LoginForm"> 
       <div class="ui-grid-a"> 
        <div class="ui-block-b"> 
         <input id="noscan" type="text" class="input-text" placeholder="Number of scans"/> 
        </div> 
       </div> 
      </form> 
     </div> 
    </div> 
</body> 

'. document.getElementById를 ("noscan")는 innerHTML을' 아니었다 알림을 받고 필드가 'document.getElementById ("noscan"). innerHTML' 아래 명시된 함수를 실행 한 후에도 공백이었습니다. 이 코드는 json_encoding을 모두 제거한 후 프로그래밍 노트의 일부였습니다. 그 이유는 무엇인지 잘 모르고 예제를 더 잘 따르기 때문입니다. jQuery가 설치되어 있지만 jQuery Ajax가 너무 압도적인데 jQuery를 사용하지 않고 Ajax 호출을 시각화 할 수있다.

질문 : PHP의 개수 (*) 값이 자바 스크립트 코드로 이전되도록하려면 어떻게해야합니까? JSON으로 결과를 인코딩하기 전에 어떤 상황에서 인코딩해야합니까?

감사합니다. xmlhttp해야 자바 스크립트 함수 showscan()의 라인 document.getElementById("noscan").innerHTML = xmlHttp.responseText;showscan.php =><?php

xmlHttp

오타 귀하의 <php 태그 :

+0

그리고 html 코드? – jonju

+0

괜찮아요, 물건의 대부분을 추가, 오직 로그인 및 비밀 번호와 같은 관련이없는 분야를 떠났다! –

+1

'innerHTML' 대신'value'를 사용해보십시오 :'document.getElementById ("noscan"). value = xmlHttp.responseText;' – ImClarky

답변

1

좋아, 당신은 약간의 오타 & 코드에 약간의 오차가 있습니다. (작은 h) 이후 JavaScript입니다.입니다.

오류 스크립트 showscan() document.getElementById("noscan").innerHTML = xmlHttp.responseText; noscantext input이고 입력 텍스트 속성 innerHTML

showscan.php echo mysql_num_rows($result);

mysql_num_rows;mysqli_num_rows; 통지되어야이없는 차? 같은 폴더에이 showscan.php 페이지가있는 경우 mysql & mysqli

Suggesstion는

serverURL()var url = serverURL() + "/showscan.php";에 그럼 그냥 showscan.php 물품.

코드를 보면 HTML 코드를 보았을 때 귀하의 기능을 호출하는 내용이 전혀 표시되지 않습니다. showscan().따라서, 하나는 body로드 <body onload="showscan();">에 전화 또는 <button>를 삽입하고 onclick 속성을 호출 할 수 있습니다 <button type="button" onclick="showscan();">submit</button>

지금이 코드를 TRY

showscan.php

<?php 
    header("Access-Control-Allow-Origin: *"); 
    header("Content-Type: application/json; charset=UTF-8"); 
    error_reporting(E_ERROR); 
    try{ 
     $conn = new mysqli("localhost", "root", "root", "bencoolen"); 
     $query = "SELECT * FROM discountcode"; 
     $result = $conn->query($query); 
     echo mysqli_num_rows($result); 
     $conn->close(); 
    } 
    catch(Exception $e) { 
     echo "error"; 
    } 
?> 

자바 스크립트 함수 showscan()

function showscan(){ 
    var xmlhttp = new XMLHttpRequest(); 
    var url = "showscan.php"; 
    xmlhttp.onreadystatechange=function() { 
     if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
      //The alert below was alerted       
      alert("readystate and status OK"); 
      document.getElementById("noscan").value = xmlhttp.responseText; 
      //The alert below was not alerted 
      alert(document.getElementById("noscan").value); 
     } 
    }; 
    xmlhttp.open("GET", url, true); 
    xmlhttp.send(); 
} 
+0

어이! 이거 야! 대답을 향해 단계별로 나가서 시간을내어 살펴보고 코드를 입력 해 주셔서 감사합니다. –

+0

도와 드리겠습니다. 도움을 청합니다. – jonju

+0

mysqli_num_rows ($ result); _;를 사용하려면 내 sql 쿼리도 잘못되었습니다. 그래서 'select count()'대신 'select *'문을 편집 할 것입니다. –

관련 문제