-1

I have this script in a separate file as my index.php.

I want to have a button that if pressed shows the results from my PHP script in the index.php, but when I use

<form action="../inc/script.php" method="post">
    <input type="submit" value="Scan">
</form>

it just goes to the script.php page. how can I change it so it stays on index.php and print_r the array from the script on the index.php page?

harmstyler
  • 1,381
  • 11
  • 20
Malzatic
  • 41
  • 6

2 Answers2

3

The form action tells the browser to post the form results to that page. If you want to stay on the current page you'll need to remove the action. Then, you'll have to check the post and include the results within the current page.

<form method="post">
    <input type="submit" name="Scan" value="Scan">
</form>

<?php 
    if (isset($_POST['Scan'])) { 
        include '../inc/script.php';
    } 
?>
harmstyler
  • 1,381
  • 11
  • 20
0

One way to solve this problem is through using JavaScript, and specifically jQuery in my example.

What you would do is set up a POST request to the PHP document, that would return the print_r result. You also won't need a form in this instance, more than a button.

HTML:

<button type="button" id="scan"></button>

AND

<div id="returned-data"></div>

so you have a place to put the data when you get it back.

JS/jQuery:

$(document).ready(function(){
    $('#scan').click(function(){
        $.post('../inc/script.php').done(function(data){
            $('#returned-data').html(data);
        });
    });
});

PHP (../inc/script.php):

<?php
    //do something with your data, and finish with:
    print_r($data);
?>
Ryan
  • 481
  • 2
  • 10