2014-03-05 12 views
0

url : 섹션의 jquery 스크립트에 $ data 변수를 보내어 다른 PHP 파일에서 소스를 가져올 수 있습니까? (초보자 프로그래머). 내가 예를 들어 같은 것을 할 수 있어야합니다 :JQUERY - PHP 변수를 저장하고 jquery 스크립트에 에코?

var source = 
      { 
       datatype: "json", 
       datafields: [ 
        { name: 'timeserver', type: 'time'}, 
        { name: 'val1', type: 'number'}, 

       ], 

       url: '<?php echo $data; ?>.php', 

       cache: false 
      }; 


<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <link rel="stylesheet" href="jqwidgets/styles/jqx.base.css" type="text/css" /> 
    <script type="text/javascript" src="scripts/jquery-1.10.2.min.js"></script> 
    <script type="text/javascript" src="jqwidgets/jqxcore.js"></script> 
    <script type="text/javascript" src="jqwidgets/jqxchart.js"></script>  
    <script type="text/javascript" src="jqwidgets/jqxdata.js"></script> 

    <script type="text/javascript"> 
     $(document).ready(function() { 
      // prepare the data 
      var theme = 'classic'; 

      var source = 
      { 
       datatype: "json", 
       datafields: [ 
        { name: 'timeserver', type: 'time'}, 
        { name: 'val1', type: 'number'}, 

       ], 
       url: 'data.php', 
       cache: false 
      };  

      var dataAdapter = new $.jqx.dataAdapter(source, 
      { 
       autoBind: true, 
       async: false, 
       downloadComplete: function() { }, 
       loadComplete: function() { }, 
       loadError: function() { } 
      }); 

     // prepare jqxChart settings 
      var settings = { 
       title: "Temperatura", 
       showLegend: true, 
       padding: { left: 5, top: 5, right: 5, bottom: 5 }, 
       titlePadding: { left: 90, top: 0, right: 0, bottom: 10 }, 
       source: dataAdapter, 
       categoryAxis: 
        { 
         text: 'Category Axis', 
         textRotationAngle: 0, 
         dataField: 'timeserver', 
         formatFunction: function (value) { 
          return $.jqx.dataFormat.formatdate(value, 'HH:mm:ss'); 
         }, 
         showTickMarks: true, 
         tickMarksInterval: Math.round(dataAdapter.records.length/6), 
         tickMarksColor: '#888888', 
         unitInterval: Math.round(dataAdapter.records.length/6), 
         showGridLines: true, 
         gridLinesInterval: Math.round(dataAdapter.records.length/3), 
         gridLinesColor: '#888888', 
         axisSize: 'auto'      
        }, 
       colorScheme: 'scheme05', 
       seriesGroups: 
        [ 
         { 
          type: 'line', 
          valueAxis: 
          { 
           displayValueAxis: true, 
           description: 'Temperatura', 
           //descriptionClass: 'css-class-name', 
           axisSize: 'auto', 
           tickMarksColor: '#888888', 
           unitInterval: 5, 
           minValue: 10, 
           maxValue: 50       
          }, 
          series: [ 
            { dataField: 'val1', displayText: 'Temperatura' } 
           ] 
         } 
        ] 
      }; 

      // setup the chart 
      $('#jqxChart').jqxChart(settings); 
     }); 
    </script> 
    <style type="text/css"> 
</style> 
</head> 
<body class='default'> 
    <!-- <div class="flip3D"> 
     <div class="back">--> 
     <div style="width:670px; height:400px" id="jqxChart"> 
     </div> 
     <!--</div> 
     <div class="front"><div style="width:670px; height:400px" id="jqxChart"></div></div> 
    </div> --> 
     <div class="flip3D"> 
<?php 
// define variables and set to empty values 
$data = $data1h = $data24h = $data7zile = $data1luna = ""; 

if ($_SERVER["REQUEST_METHOD"] == "POST") 
{ 
    $data = test_input($_POST["gender"]); 
} 
function test_input($data) 
{ 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
} 
?> 

<h2>Arhiva</h2> 
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 

    Select: 
    <input type="radio" name="gender" value="data" >Curent 
    <input type="radio" name="gender" value="data1h" >Ultima ora 
    <input type="radio" name="gender" value="data24h" >Ultima zi 
    <input type="radio" name="gender" value="data7zile" >Ultima saptamana 
    <input type="radio" name="gender" value="data1luna" >Ultima luna 
    <br><br> 
    <input type="submit" name="submit" value="Submit"> 

</form> 
<?php 
echo json_encode($data); 
?> 
</div> 
</body> 

+0

당신은 브라우저에서 렌더링됩니다 페이지에 PHP 변수를 구문 분석 할 수 있습니다. – Daniel

+0

정확히 어디서 문제가 있습니까? 좀 더 구체적으로 말하십시오 – rpax

답변

0
var data = "<?php echo data; ?>"; 

위의 줄은 PHP 템플릿 /보기/컨트롤러 jQuery를에

사용에 간다 :

url : data + '.php' 
+0

사용자 입력으로부터 PHP 변수를 출력하는 것은 끔찍한 생각입니다 – davur

+0

'$ data'는 현재'trim','stripslashes' 및'htmlspecialchars 'javascript (newlines 특히)에서는 안전하지 않습니다. 'var data = ;'javascript 용으로'$ data'를 이스케이프 처리 할 때 더 좋은 옵션입니다 (제 답변 참조). – davur

0

은 그 부분에 관하여 경우

url: '<?php echo $data; ?>.php' 

는 예, 가능합니다.

+0

사용자 입력으로부터 PHP 변수를 출력하는 것은 끔찍한 생각입니다 – davur

0

이 다른 답변을 작동 할 수 있지만이 작업을 수행하는 올바른 방법으로하지 않습니다 .

적절한 방법은 json_encode입니다.

var data = <?php echo json_encode($data) ?>; 

PHP 구문에는 따옴표가 없습니다. json_encode은 javascript 용 문자를 특별히 이스케이프 처리하고 빈 변수뿐만 아니라 다른 유형도 올바르게 처리 할 수 ​​있습니다.

: 당신이 여기서 뭘하려고하는 것은 나에게 분명하지 않다하지만

<?php 
    $str = "Hello World"; 
    $nothing = null; 
    $arr = ['apple', 'orange', 'banana']; 
    $user = array(
     'name' => 'Rob', 
     'age' => 42, 
     'admin' => true, 
    ); 
?> 

<script type="text/javascript"> 
    var str = <?php echo json_encode($str) ?>, 
     nothing = <?php echo json_encode($nothing) ?>, 
     arr = <?php echo json_encode($arr) ?>, 
     user = <?php echo json_encode($user) ?>; 
</script> 



// Results in all valid javascript variables 
var str = "Hello World", 
    nothing = null, 
    arr = ["apple","orange","banana"], 
    user = {"name":"Rob","age":42,"admin":true}; 
관련 문제