Passing variables between separate html and php files

Passing variables between separate html and php files

от David Swan -
Количество ответов: 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>




В ответ на David Swan

Re: Passing variables between separate html and php files

от Richard Oelmann -
Изображение пользователя Core developers Изображение пользователя Plugin developers Изображение пользователя 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

В ответ на Richard Oelmann

Re: Passing variables between separate html and php files

от 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.

В ответ на David Swan

Re: Passing variables between separate html and php files

от Davo Smith -
Изображение пользователя Core developers Изображение пользователя Particularly helpful Moodlers Изображение пользователя Peer reviewers Изображение пользователя 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.


В ответ на Davo Smith

Re: Passing variables between separate html and php files

от David Swan -

Thanks Davo im going over Generico just now, looks prominsing

В ответ на David Swan

Re: Passing variables between separate html and php files

от Michael Milette -
Изображение пользователя Core developers Изображение пользователя Documentation writers Изображение пользователя Particularly helpful Moodlers Изображение пользователя Plugin developers Изображение пользователя Testers Изображение пользователя 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

В ответ на David Swan

Re: Passing variables between separate html and php files

от Justin Hunt -
Изображение пользователя Particularly helpful Moodlers Изображение пользователя 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.)