Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将变量发送到另一个php文件_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript 将变量发送到另一个php文件

Javascript 将变量发送到另一个php文件,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我真的不知道该怎么办。我的问题是PHP变量(或javascript/jQuery)没有通过我创建的AJAX请求发送到另一个PHP文件。好的,我正在创建一个聊天应用程序。它有一个消息文本框和一个发送按钮。单击send按钮时,ajax请求工作,并执行我希望它执行的php文件(我知道这一点,因为它将“:”写入我的文本文件,这是正确的),但我得到:“undefined index:message”,因为message变量没有被发送到php文件!我在这个话题上发现了很多问题,但似乎没有一个对我有帮助,所以

我真的不知道该怎么办。我的问题是PHP变量(或javascript/jQuery)没有通过我创建的AJAX请求发送到另一个PHP文件。好的,我正在创建一个聊天应用程序。它有一个消息文本框和一个发送按钮。单击send按钮时,ajax请求工作,并执行我希望它执行的php文件(我知道这一点,因为它将“:”写入我的文本文件,这是正确的),但我得到:“undefined index:message”,因为message变量没有被发送到php文件!我在这个话题上发现了很多问题,但似乎没有一个对我有帮助,所以我希望你们能找到答案

这是我的test.php文件:

<!doctype html>
<html>
<head>
<script src="jquery.min.js"></script>
<script>
        $(document).ready(function(){
            $("#chat").load("chat.txt");
        });
</script>

<script>    
    $(document).ready(function(){
            $("#send").click(function(){
                    $.ajax({
                    type: 'post',
                    url: 'chat.php',
                    data: '' + message,
                    success: function(response){
                        $("#chat").html(response);
                    }
                });
            });
        });
</script>
</head>

<body>

<?php

    if (isset($_POST['send'])) {
        $message = $_POST['message'];
    }

?>

<style type="text/css">
    #chat {
        width: 194px;
        background-color: #292929;
        color: #493C9E;
        overflow: scroll;
    }
</style>

<pre id="chat">

</pre>

<form method="post">
    Message: <input type="text" id="message" name="message"><br>
    <input type="submit" value="Send" id="send" name="send" onclick="return false;">
</form>
</body>
</html>
我已经尝试将test.php文件包含在chat.php文件中,但没有效果。我还创建了message变量
$message=$\u POST['message']
和消息数据是从test.php文件发送的。我已尝试检查消息变量是否已设置(使用
isset()
)。在test.php文件中,我还尝试通过AJAX创建消息变量,
{message:$message},
。而且似乎什么都不管用!!!!这对我来说真的很难,所以如果有人能帮助我,我会非常感激。 提前很多时间:)

PS:如果你需要任何其他信息,请在下面发表评论,我会给你的。感谢大家在我前面的问题中反应如此之快:)

在ajax调用中添加一个错误函数,看看返回了什么

error: function(msg){alert(msg.status + " " + msg.statusText);}

chat.php
像这样:

<?php
            $file = "chat.txt";
            $handle = fopen($file, "a+");
            fwrite($handle, ": " . $_POST['message'] . "\n");
            echo "\n: " . $_POST['message'];

?>

我发现了几个问题:

  • 将您的
    isset()
    移动到chat.php,而不是test.php内部。如果您有
    $.ajax()
    ,那么为什么需要在chat.php中使用
    isset()
    ,作为获取和处理
    $\u POST[]
    的一部分呢

  • 您缺少
    var
    ,并且ajax不知道要发送哪个字符串

  • 将所有jquery代码片段包装到一个标记中,而不是使用2个
    $(document).ready(function()

  • 我总是将jQuery放在html的底部(就在
    之前,而不是
    内部)

  • 因此,根据我的反馈,以下是根据我的编码方法应该显示的内容:

    test.php

    <!doctype html>
    <html>
    <head>
    <style type="text/css">
        #chat {
            width: 194px;   
            background-color: #292929;
            color: #493C9E;
            overflow: scroll;
        }
    </style>
    
    <script src="jquery.min.js"></script>
    </head>
    <body>
    
    <pre id="chat">
    </pre>
    
    <form id="chatForm" method="post">
        Message: <input type="text" id="message" name="message"><br>
        <input type="submit" value="Send" id="send" name="send" onclick="return false;">
    </form>
    
    <script type="text/javascript">
    /* notice I moved the jQuery script to bottom right before </html> instead of inside    <head> */
    /* combine both of 2 jQuery into same grouping */
    
    $(document).ready(function(){
        $("#chat").load("chat.txt");
    
    /* use form id for ajax to manipulate the data so, I added id="chatForm" in your html form for this one: */
    
            $("#chatForm").submit(function(){ /* <-- use `submit()` so it'd handle the whole form at once */
    var message = $( this ).serialize();  /* <-- this inkove ajax to know to expect string before sending to php */
                    $.ajax({
                    type: 'post',
                    url: 'chat.php',
                    data: ''+ message,
                    success: function(response){
                        $("#chat").html(response);
                    } // end of success
                }); // end of $.ajax()
            }); // end of submit
        });// end of document ready
    </script>
    </body>
    </html>
    
    
    #聊天{
    宽度:194px;
    背景色:#292929;
    颜色:#493C9E;
    溢出:滚动;
    }
    信息:
    /*请注意,我将jQuery脚本移到了右下角的前面,而不是内部*/ /*将两个jQuery组合到同一个分组中*/ $(文档).ready(函数(){ $(“#chat”).load(“chat.txt”); /*使用表单id for ajax来操作数据,因此,我在html表单中添加了id=“chatForm”来处理此数据:*/
    $(“#chatForm”).submit(function(){/*请提供fiddle上的代码这是什么意思?“我还创建了消息变量$message=$\u POST['message'];消息数据是从test.php文件发送的”我的意思是,我创建了一个message变量,它用于在单击按钮时将消息的值保存在文本框中,并将其发送到chat.php文件,以便用于写入文本文件。我应该更清楚一点:)
    message
    从来没有在JS代码中的任何地方定义过。@Fred ii-:是的。
    message
    是一个JS变量,它从来没有定义过,因此代码将随着一个未定义的变量而消亡。即使定义了它,
    data
    参数也没有被正确定义,因此它永远不会在php中出现。它应该是这样的<代码>数据:{name_of_post_var_in_php:'value to assign'}
    。忘了添加此项!我尝试了此项,但什么也没有得到,没有出现警报框,告诉我它应该有什么问题。尝试使用响应向成功函数添加警报,如果调用该方法,您将获得成功或错误。我刚刚做了,这是我收到的一条长消息。以下是链接:I tried response.status,我未定义!您是否使用浏览器调试器检查错误并查看变量中的值?例如,在chrome press F12中。这可能会有所帮助。此外,警报似乎提示了问题所在,消息变量未定义。
    <!doctype html>
    <html>
    <head>
    <style type="text/css">
        #chat {
            width: 194px;   
            background-color: #292929;
            color: #493C9E;
            overflow: scroll;
        }
    </style>
    
    <script src="jquery.min.js"></script>
    </head>
    <body>
    
    <pre id="chat">
    </pre>
    
    <form id="chatForm" method="post">
        Message: <input type="text" id="message" name="message"><br>
        <input type="submit" value="Send" id="send" name="send" onclick="return false;">
    </form>
    
    <script type="text/javascript">
    /* notice I moved the jQuery script to bottom right before </html> instead of inside    <head> */
    /* combine both of 2 jQuery into same grouping */
    
    $(document).ready(function(){
        $("#chat").load("chat.txt");
    
    /* use form id for ajax to manipulate the data so, I added id="chatForm" in your html form for this one: */
    
            $("#chatForm").submit(function(){ /* <-- use `submit()` so it'd handle the whole form at once */
    var message = $( this ).serialize();  /* <-- this inkove ajax to know to expect string before sending to php */
                    $.ajax({
                    type: 'post',
                    url: 'chat.php',
                    data: ''+ message,
                    success: function(response){
                        $("#chat").html(response);
                    } // end of success
                }); // end of $.ajax()
            }); // end of submit
        });// end of document ready
    </script>
    </body>
    </html>
    
    <?php
    
    /* transfer isset() from test.php to this file and use it to wrap your fwrite function */
    
        if (isset($_POST['message'])) { 
            $message = $_POST['message'];
           $file = "chat.txt";
                $handle = fopen($file, "a+");
                fwrite($handle, ": " . $message . "\n");
                echo "\n: " . $message;
        }        
    ?>