2013-03-29 5 views
1

모든 코드 예기치 T_ECHO는이 광고 오류파싱 에러 : 구문 오류

$(document).ready(function(){ 
    $('#sub').click(function() { 
     var r = confirm("سلام") 
     if (r==true){ 
     var in_code=document.getElementsByName("code")[0].value; 
     var echo="<?php 
     include ('conect.php'); 
     $res2=mysql_query("select * from products where code=".echo('document.write(in_code);');." "); 
     $row2=mysql_fetch_array($res2); 
     echo $row2['name']; 
      ?>" 
     alert(echo);   } 
     else { 
      document.getElementById("frm1").reset(); 
      } 

:

$res2=mysql_query("select * from products where code=".echo('document.write(in_code);');." "); 

에코 ('document.write를 (in_code)'); 작동하지 않습니다

어떻게 해결할 수 있습니까?

+1

코드가 작동한다고 생각하지 않습니다. js는 클라이언트 측이고 PHP는 서버 측입니다. – Class

+0

$ sql = "select * from products from code"+ in_code –

답변

1

아마도 당신이 찾고있는 것이지만 클라이언트 측과 서버 측이 혼동을 일으킬 수 있습니다. 이 스크립트는 javascript가 클라이언트 측이고 php가 서버 측이므로 작동하지 않으며 PHP 스크립트로 해석되지 않는 텍스트로만 인쇄/경고합니다.

var echo="<?php 
include 'conect.php'; 
$res2 = mysql_query(\"select * from products where code='" + in_code + "'\"); 
$row2 = mysql_fetch_array($res2); 
echo $row2['name']; 
?>"; 

이렇게하려면 Ajax를 사용해야합니다. 이런 식으로 당신의 js에 추가하십시오. 나는 위의 코드를 테스트하지 않았습니다

<?php 
    include 'conect.php'; 
    $foo = $_POST['inCode']; 
    $res2 = mysql_query("select * from products where code='$foo'"); 
    $row2 = mysql_fetch_array($res2); 
    echo $row2['name']; 
?> 

하지만 당신에게 출발을해야합니다

$(document).ready(function(){ 
    $('#sub').click(function() { 
     var r = confirm("سلام") 
     if (r==true){ 
      var in_code=document.getElementsByName("code")[0].value; 
      var request = $.ajax({ 
       url: "queryscript.php", 
       type: "post", 
       dataType: "html" , 
       data: {inCode: in_code} 
       success: function(response){ 
        alert(response); 
       } 
      }); 
     }else{ 
      document.getElementById("frm1").reset(); 
     } 
    }); 
}); 

는 파일 queryscript.php 데이터를 검색 할 수 있습니다.

1
는 따옴표
var echo="<?php 
    include (\'conect.php\'); 
    $res2=mysql_query(\"select * from products where code=\".echo('document.write(in_code);');.\" \"); 
    $row2=mysql_fetch_array($res2); 
    echo $row2['name']; 
     ?>"; 

탈출 그리고 마지막에 ;를 추가

.

+1

작은 따옴표를 이스케이프 처리하는 것은 위의 경우 선택적입니다. – techfoobar