Html sayfası PHP çağırıyor

6 Cevap php

Ben sadece (index.html diyelim) bir sayfa html kodlarını kullanabilirsiniz, diyelim.

Yani şimdi, ben (o userCheck.php diyoruz) php kullanarak bir şeyler kontrol etmek istiyorum.

Bu yüzden ben html php dosyaları ile iletişim kurmak isteyen mümkündür.

Örneğin, gibi bir şey

Kullanıcıların index.html eriştiğinizde, bu userCheck.php kontrol edecek, böylece userCheck evet, o görünümü onu devam edebilir veya başka userCheck.php yönlendirme söyledi.

Bu javascript veya ajax kullanarak gibi olası bir şey mi? Ben o iki acemi olduğum.

Teşekkür ederim

6 Cevap

Eğer php html içine bazı verileri almak için javascript kullanabilirsiniz.

Kullanarak Örnek jQuery (HTML'nizdeki koymak):

<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>

Bu sizin için tüm tamamen yeni ise, kendinize bir iyilik yapın ve size valueable anlayışlar verecek bazı Kazandırma kitapları, lütfen okuyun - some random book tips from O'Reilly, Google Tech Talk on jQuery

Bir. Html uzantılı dosyaları php ile ayrıştırılır böylece bir Apache Handler kurmak istiyorum. Web kökünde bulunan basit bir .htaccess dosyası ile bunu:

AddHandler application/x-httpd-php .html

Sen şimdi. Html sayfaları içinde PHP kullanabilirsiniz. Ek AJAX hackery veya NoScript kullanıcılar veya JS zayıf tarayıcılar ile başa çıkmak için gerek yok.

Sunucu php dosyalarını izin vermezse, o zaman doğrudan bunu yapamazsınız. Sen php destekleyen, başka bir sunucu üzerinde php dosyasını yerleştirmek istediğiniz, ve olsa da, ajax ile çağırabilir. Ama sizin php sunucu sağlayıcınız izin verdiğinden emin olmanız gerekir.

Sonra konum, ama sadece sayfa index.php adını tam olarak ne olduğundan emin, içinde tüm html yazmak ve gerektiğinde, gibi PHP kodu sokmayın <?php ...code... ?>? PHP sunucu tarafında çalışan ve şartlı sizin faktörler ne olursa olsun bağlı olarak farklı HTML sunabilir.

JavaScript JSON yoluyla bir HTML sayfası çağrı ve iletişim kurmak için kullanılabilir.

Burada JQuery ve bir önceki açılan seçimine dayalı açılan kutuyu doldurmak için select-zincir plug-in kullanarak bir örnek ..

HTML sayfası:

$('#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) . ']';
}

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) yourPageName.php adında bir dosya oluşturun ve ona böyle bir şey koymak:

<?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.

Bunun gibi 5) Düzenleme yourPageNameTemplate.php:

<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>

Bunun gibi 6) Düzenleme yourPageName.php:

<?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';
}

?>