How to make an ajax call with parameters into WordPress site
-
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_Pluginsbut I don’t know how to pass parameters to the ajax call, please can you help me? thanks
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
The topic ‘How to make an ajax call with parameters into WordPress site’ is closed to new replies.