开发者

Making events in php and passing variables

I am coming from asp.net and in asp.net you have the onclick event that is triggered when you press the button.. in that event you could write your code that you want to be executed...

But in PHP there is no such mechanism...

Whats the best practice to initiate a link and pass it url variables..

Do I put a javascript function in the onclick that will do the work for me (go to the server and execute the code..

<script type="text/javascript>
  function Javascript()
  {
    //what do you do here..how can you make the javascript go back to ther server.. 
    // or initialize a link say.. http://home.com?newVariable=2
  }
</script>
开发者_运维知识库


If you want to put query string variables in your JavaScript code here's an example:

<script type="text/javascript>
document.getElementById('some_link_id').addEventListener('click', function () {
    window.location = 'http://home.com?newVariable=<?php echo $_GET['newVariable']; ?>';
}, false);
</script>


As I understand:

You need to use ajax to make your event onclick communicates with the server (your file.php).

The simplest is to use jQuery functions .click and .ajax :

$('#myButton').click(function(){
    $.ajax({
        type: "POST",
        url: "file.php"
        data: "newVariable = 2"
        success: function (msg) {
            alert("Data Saved:" + msg);
        }
    })
});
  • http://api.jquery.com/click/

  • http://api.jquery.com/jQuery.ajax/

In file.php :

<?php
    $newVariable = $_POST['newVariable']; //$newVariable = 2
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜