0

I'm trying to put two onClick functions, but when i put them together I get an error. And first function ain't working, here is my script. As you can see im trying to call insertData function and then reload page with this function window.location.reload(). Bellow the script there is photo with an error.

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>

Here is the photo of the error: enter image description here

8
  • Can you show us the response of the request? Commented Jun 25, 2015 at 16:48
  • Should i put some code into script to show you the response? Commented Jun 25, 2015 at 16:52
  • The screenshot you posted shows (part of) the request. The response should be right below it. Commented Jun 25, 2015 at 16:53
  • s2.postimg.org/4t460a6ix/Untitled2.png Here is the bellow Commented Jun 25, 2015 at 16:56
  • Ok, I guess that means the request is failing with no response. Can you look on the server to see what is wrong? Check error logs, maybe the access log, you should be able to find at least a statuscode there. Commented Jun 25, 2015 at 17:00

1 Answer 1

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

Make you click function call the two functions and then set that as the click event

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));
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Okay I've updated it so that it calls window.location.reload() in a success callback. See if that works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.