2014-05-17 51 views
0

爲什麼我無法將我的PHP代碼中的值傳遞給我的按鈕上的OnClick函數以傳遞給我的JavaScript函數depCheck2()?我想根據它攜帶的值重定向到2個不同的頁面。任何人都可以幫我嗎?如何將PHP值傳遞給Javascript並重定向到其他頁面?

<html> 
<head> 
<?php 
$company_id = $_GET['cont']; 
$query = "SELECT count(Department_ID) as countDep FROM department WHERE Company_ID=$company_id"; 
$result = mysql_query($query, $db) or die(mysql_error($db)); 
$row = mysql_fetch_array($result); 
extract($row); 
?> 
<script> 
function depCheck2(x){ 
    var CountRow = '<?php echo "$countDep";?>'; 
     if (CountRow>0){ 
      window.location.assign("editEvent.php?id="+x""); 
     } 
     else{ 
      window.location.assign("editEvent.php2?id="+x""); 
     } 
} 
</script> 
</head> 
<body> 
<?php 
include("config.php"); 
$company_id = $_GET['cont']; 
$query = "select * from event_details where Company_ID=".$company_id." ORDER BY EventDetails_ID DESC"; 
$result=mysql_query($query, $db) or die(mysql_error($db)); 
echo "<table border=1 width='1000'>"; 
echo "<tr><th>Serial Number</th><th>Status</th><th>Event Type</th><th>Date of Event</th><th>End Date</th><th>Details</th></tr>"; 
while ($row = mysql_fetch_array($result)) 
{ 
    extract($row);   
    echo " 
    <tr> 
     <td align='center'>$EventDetails_ID</td> 
     <td align='center'>$Status</td> 
     <td align='center'>$EventType</td> 
     <td align='center'>$StartDate</td> 
     <td align='center'>$EndDate</td> 
     <td align='center'> 
     <input type='button' value='Details' class='btn btn-large btn-primary' onClick='depCheck2(\''.$EventDetails_ID.'\')'> 
     </td> 
    </tr> 
    "; 
    } 
?> 

</table> 
</body> 
</html> 
+1

你不必在你的PHP中的'$ countDep'變量? – adeneo

+0

它來自SELECT count(Department_ID)作爲countDep – Rodave

+0

我相信你的'onClick'應該'onclick' – db579

回答

0

起初:<?php echo "$countDep";?>包含未在PHP定義$countDep

只有在數據庫中查詢存在,但你有沒有它從$result

然後window.location.assign("editEvent.php?id="+x"");語法是錯誤分配給任何變量。

結尾處有多餘的引號。

應該

+0

哈哈感謝兄弟。有效。我很喜歡這個:) – Rodave

+0

很高興能幫到:) – mohamedrias

0

試試這個:

window.location.href ="editEvent.php2?id="+x; 
+0

甚至可以使用window.location.assign(「」)。只是在最後用戶有額外的報價。另外變量$ countDep沒有在任何地方定義 – mohamedrias

相關問題