Passing variables between separate html and php files

Passing variables between separate html and php files

by David Swan -
Number of replies: 8

Hi i hope I may be able to get some more advice on creating my suggestion form, or a point to any tutorials they may help creat a table form mysql where the html file and php file are seperate.


i have two files one containing only html and another with only php. 

in my html file i have a table and i wish to pass into this table the values from a mysql query that is contained within the php file.

all the examples i have looked at have used both html and php in the one file.

at the moment im trying to pass the values using,"<?php echo $name; ?>" but this doesn't seem to be working.

HTML file

<?php include("SubmittedSuggestions.php"); ?>

<div class="container" style="background-image: url('http://www.worldmarkacademy.com/OurWorld/EditorBG.png');

    -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover;

    background-size: repeat; padding-bottom: 5px;">

    <br />

    <div class="mainbody" style="background-image: url('http://www.worldmarkacademy.com/Test/Notepad.png');

        background-repeat: no-repeat; margin-left: auto; margin-right: auto; margin-bottom: 10px;

        width: 646px; height: 800px !important; min-height: 100%; overflow: hidden;">

        <div class="main-form" style="margin-top: 280px; margin-left: 15px;">

        <table>

    <thead>

        <tr>

            <th>Name</th>

            <th>Site</th>

            <th>Status</th>

            <th>Date</th>            

        </tr>

    </thead>

   

            <tbody>   

        <tr>

        <td> <?php echo $name; ?> </td> 

        <td> <?php echo $site; ?> </td>

        <td> <?php echo $status; ?> </td>

        <td> <?php echo $date; ?> </td>  

    </tr>

    </tbody>

</table>           

        </div>

    </div>

</div>




Average of ratings: -
In reply to David Swan

Re: Passing variables between separate html and php files

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

Create it as a function - you can then pass variables to the function when you call it.

http://www.w3schools.com/php/php_functions.asp

In reply to Richard Oelmann

Re: Passing variables between separate html and php files

by David Swan -

Hi Richard thanks for taking time out to answer my question,

I still am unable to pass the variables from with the function to the HTML file, I think this is because the HTML is inside moodles page content, see photo for where I am adding the HTML


i call the variable in my HTML using <?= GetInfo(); ?>

and within my php i use the function 

function GetInfo()

{

$testname = "BLEGH";

echo $testname;

}



content page

is it even possible to pass values over when the HTML is contained with the page content like this, 


any advice appreciated.

In reply to David Swan

Re: Passing variables between separate html and php files

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

No, you can't execute PHP commands embedded in HTML entered into a Moodle text editor (the level of security issues that would cause would be just horrible).

The only place you can put PHP scripts that will execute is within the Moodle code directory.

On the other hand, what you are trying to do may be possible by using the Generico filter - https://moodle.org/plugins/view/filter_generico

That allows you to (amongst many other features), use a template format to insert user profile fields into a HTML page. It is also (I believe) extensible to allow replacing of other fields.


In reply to Davo Smith

Re: Passing variables between separate html and php files

by David Swan -

Thanks Davo im going over Generico just now, looks prominsing

In reply to David Swan

Re: Passing variables between separate html and php files

by David Swan -

Seems its only available up to moodle version 2.8 im using 2.9 and get a syntax error trying to access site administration after installing it.

In reply to David Swan

Re: Passing variables between separate html and php files

by Michael Milette -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Hi David,

As previously mentioned, you can't do PHP from an HTML file. The simplest solution, if you can do it, is to simply change the filename extension to .php.

If you can't do that, the other option is to use your HTML file as a kind of template:

  1. Put some kind of placeholders in your HTML file like [name] or {name} or %name%. The idea is to choose a syntax which would be very unlikely to be typed in by someone by accident in normal text or used by some other plugin. So for example, "name" would probably not be a good choice.
  2. Next, from your PHP file, have it use the PHP readfile() function to read the contents of your HTML file into a variable.
  3. Use the PHP str_replace() function to replace the placeholders with the desired content.
  4. Finally, have PHP echo the contents of the variable.

This is a simplified version of what the Generico plugin does.

You can create a Filter plugin if you want to make the placeholders work throughout the site or at least where text is processed through the format_string() or format_text() functions.

Hope this helps.

Best regards,

Michael Milette

In reply to David Swan

Re: Passing variables between separate html and php files

by Justin Hunt -
Picture of Particularly helpful Moodlers Picture of Plugin developers
The other way you might do this, is to pass the information that you want into the html page as url parameters and then pick them up and poke them into the fields you want using javascript.

You can pick up url params from javascript. It gets a bit funky, but if you really can't use php for this page (are you sure?) then this is how I would do it.
http://stackoverflow.com/questions/979975/how-to-get-the-value-from-the-url-parameter

(I have tested Generico on Moodle 2.9 and it is working so far. But the generico filter won't work in an html page, as has earlier been said.)


In reply to Justin Hunt

Re: Passing variables between separate html and php files

by David Swan -

Hi Guys thanks for the advice, I'll be working on this today.  I'll give both of the approaches a try and I'll let you know how I get on, Who would have thought a simple suggestion box would be so complicated.