0

I made a PHP code that tracks packages by getting information from a JSON file and I turned it as a Wordpress plugin. but every time I search the outcome appears on the top of the page.hw can I make it appear where I need it to?

https://i.stack.imgur.com/bERlS.jpg

<?php

    $url = "a url where i get my json fille";
    $code = $_POST[code];
    $data = file_get_contents("$url$code");
    $json = json_decode($data);

    echo "<table class=\"table\">";
    foreach ($json as $key => $row) {
        echo "<tr>";
        foreach ($row as $key0 => $row0) {
            echo "<th>" . $key0 . " : " . $row0 . "</th>";
        }
        echo "</tr>";
    }
    echo "</table>";
?>
Koala Yeung
  • 7,475
  • 3
  • 30
  • 50
Geri Boja
  • 23
  • 5

2 Answers2

0

Try to make an Ajax call and load the result to a response div.

Check here for some ideas How to call ajax in wordpress

You can replace console.log(msg); with jQuery('#response_div').html(msg);

Rizwan
  • 1
  • 1
0

The simplest way is to use shortcodes.

  1. Firstly, wrap your code in a function
// NB: you can name your function anything
function trackingplugin_print_table(){
    // Not part of your code, but add it just in case
    // This simply checks if user submitted anything
    if( ! $_POST['code'] ) {
       return;
    }
    $url = "a url where i get my json fille";
    $code = $_POST[code];
    $data = file_get_contents("$url$code");
    $json = json_decode($data);

    echo "<table class=\"table\">";
    foreach ($json as $key => $row) {
        echo "<tr>";
        foreach ($row as $key0 => $row0) {
            echo "<th>" . $key0 . " : " . $row0 . "</th>";
        }
        echo "</tr>";
    }
    echo "</table>";
}
  1. Then add the shortcode to WordPress More Info Here
// NB: you can also name your shortcode whatever you want
add_shortcode( 'trackingplugin', 'trackingplugin_print_table' );
  1. Then use the shortcode [trackingplugin] wherever you want. In this case it would be after the form.
Thapedict
  • 363
  • 2
  • 9