I'm just learning jquery , The thing is that how can I make jquery and php contact each other in one single page ? if there is an example I really appreciate it!

Member Avatar for diafol

Depends on the direction of communication and whether you want communication after the page has left the server.

You can include php inside js scripts for scripts running on page load...

var myVar =<?php echo $hisVar;?>;

However, you can't do the reverse as javascript is run after the info has left the server, so can't communicate with PHP. So this...

<?php $hisVar = myVar; ?>

can't work - well it couldn't anyway - but we CAN use AJAX.

var req = $.ajax({
        url: 'phpfile.php',
        method: 'get',
        data: {isbn: myisbn},
        dataType: 'json'
    });
req.done(function(data){
        $('#msg').html(data.message);
    }); 

This is now the standard method of connecting client and server-side without page reload.

I agree with diafol's answer above. PHP runs first on the server then the javascript runs in your browser.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.