UFOman
UFOman

Reputation:

Calling PHP in html page

Let say, i can only use html codes in a page ( call it index.html ).

So now, i want to check something using php ( call it userCheck.php ).

So it is possible i want the html to communicate with php files.

For example, something like

when users access index.html, it will check the userCheck.php, so userCheck says yes, then can proceed view it or else it will redirect to userCheck.php.

Is it possible something like using javascript or ajax? I'm newbie on those two.

Thank you

Upvotes: 5

Views: 22207

Answers (6)

lo_fye
lo_fye

Reputation: 6840

UFOman said "server allows php but because the program i used can only output html pages" It sounds to me like what you really need is to understand how to combine html pages with php code. The simplest (yet proper) way to do what you want is like this:

1) Using Notepad or TextEdit, open the HTML file your program output
2) Select All and Copy the contents
3) Create a file called yourPageNameTemplate.php and Paste into it. Pretend it looks like this:

<html> 
<body>
   <form method="post">
      <input type="text" name="user_name" value="" />
      <input type="submit" name="submit" value="submit" />
   </form>
</body>
</html>

4) Create a file called yourPageName.php and put something like this in it:

<?php 

if(isset($_POST['submit']) && isset($_POST['user_name']))
{
   echo 'Thanks for submitting the form!';
}
else
{
   include 'yourPageNameTemplate.php';
}

?>

That should work just fine. Now, if you want to add some error checking, an an error message to your Template, we can do that quite simply.

5) Edit yourPageNameTemplate.php like this:

<html> 
<body>
   <?=$form_error_message?>
   <form method="post">
      <input type="text" name="user_name" value="<?=$form_user_name?>" />
      <input type="submit" name="submit" value="submit" />
   </form>
</body>
</html>

6) Edit yourPageName.php like this:

<?php 

$form_error_message = '';
$form_user_name = '';
if(isset($_POST['user_name']) && strlen($_POST['user_name']) < 5)
{
   $form_error_message = 'User Name must be at least 5 characters long';
   $form_user_name = $_POST['user_name'];
}

if(isset($_POST['submit']) && empty($form_error_message))
{
   echo 'Thanks for submitting the form!';
   //it would be better to include a full 'thank you' template here, instead of echoing.
}
else
{
   include 'yourPageNameTemplate.php';
}

?>

Upvotes: 0

user50049
user50049

Reputation:

You want to set up an Apache Handler so that files with a .html extension are parsed with php. Do this via a simple .htaccess file located in your web root:

AddHandler application/x-httpd-php .html

You can now use PHP inside of the .html pages. No need for additional AJAX hackery, or dealing with NoScript users or JS feeble browsers.

Upvotes: 2

miku
miku

Reputation: 187994

You can use javascript to fetch some data into your html from your php.

Example using jQuery (put this in your html):

<script type="text/javascript" charset="utf-8"> <!-- javascript in html -->
    $(document).ready(function(){ // start here, if DOM is loaded

    // request data from php script ...
    // we expect the userCheck.php script to actually 'return' something, 
    // some data ...
    $.get("userCheck.php", function(data){
            alert("Data Loaded: " + data);
    });

    // or with some params to the php
    // e.g. userCheck.php can handle username and favcolor parameters
    $.get("userCheck.php", 
            {"username" : "lazy", "favcolor" : "FFFFFF" },          
            function(data){ alert("Data Loaded: " + data);
    });



});
</script>

If this is all completely new to you, please do yourself a favor and read some introductional books, which will give you valueable insights - some random book tips from O'Reilly, Google Tech Talk on jQuery

Upvotes: 3

Zoe
Zoe

Reputation: 2169

JavaScript can be used to call and communicate with a HTML page via JSON.

Here's an example using JQuery and the select-chain plug-in to populate a drop-down box based on the selection of a previous drop-down..

HTML page:

$('#selectbox1').selectChain({
        target: $('#selectbox2'),
        url: 'selectlist.php', 
        data: { ajax: true }
    }).trigger('change');

selectlist.php

if (@$_REQUEST['ajax']) {
$json = array();
    if ((isset($_REQUEST['selectbox1'])) && ($_REQUEST['selectbox1'] != '')){   
        $results = mysql_query('SELECT * FROM table WHERE id="'.$_REQUEST['selectbox1'].'"') or die ("Error: ".mysql_error());
        while($row = mysql_fetch_array($results)) {
            $json[] = '{"id" : "' . $row['id'] . '", "label" : "' . $row['name'] . '"}';
        }
    }
    echo '[' . implode(',', $json) . ']';
}

Upvotes: 0

Krzysztof Bujniewicz
Krzysztof Bujniewicz

Reputation: 2417

If your server doesn't allow php files, then you cannot do it directly. You may want to place your php file on another server, supporting php, and call it with ajax, though. But you must make sure that your php server provider allows that.

Upvotes: 1

carillonator
carillonator

Reputation: 4743

Not sure exactly what you're after, but can you just name your page index.php, write all your html in it, and where needed, insert your PHP code as <?php ...code... ?> ? The PHP runs server-side and can conditionally present different HTML depending on whatever your factors are.

Upvotes: 0

Related Questions