我无法将变量从我的jQuery代码传递到我的PHP代码


I can't pass a variable from my jQuery code to my PHP code

我正在尝试使用 AJAX 和 POST 将变量从我的 jQuery 代码传递到我的 HTML/PHP 代码,但我收到错误消息"注意:未定义的索引:C:''xampp''htdocs''teszt''test1.php 中的 testData 在第 9 行"。

我正在使用

XAMPP,我在localhost中运行代码,我正在使用Mozilla Firefox,这是我的HTML/PHP代码(test1.php(:

<!DOCTYPE html>
<html lang="hu">
    <head>
        <title>Test</title>
        <meta http-equiv="Content-type" content="text/html;charset=utf-8">
    </head>
    <body>
        <?php echo "<p class='testParagraph'>" . $_POST['testData'] . "</p>";?>
        <script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
        <script type="text/javascript" src="script.js"></script>
    </body>
</html>

这是我的jQuery代码(script1.js(:

$(document).ready(function() {
   var temporaryVariable = "temporary variable";
   $.ajax({
        type: "POST",
        url: "test1.php",
        data: { testData:"2" },
        success: function (result) {
            alert('success');
        }
      }).done(function() {
        $('.testParagraph').addClass( temporaryVariable );
        });
});

到目前为止,我尝试更改的内容(但当然不起作用(:

测试1.php:

  1. 查塞特以前曾iso-8859-2
  2. GET而不是两个代码中的POST
  3. 注释掉script标记包括

脚本1.js:

  1. 注释掉$(document).ready(function() {...
  2. data:我尝试将报价符号从 ' 更改为 " 或根本没有报价符号
  3. 注释掉success: function...行及其下方的两行

另外,当我运行PHP代码时,p标签从jQuery代码中获取temporaryVariable类。

不过,我还是收到了上面写的错误消息。我将不胜感激我得到的任何帮助。

将测试1.php更改为:

<!DOCTYPE html>
<html lang="hu">
    <head>
        <title>Test</title>
        <meta http-equiv="Content-type" content="text/html;charset=utf-8">
    </head>
    <body>
        <p id='testParagraph'>&nbsp;</p> <!-- You receive the value by jquery not from PHP. -->
        <script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
        <script type="text/javascript" src="script.js"></script>
    </body>
</html>

而您的脚本1.js:

$(document).ready(function() {
   var temporaryVariable = "temporary variable";
   $.ajax({
        type: "POST",
        url: "test1.php",
        data: { testData:"2" },
        success: function (result) {
            alert('success');
        }
      }).done(function() {
        $('#testParagraph').html(temporaryVariable); //The value is inserted into the paragraph.
        });
});

希望对你有用。

除非您从 post 请求中获取 php 页面,否则使用 $_POST['testData'] 将毫无用处,并且总是会给出未找到索引的错误。如果你想在 jquery 的 p 标签中设置值,你可以使用 $('.testParagraph').html( temporaryVariable ) 而不是.addClass

使用数据:"testData=2",

而不是数据:{testData:"2"},

注意:未定义的索引:C:''xampp''htdocs''teszt''test1中的testData.php 在第 9 行

表示您的

    data: { testData:"2" },

是错误的。

没有测试它,但这应该有效:

    data: [ testData:"2" ],

因为 $_POST 是一个数组

听起来

你是在发布test1.php给自己,目前还不清楚你为什么要这样做。

正如@Rasclatt在评论中指出的那样,在您尝试访问数据之前,请检查数据是否存在。

改变:

<?php echo "<p class='testParagraph'>" . $_POST['testData'] . "</p>";?>

自:

<?php
if( isset( $_POST['testData'] ) ) {
    echo "<p class='testParagraph'>" . $_POST['testData'] . "</p>";
} 
?>

但是,如果test1.php是完全不同的脚本,则进行以下更改:

在 HTML 中包含 ajax 内容的占位符:

<p class="tetParagraph"></p>

修剪并编辑您的 JavaScript 以使其如下所示:

$(document).ready(function() {
   var tempVal = "temporary variable";
   $.ajax({
        type: "POST",
        url: "test1.php",
        data: { testData:"2" },
        success: function (result) {
            tempVal = result;
            $('.testParagraph').html( tempVal );
            alert('success');
        }
   });
});