2012-03-26 3 views
0

PHP에서 Javascript/jQuery로 어떻게 할 수 있습니까? 시작일부터 최대 6 개월 전에 미리 선택 목록에 채우려고합니다.어떻게 이것을 PHP에서 JavaScript로 변환합니까?

$startDate = intval($c->getStartDate($pid)); 
    $endDate = strtotime('+6 month', $startDate); 
    $dateSelectList = '<select name="dateSelect" id="dateSelect">'; 
    $count = 1; 
    //populates the select list with the starting date of the course up to the next six months 
    for($date = $startDate; $date <= $endDate ; $date = strtotime('+1 day', $date)) 
    { 
     $dateSelectList .= '<option id="select'.$count.'" value="'.$date.'">'.date('D d F Y', $date).'</option>'; 
     $count++; 
    } 

    $dateSelectList .= '</select>'; 
+2

당신이 지금까지 생각해 있나요? – Zar

+1

JavaScript가 그림을 입력하는 곳을 보지 못했습니다 ... –

답변

3

이렇게 할 수있는 방법 중 하나는 jQuery와 함께 ajax를 사용하는 것입니다.

$startDate = intval($c->getStartDate($pid)); 
$endDate = strtotime('+6 month', $startDate); 
$dateSelectList = ''; 
$count = 1; 
//populates the select list with the starting date of the course up to the next six months 
for($date = $startDate; $date <= $endDate ; $date = strtotime('+1 day', $date)) 
{ 
    $dateSelectList .= '<option id="select'.$count.'" value="'.$date.'">'.date('D d F Y', $date).'</option>'; 
    $count++; 
} 

echo $dataSelectList; 

후 별도의 파일로 저장

이 populate.php에게

그런 다음 HTML 페이지에 다음과 같은 코드를 작성 말 : 그래서 첫 번째 단계는 당신의 코드를 수정하는 것입니다. 코드는 페이지에 포함 된 jQuery를 가지고 있고 아이디와 선택 요소를 가지고 = dateSelect

<script> 
$(function(){ 
    $.get('populate.php',function(data) 
    { 
     $('#dateSelect').html(data); 
    } 
}); 
</script> 

위의 코드는 모든 옵션을 반환하고로를 주입 페이지의로드에 populate.php 호출 가정 요소를 선택하십시오

+0

그래,이게 내가 한 짓이지만 선택 목록은 내가 jquery로 조작 할 수있는 유일한 것이 아니라는 것을 깨달았다. jquery에 대한 데이터. –

+0

흠, 알 겠어. 네가 여기에서 가져 가면 좋겠다. – boug

1

여기 PHP에서 내 시도 -> 귀하의 코드의 자바 스크립트 포트입니다. 6 개월 기간의 정확한 일수는 해당 기간의 시작일과 종료일에 따라 다르므로 정확한 6 개월 범위가 필요한 경우 각 달의 일수를 기준으로 더 많은 계산을 수행해야합니다. 아래의 코드는 6 개월의 시간 프레임에서 182 일을 가정합니다.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
     <title>Untitled Document</title> 
    </head> 

    <script> 
     function newOption(value, text) { 
      var opt = document.createElement('option'); 
      opt.text = text; 
      opt.value = value; 
      return opt; 
     } 

     function formatDate(date) { 
      var da = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'], 
       ma = ['January','February','March','April','May','June','July','August','September','October','November','December'], 
       dr = date.getDate(), 
       wr = date.getDay(), 
       mr = date.getMonth(), 
       yr = date.getFullYear(), 
       dn = da[wr], 
       ds = (dr.toString().length == 1) ? '0' + dr.toString() : dr.toString(), 
       mn = ma[mr], 
       ys = yr.toString(), 
       v = dn + ' ' + ds + ' ' + mn + ' ' + ys; 

      return v; 
     } 

     function PopulateDateSelector(startDate, endDate, selector) { 
      while(selector.length > 0) selector.remove(0); 

      while(startDate < endDate) { 
       selector.add(newOption(startDate.getTime().toString(), formatDate(startDate))); 
       startDate.setTime(startDate.getTime() + (24*60*60*1000)); 
      } 
     } 


    </script> 
    <body> 

     <select id="dateSelector"></select> 

     <script type="text/javascript">    
      var s = document.getElementById('dateSelector'), 
       startDate = new Date(), 
       endDate = new Date(startDate.getTime() + (182*24*60*60*1000)); 
       //182 is estimated number of days in a 6 month period. 

      PopulateDateSelector(startDate,endDate,s); 
     </script> 
    </body> 
</html> 
1

당신이 수해야 검색하는 더 또는 덜 :

<html> 
<head> 
    <title>Demo Javascript populing select field</title> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" ></script> 
    <script type="text/javascript" > 
     $("document").ready(function(){ 
      nElement = 6; 
      sList = {}; 
      for (i = 0; i<=nElement; i++) { 
       var now = new Date(); 
       var nextDate = new Date(); 
       nextDate.setMonth(now.getMonth()+(i*6)); 
       var value = nextDate.getMonth()+"/"+nextDate.getFullYear(); 
       sList[value] = value; 
      } 


      $.each(sList, function(key, value){ 
       var option = $("<option></option>").attr("value",key).text(value); 
       $("#demo-select").append(option); 

      }); 
     }); 
    </script> 
</head> 
<body> 
    <select name="demo-select" id="demo-select" ></select> 
</body> 

관련 문제