頂部有變數和表格。該程式根據誰登錄呼叫一個 html 頁面。我需要能夠根據登錄資訊拉取頁面。
呼叫頁面時,我從 url 中提取頁碼。我需要在底部程式的 ajax 部分中使用該頁碼來將資料從右側頁面中提取出來并將其放入聊天框中。
現在我無法拉正確的頁面。我在 ajax url 中設定變數時遇到問題:
我嘗試使用“”,但它會將聊天框內的聊天框作為影像加載。
<?php
if(isset($_POST['enter'])){
if($_POST['name'] != ""){
$_SESSION['name'] =
stripslashes(htmlspecialchars($_POST['name']));
}
else{
echo '<span >Please type in a name</span>';
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Chat - Customer Module</title>
<link type="text/css" rel="stylesheet" href="style1.css" />
</head>
<body>
<?php
if(!isset($_SESSION['name'])){
loginForm();
}
else{
?>
<div id="wrapper">
<div id="menu">
<p class="welcome">Welcome, <b><?php echo $_SESSION['name']; ?>
</b></p>
<p class="logout"><a id="exit" href="#">Exit Chat</a></p>
<div style="clear:both"></div>
</div>
<div id="chatbox"><?php
if(file_exists($log) && filesize($log) > 0){
$handle = fopen($log, "r");
$contents = fread($handle, filesize($log));
fclose($handle);
echo $contents;
}
else{
if(!isset($log));
$contents = 'Welcome!';
file_put_contents($log, $contents);
echo $contents;
}
?>
</div>
<form name="message" action="">
<input name="usermsg" type="text" id="usermsg" size="63" />
<input name="submitmsg" type="submit" id="submitmsg"
value="Send" />
</form>
<br />
<form action="upload.php" method="post" enctype="multipart/form-
data">
Select image to upload:
<input type="file" name="fileToUpload" />
<input type="submit" value="Upload Image" name="submit" />
</form>
</div>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js">
</script>
<script type="text/javascript">
// jQuery Document
$(document).ready(function(){
//If user submits the form
$("#submitmsg").click(function(){
var clientmsg = $("#usermsg").val();
$.post("post.php", {text: clientmsg});
$("#usermsg").attr("value", "");
return false;
});
//Load the file containing the chat log
function loadLog(){
var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
$.ajax({
url: "chat/9999.html",
cache: false,
success: function(html){
$("#chatbox").html(html); //Insert chat log into the #chatbox div
var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
if(newscrollHeight > oldscrollHeight){
$("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal');
//Autoscroll to bottom of div
}
},
});
}
setInterval (loadLog, 2500); //Reload file every 2.5 seconds
//If user wants to end session
$("#exit").click(function(){
var exit = confirm("Are you sure you want to end the
session?");
if(exit==true){window.location = 'index.php?logout=true';}
});
});
</script>
我已經更新為您提供更多代碼。我嘗試使用“”,但表格不正確。它會再次在表單中加載表單。
我需要將“chat/9999.html”作為一個變數,以便它可以調出幾個頁面之一。
uj5u.com熱心網友回復:
如果您的 ajax 代碼在 JS 檔案中,可以使用隱藏輸入使 URL 可用,但在頁面上不可見,然后從 JS 查詢輸入的值。
// index.php
<?php
// Get URL
$url = 'chat/9999.html';
?>
<input id="url-from-php" type="hidden" value="<?= $url ?>">
// script.js
$.ajax({
url: document.getElementById('url-from-php').value,
cache: false,
success: function (html) {
$("#chatbox").html(html); //Insert chat log into the #chatbox div
var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
if (newscrollHeight > oldscrollHeight) {
$("#chatbox").animate({ scrollTop: newscrollHeight }, "normal"); //Autoscroll to bottom of div
}
},
});
uj5u.com熱心網友回復:
如果沒有代碼的 PHP 部分,很難知道它的外觀,但根據我的經驗,如果您混合使用 php 和 html/javascript/jquery/ajax 代碼,這樣的事情會起作用:
<?php
//this is where you list your urls (array, user input, db, etc.)
$url = 'chat/9999.html';
?>
$.ajax({
url: "<?=$url?>",
cache: false,
success: function(html){
$("#chatbox").html(html); //Insert chat log into the #chatbox div
var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
if(newscrollHeight > oldscrollHeight){
$("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal');
//Autoscroll to bottom of div
}
},
});
uj5u.com熱心網友回復:
您可以將 JavaScript 檔案保存為.php擴展名或更改服務器配置以使用 PHP 解釋給定檔案。
一旦啟用 PHP 來處理檔案,您就可以像往常一樣使用 PHP 中的所有內容。
為確保瀏覽器以正確的 MIME 型別加載腳本,您可以使用 PHP 進行設定。
<?php
header('Content-Type: application/javascript');
$url = "chat/9999.html";
?>
$.ajax({
url: "<?= $url ?>",
// …
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/424085.html
上一篇:在一頁Jquery中發送多個資料
下一篇:vannilajs的微光效果
