2015-06-24 24 views
0

我需要在數據庫中寫一個單詞,並且我無法發現錯誤:ReferenceError: Patikrinta is not defined這是我的ajax腳本,它將數據發送到php文件。如果你需要的話,貝婁有php腳本。不能在stackowerflow中找到解決方案。ReferenceError因爲字

$s .= "\n\t<td>"; 
$canEdit = getPermission('tasks', 'edit', $a['task_id']); 
$canViewLog = getPermission('task_log', 'view', $a['task_id']); 
$currentTasken=$a['task_id']; 
$currentUser=$AppUI->user_id; 
$currentPercent="5"; 
$currentDescription="Patikrinta"; 
if ($canEdit) { 
    $s .= ("\n\t\t".'<a href="#">' 
      . "\n\t\t\t".'<img src="./images/icons/tick.png" alt="' . $AppUI->_('Check') 
      . '" border="0" width="12" height="12" onclick="javascript:insertData('. $currentTasken .', '.$currentUser.', '.$currentPercent.', '.$currentDescription.')" />' . "\n\t\t</a>"); 
} 
$s .= "\n\t</td>"; 
?> 
<script type="text/javascript"> 

// Note that you should use `json_encode` to make sure the data is escaped properly. 
var currentTasken = <?php echo json_encode($currentTasken=$a['task_id']); ?>; 
var currentUser = <?php echo json_encode($currentUser=$AppUI->user_id); ?>; 

function insertData(currentTasken, currentUser, currentPercent, currentDescription) 
{ 
    if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlhttp=new XMLHttpRequest(); 
    } 
    else 
    {// code for IE6, IE5 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.open("POST","modules/tasks/datafile.php",true); 
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 

    // Here, use the JS variables but, likewise, make sure they are escaped properly with `encodeURIComponent` 
    xmlhttp.send("currentUser=" + encodeURIComponent(currentUser) + "&currentTasken=" + encodeURIComponent(currentTasken) + "&currentPercent=" + encodeURIComponent(currentPercent)+ "&currentDescription=" + encodeURIComponent(currentDescription)); 
} 

</script> 

這裏是我的PHP腳本:

<?php 
$currentUser = isset($_POST['currentUser']) ? $_POST['currentUser'] : ''; 
$currentTasken = isset($_POST['currentTasken']) ? $_POST['currentTasken'] : ''; 
$currentPercent = isset($_POST['currentPercent']) ? $_POST['currentPercent'] : ''; 
$currentDescription = isset($_POST['currentDescription']) ? $_POST['currentDescription'] : ''; 
    $con = mysql_connect("localhost", "root", "") or die(mysql_error()); 
    if(!$con) 
     die('Could not connectzzz: ' . mysql_error()); 
    mysql_select_db("foxi" , $con) or die ("could not load the database" . mysql_error()); 

    $check = mysql_query("SELECT * FROM dotp_task_log"); 
    $numrows = mysql_num_rows($check); 
    if($numrows >= 1) 
    { 
     //$pass = md5($pass); 

     $ins = mysql_query("INSERT INTO dotp_task_log (task_log_creator, task_log_Task, task_log_description) VALUES ('$currentUser' , '$currentTasken', '$currentDescription')") ; 

     if($ins) 
     { 
       $check = mysql_query("SELECT * FROM dotp_tasks"); 
       $numrows = mysql_num_rows($check); 
       if($numrows > 1) 
       { 
        //$pass = md5($pass); 

        $inss = mysql_query("UPDATE dotp_tasks SET task_percent_complete = '$currentPercent' WHERE task_id='$currentTasken'") ; 

        if($inss) 
        { 
         die("Succesfully added Percent!"); 
        } 
        else 
        { 
         die("GERROR"); 
        } 

       } 
       else 
       { 
        die("Log already exists!"); 
       } 
     } 
     else 
     { 
      die("ERROR"); 
     } 

    } 
    else 
    { 
     die("Log already exists!"); 
    } 


?> 
+0

mysql_query OOUCH !! –

+0

您是否嘗試過在函數參數中添加如下引號: 'javascript:insertData('。$ currentTasken。','。$ currentUser。','。$ currentPercent。',''。$ currentDescription。'\' )「/>'。」\ n \ t \ t「);' 如果您將currentDescription作爲字符串傳遞,那麼我認爲您需要引用它或JS會嘗試找到對'Patikrinta'這將失敗(因爲它)。 – Henders

+0

你那麼我得到其他錯誤'SyntaxError:非法字符javascript:insertData(1465,37,5,\' – FriendlyPower

回答

0

您是否嘗試過周圍的函數參數這是字符串加引號? JS正在尋找「Patikrinta」的參考,因爲您並未在字符串周圍添加引號。它應該更像這樣:

javascript:insertData('. $currentTasken .', '.$currentUser.', '.$currentPercent.', \''.$currentDescription.'\')" />' . "\n\t\t</a>"); 

其他參數的工作原理是因爲它們是以數字的形式傳遞的,而Javascript將它們解釋爲這樣。這裏的區別是$currentDescription的值是Patikrinta這不是一個數字,所以JS尋找一個稱爲變量或對象的對象。

作爲一個備註 - 如果可以的話,切換到使用MySQLi是值得的。 MySQL_*函數已棄用。