2

I want to execute php file in javascript function. Code is as shown below:

<a id="cancel" href="./?&amp;_action=list" onclick="javascript:clearRecord();return rcm.command('list','',this,event)">Cancel</a>


<script language="javascript" type="text/javascript">
var u1='michael';
function clearRecord() {
        $(function() {
            location.href = 'clear.php?h='+u1;
        });
    }
</script>

But when I click to cancel button, clear.php not executed. How I should come out from this?

4
  • 1
    No "javascript:" prefix in onclick, it expects nothing but Javascript. But you could write that single line into the onclick handler too instead of calling a function that does nothing more than that anyway. I'm not going to mention that you shouldn't have any "onclick" stuff in your HTML in any real project... :-) Commented Dec 1, 2013 at 17:26
  • 1
    AS Morre says there is not need to have javascript: in the onclick attribute. Why don't you move return rcm.command to the function clearRecord? Commented Dec 1, 2013 at 17:35
  • How can this been possible? Commented Dec 1, 2013 at 17:44
  • You seem to be using jQuery(?) - if so, here are examples for writing on-click handlers. Remove your entire onclick html-inline code and replace with a proper click handler as shown here: api.jquery.com/click Commented Dec 1, 2013 at 17:46

2 Answers 2

10

full & working answer to your question:

<html>
<head>
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script language="javascript">
        function cancelClicked() {
            // function below will run clear.php?h=michael
            $.ajax({
                type: "GET",
                url: "clear.php" ,
                data: { h: "michael" },
                success : function() { 

                    // here is the code that will run on client side after running clear.php on server

                    // function below reloads current page
                    location.reload();

                }
            });
        }
    </script>
</head>
<body>
    <a id="cancel" href="#" onclick="cancelClicked()">Cancel</a>
</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

3

if you want to just execute some php and leave current page to the one generated by this php script. then you got it almost right. I do not see that you are using jquery - so skip this "$(function(){})" part, and i don't see what u1 is added for but this will work:

function clearRecord() { location.href = 'clear.php'; }

and this will do the same:

<a href="clear.php">Cancel</a>

BUT if you want only to run "clear.php" and then reload current page. One way of doing it can be putting at the end of your "clear.php" file something like:

header("Location:/");

(it will work only if clear.php does't write anything in response).

But you can do it other ways:
- using AJAX - call clear.php with jQuery.get() and call location.reload() on success;
- using IFRAME - set iframe's location to clear.php and then call window.location.reload();
- using IMG - set img.src to clear.php ...
...and possibly many other ways :)

2 Comments

This page is another one(com.html) & I want to return on this page. If I use above code, it'd redirect to clear.php
i see you don't get what states in my answer - i'll give you one more below - don't forget to mark it as usefull

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.