PShickle
PShickle

Reputation: 85

Pass PHP array to HTML Page.

What I want to be able to is on button click I want to check a Server-Side directory for the existence of certain files.

If they exist I wish to display a checkbox corresponding to that file.

So far I have managed to use PHP and AJAX to check if the files exist or not and write to and array: 1 if the file exists, and a 0 if not.

Now...what I need to do at this stage is call upon this array, from my PHP file, or write this array to a div on my main HTML page. However when I go to echo it navigates away from my Main.html, opening a new page and writing on that.

Big question is can I write the array on my main html page from my PHP_Function.php file.

I have the following HTML code:

    <form action="PHP_Function.php">
        <input type="submit" class="learnButton" name="insert" value="Find Available Evidence" />   

    </form>


    <form action="available_evidence">
        <input type="checkbox" name="vehicle" value="Bike"> Facebook<br>
        <input type="checkbox" name="vehicle" value="Car" checked> Facebook Messenger<br>
        <input type="checkbox" name="vehicle" value="Bike"> Twitter<br>
    </form>

With the following array within PHP_function.php file:

if(in_array("Facebook.xml", $dirArray)){


    $IfPresentArray[0]="1";

    }else {

    $IfPresentArray[0]="0";
}
foreach ($IfPresentArray as $value) {

    echo "$value<br />\n";
}

I am very new to PHP and HTML, and I have been banging my head off the wall with this for a while now.

Any help would be greatly appreciated.

Upvotes: 1

Views: 3341

Answers (2)

Peter Bowers
Peter Bowers

Reputation: 3093

You are using AJAX so all your communication will need to go back and forth over that medium. In brief, your PHP script (the one called by ajax) will echo JSON values (typically created by json_encode($myarray)) and this will then be available to your javascript over on your client-side.

A quick google for "jquery ajax json example" should get you some good, helpful ideas of how it fits together.

Here's the client-side from one of those google links above - note the .done part

<script type="text/javascript">
$(document).ready(function(){
  $(':submit').on('click', function() { // This event fires when a button is clicked
    var button = $(this).val();
    $.ajax({ // ajax call starts
      url: 'serverside.php', // JQuery loads serverside.php
      data: 'button=' + $(this).val(), // Send value of the clicked button
      dataType: 'json', // Choosing a JSON datatype
    })
    .done(function(data) { // Variable data contains the data we get from serverside
      $('#wines').html(''); // Clear #wines div

      if (button == 'all') { // If clicked buttons value is all, we post every wine
        for (var i in data.red) {
          $('#wines').append('Red wine: ' + data.red[i] + '<br/>');
        }
        for (var i in data.white) {
          $('#wines').append('White wine: ' + data.white[i] + '<br/>');
        }
      }
      else if (button == 'red') { // If clicked buttons value is red, we post only red wines
        for (var i in data) {
          $('#wines').append('Red wine: ' + data[i] + '<br/>');
        }
      }
      else if (button == 'white') { // If clicked buttons value is white, we post only white wines
        for (var i in data) {
          $('#wines').append('White wine: ' + data[i] + '<br/>');
        }
      }
    });
    return false; // keeps the page from not refreshing 
  });
});
</script>

Upvotes: 1

user3574640
user3574640

Reputation:

You can store your array inside a $_SESSION variable, but you'll have to use a session_start(); on both pages.

On your php page, use: $_SESSION['array'] = $array; and then on your main page retrieve the array by the inverse: $array = $_SESSION['array'];.

You can now use said array on your main page. To retrieve a specific value: $value1 = $_SESSION['array'][0];

For further reference:

Array as session variable

Upvotes: 0

Related Questions