我需要 ajax 脚本中的 url 成为 php 文件中的变量,但我不知道该怎么做

I need the url in the ajax script to be a variable from a php file but I don't know how to do this

顶部有变量和表格。该程序根据登录者调用 html 页面。我需要能够根据登录信息拉取页面。

调用页面时,我从 url 中提取页码。我需要在程序底部的 ajax 部分使用该页码从右侧页面中提取数据并将其放入聊天框。

现在我无法拉出正确的页面。我在 ajax url:

中设置变量时遇到问题

我尝试使用“”,但它会将聊天框作为图像加载到聊天框内。

    <?php
    if(isset($_POST['enter'])){
    if($_POST['name'] != ""){
    $_SESSION['name'] = 
    stripslashes(htmlspecialchars($_POST['name']));
    }
    else{
    echo '<span class="error">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”作为一个变量,以便它可以调出几页之一。

您可以将 JavaScript 文件另存为 .php 扩展名或更改您的服务器配置以使用 PHP.

解释给定文件

启用 PHP 处理文件后,您可以照常使用 PHP 中的所有内容。

为确保浏览器以正确的 MIME 类型加载脚本,您可以使用 PHP 进行设置。

<?php

header('Content-Type: application/javascript');

$url = "chat/9999.html";

?>

$.ajax({
    url: "<?= $url ?>",
    // …
})

如果你的代码中没有 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
            }               
        },
});

如果您的 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
    }
  },
});