Quantcast
Channel: WordPress.org Forums » All Topics
Viewing all articles
Browse latest Browse all 210725

How to make an ajax call with parameters into WordPress site

$
0
0

Replies: 0

I have a simple web with an ajax call with parameters that works fine alone: http://www.colome.org/utils/

This is the sample code:

index.html:


<!DOCTYPE html>
<html lang=en>
<head>
    ...
    <script src="codi.js"></script>
    ...
</head>
<body>
<section id="main">
    <table id="tng" class="datagrid">
        <tbody>
        </tr><tr>
            <td width="50%" align="right"><b>Host or IP:</b></td>
            <td><input  id="ip" size="20" value="" ></td>
            <td><button type="button" onclick="Ping()">Ping</button></td>
        </tr>
        </tbody>
    </table>
  ...
</section>
<h3 id="notification"></h3>
</body>
</html>

I created a function “Ping” inside codi.js that makes the ajax call

codi.js:

function Ping()
{
    $("#image").show();
        var ip=document.getElementById('ip');
    $.ajax({
        type: "POST",
        url: "http://...../ping.php",
        cache: false,
        //contentType: "application/json; charset=utf-8",
        //dataType: "json",
        data: {"ip" : ip.value},
        success: onSuccesPing,
        error: onErrorPing,
        crossDomain:true
    });

}
function onSuccesPing(data,status)
{
   document.getElementById("notification").innerHTML=data;
}
function onErrorPing(data,status)
{
   document.getElementById("notification").innerHTML=data.responseText);
}

And finally the ping.php code, very simple:


<?php
$ip =   $_POST["ip"];
exec("ping -c 3 ".$ip." 2>&1", $output, $status);
foreach ($output as $key => $value) {
    echo ($value.'<br>');
}

?>

I was trying to integrate this code to my wordpress website, like the example below:
https://codex.wordpress.org/AJAX_in_Plugins

but I don’t know how to pass parameters to the ajax call, please can you help me? thanks

  • This topic was modified 5 minutes ago by  colome.

Viewing all articles
Browse latest Browse all 210725

Trending Articles