将变量从 php 文件发送到 jquery 文件


Sending Variable from php file to jquery file

我正在尝试将位于php文件中的php变量传递给单独的JavaScript文件。 我正在尝试将输入函数中 php 文件末尾的变量传递到名为 $message$username 的 jQuery 变量中。

这是我目前所处的位置:

聊天.php

<?php
    //form data
    $username = $_POST['username'];
    $password = $_POST['password'];
    //sql server connection credentials
    $servername = "localhost";
    $serverusername = "suser11";
    $serverpassword = "suser11";
    $databasename = "chat_database";
    // Create connection
    $conn = new mysqli($servername, $serverusername, $serverpassword, $databasename);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    echo "Connected successfully<br /><br />";
    $username = $conn->real_escape_string($username);
    $password = $conn->real_escape_string($password);
    $sql = "SELECT Salt FROM users WHERE Username='$username'";
    $result = $conn->query($sql);
    while($row = $result->fetch_assoc()) {
        $salt = $row["Salt"];
    }
    $sql = "SELECT * FROM users WHERE Username='$username' AND Password=MD5('$password$salt')";
    $result = $conn->query($sql);
    if($result->num_rows === 0) {
        $conn->close(); //close the db connection
        header('Location: login.html'); //redirect to login.html
    } else {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo "UserID: " . $row["user_id"]. " - Name: " . $row["username"]. "<br />";
        }
    }
    // Close the database connection
    $conn->close();
?>
<textarea id="myChat" type="text" style="width:500px; height:500px;"></textarea>
<br/>
<br/>
<input id="myText" name="myText"/>
<input type="hidden" value="<?php echo $username; ?>"/>
<button id="add">
    <b>Add to chat</b>
</button>
</body>
</html>

这是我的jquery文件

$(document).ready(function() { //start 1
    //alert("hello world, jQuery is working");
    setInterval(function(){$("#myChat").load("chat.txt")},100); //update the textarea with the text file contents every 10th of a second
    var $message = '';
    var $username = ''; 
    $('#add').click(function(){ // start 2
        var $message = $('#myText').val();
        var $username = $('$username').val();
        //alert("Got the message");
        $.ajax({ // start 3
            type: "POST",
            url:'myprocess.php',
            data:{'xml': $xmlString},
            dataType:'text/xml',
            //success: function(r){ // start 4
                //alert('Got it');
            //}, // end 4
            //error: function (xhr, desc, err) {
                //console.log(xhr);
                //console.log("Details: " + desc + "'nError: " + err);
                //alert ("Error: " + err);
            //}
        }); // end 3
        $('#myText').val(""); //clears value of text box on click of button with id=add
        }); // end 2
    //forming proper xml
    $xmlString ='<?xml version="1.0" encoding="ISO-8859-1"?>';
    $xmlString += '<message><user>' + username + '</user><text>' + $message + '</text></message>';  
}); // end 1

在 php 中,你可以像这样传递:只需替换您的代码:

// output data of each row
        while($row = $result->fetch_assoc()) {
            echo "UserID: " . $row["user_id"]. " - Name: " . $row["username"]. "<br />";
        }

使用以下代码:

while($row = $result->fetch_assoc()) {
   echo $row["user_id"]."||".$row["username"];
}

在阿贾克斯中:

$.ajax({ 
       type: "POST",
       url:'myprocess.php',
       data:{'xml': $xmlString},
       dataType:'text/xml',
       success: function(data){ 
              details = data.split("||");
              username = details[0];
              password = details[1];
       }
});