试图放两个javascripts

时间:2015-06-25 16:47:16

标签: javascript php mysql

我正在尝试放置两个onClick函数,但是当我把它们放在一起时,我得到一个错误。第一个功能不起作用,这是我的脚本。正如您所见,我试图调用insertData函数,然后使用此函数window.location.reload()重新加载页面。在脚本上刻录有错误的照片。

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.', \''.$currentSummary.'\', '.$currentPercent.', \''.$currentDescription.'\'); window.location.reload();" />' . "\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); ?>;
    var currentSummary = <?php echo json_encode($currentSummary=$row[0]); ?>;

    function insertData(currentTasken, currentUser, currentSummary, 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) + "&currentSummary=" + encodeURIComponent(currentSummary) + "&currentPercent=" + encodeURIComponent(currentPercent)+ "&currentDescription=" + encodeURIComponent(currentDescription));
    }

    </script>

以下是错误的照片: enter image description here

1 个答案:

答案 0 :(得分:1)

onClick="javascript:doClickStuff('. $currentTasken .', '.$currentUser.', \''.$currentSummary.'\', '.$currentPercent.', \''.$currentDescription.'\');"

单击函数调用这两个函数,然后将其设置为单击事件

function doClickStuff(data){
   insertData(data);
}
function insertData(currentTasken, currentUser, currentSummary, 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");

           //callback to reload page
        xmlhttp.onreadystatechange = function() {
           if (request.readyState == 4 && request.status == 200) {
             window.location.reload();
           }
        }; 
            // Here, use the JS variables but, likewise, make sure they are escaped properly with `encodeURIComponent`
        xmlhttp.send("currentUser=" + encodeURIComponent(currentUser) + "&currentTasken=" + encodeURIComponent(currentTasken) + "&currentSummary=" + encodeURIComponent(currentSummary) + "&currentPercent=" + encodeURIComponent(currentPercent)+ "&currentDescription=" + encodeURIComponent(currentDescription));
    }