The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Help with a Link Ping Onclick event

Okay, I have an oddball requirment for a one time use scenario.

Expected functionality is:

When a nav link (not all the links on the page) is clicked and goes to another machine, we want to ping the machine. If it pings properly, go to the link. If not go to an alternate link, probably a screen shot.

Basically, I see that I need to trigger an onclick javascript function that then triggers a php system command to ping the href with a single packet

PHP Code:

<? system("ping -n 1 $href"); ?>

where 1 is the number of packets and $host is the href that needs to be pinged.

Can anyone point me in the right direction as to how to assemble this code and make php and javascript live together...

<script type="text/javascript"><!--
/* The variable http will hold our new XMLHttpRequest object. */
function createRequestObject(){
var request_o; //declare the variable to hold the object.
var browser = navigator.appName; //find the browser name
if(browser == "Microsoft Internet Explorer"){
/* Create the object using MSIE's method */
request_o = new ActiveXObject("Microsoft.XMLHTTP");
}else{
/* Create the object using other browser's method */
request_o = new XMLHttpRequest();
}
return request_o; //return the object
}
/* You can get more specific with version information by using
parseInt(navigator.appVersion)
Which will extract an integer value containing the version
of the browser being used.
*/
var http = createRequestObject();
/* Function called to get the product categories list */
function getProducts(){
/* Create the request. The first argument to the open function is the method (POST/GET),
and the second argument is the url...
document contains references to all items on the page
We can reference document.form_category_select.select_category_select and we will
be referencing the dropdown list. The selectedIndex property will give us the
index of the selected item.
*/
//grab the link to check
var thisLink = document.getElementById("_link");
//thisLink = 'www.atex.com';
if (thisLink == 'http://serverbase:9080/aoa') {
thisServer = 'serverbase';
}
http.open('get', 'pingme.php?href=' + thisServer);
/* Define a function to call once a response has been received. This will be our
handleProductCategories function that we define below. */
http.onreadystatechange = handleProducts;
/* Send the data. We use something other than null when we are sending using the POST
method. */
http.send(null);
}
/* Function called to handle the list that was returned from the internal_request.php file.. */
function handleProducts(){
/* Make sure that the transaction has finished. The XMLHttpRequest object
has a property called readyState with several states:
0: Uninitialized
1: Loading
2: Loaded
3: Interactive
4: Finished */
if(http.readyState == 4){ //Finished loading the response
/* We have got the response from the server-side script,
let's see just what it was. using the responseText property of
the XMLHttpRequest object. */
var response = http.responseText;
/* And now we want to change the product_categories <div> content.
we do this using an ability to get/change the content of a page element
that we can find: innerHTML. */
alert(response);
document.getElementById('product_cage').innerHTML = response;
}
}
// -->
</script>

Don't treat the return value as if it is a function - Ajax wants the actual output of the page, so use echo "true" or echo "false" instead. That will then be in the responseText of the XMLHttpRequest object.

The ajax bit is now opening a new window with the href if it's open, as well as creating an alert telling us it's up. If not, then it sends us to an alternate location, and tells us it's down.

Code:

<script type="text/javascript"><!--
/* The variable http will hold our new XMLHttpRequest object. */
function createRequestObject(){
var request_o; //declare the variable to hold the object.
var browser = navigator.appName; //find the browser name
if(browser == "Microsoft Internet Explorer"){
/* Create the object using MSIE's method */
request_o = new ActiveXObject("Microsoft.XMLHTTP");
}else{
/* Create the object using other browser's method */
request_o = new XMLHttpRequest();
}
return request_o; //return the object
}
/* You can get more specific with version information by using
parseInt(navigator.appVersion)
Which will extract an integer value containing the version
of the browser being used.
*/
var http = createRequestObject();
/* Function called to get the product categories list */
function getProducts(){
/* Create the request. The first argument to the open function is the method (POST/GET),
and the second argument is the url...
document contains references to all items on the page
We can reference document.form_category_select.select_category_select and we will
be referencing the dropdown list. The selectedIndex property will give us the
index of the selected item.
*/
//grab the link to check
var thisLink = document.getElementById("_link");
//thisLink = 'www.atex.com';
if (thisLink == 'http://serverbase:9080/aoa') {
thisServer = 'serverbase';
thisAltHref = 'www.yahoo.com';
}
if (thisLink == 'http://another.server.somewhere/index.php') {
thisServer = 'another.server.somewhere';
thisAltHref = 'www.yahoo.com';
}
//document.write(thisLink);
http.open('get', 'pingme.php?href=' + thisServer);
/* Define a function to call once a response has been received. This will be our
handleProductCategories function that we define below. */
http.onreadystatechange = handleProducts;
/* Send the data. We use something other than null when we are sending using the POST
method. */
http.send(null);
}
/* Function called to handle the list that was returned from the internal_request.php file.. */
function handleProducts(){
/* Make sure that the transaction has finished. The XMLHttpRequest object
has a property called readyState with several states:
0: Uninitialized
1: Loading
2: Loaded
3: Interactive
4: Finished */
if(http.readyState == 4){ //Finished loading the response
/* We have got the response from the server-side script,
let's see just what it was. using the responseText property of
the XMLHttpRequest object. */
var response = http.responseText;
/* And now we want to change the product_categories <div> content.
we do this using an ability to get/change the content of a page element
that we can find: innerHTML. */
var is_live=response.indexOf('live=true');
var thisLink = document.getElementById("_link");
if (is_live != -1) {
alert(thisLink + " is Up - Web Service Availability is Unconfirmed.");
window.open(thisLink,'jav','width=300,height=200,resizable=yes');
}
else {
alert(thisLink + ' This Server is not online at this time.');
window.open(thisAltHref,'jav','width=300,height=200,resizable=yes');
}
//alert(response);
}
}
// -->
</script>