Calling a .php file in the onclick() event
I want to call a .php file from the OnClick event.
<i开发者_StackOverflow中文版nput 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.
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';
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)
"...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.
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).
function call_php_program(code) {
$.get("/php/php_program.php?code=" + code);
return false;
}
onclick="call_php_program('<? echo $code; ?>')"
精彩评论