简体   繁体   English

通过 AJAX 将 javascript 变量传递给 PHP 变量的问题

[英]Problem with passing javascript variable to PHP variable via AJAX

I'm trying to pass a variable from javascript to a PHP variable on the same page using AJAX.我正在尝试使用 AJAX 将变量从 javascript 传递到同一页面上的 PHP 变量。

I've been looking through all the post I could find and even though is been asked a few times before (how to pass javascript variable to php via ajax), I can't get it to work..我一直在浏览我能找到的所有帖子,即使之前被问过几次(如何通过 ajax 将 javascript 变量传递给 php),我也无法让它工作..

Here is what I tried from what I read from other posts.这是我从其他帖子中阅读的内容中尝试的内容。 But it is not working.但它不起作用。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
  <script>
  $('#submitfakta').live('mouseover mouseout', function(e) {
    if (e.type == 'mouseover') {
        var idAttr = $(this).attr('value');
                        //alert($(this).attr('id'));
                        $.ajax({
                            type: "POST",
                            url: 'profile.php',
                            data: idAttr : idAttr 
                            success: function(data)
                            {
                                alert("success!");
                            }
                        });
                    });
    }
  });


  </script>

This is my javascript code alone without using AJAX.这是我的 javascript 代码,没有使用 AJAX。 It is working correctly for getting my variable using javascript.它可以正常使用 javascript 获取我的变量。 But it lacks the part that sends the variable to a php variable on the same page.但它缺少将变量发送到同一页面上的 php 变量的部分。 By looking through others post I tried using AJAX as my example above, but as already mentioned without luck.通过查看其他帖子,我尝试使用 AJAX 作为我上面的示例,但正如已经提到的那样没有运气。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
  <script>
  $('#submitfakta').live('mouseover mouseout', function(e) {
    if (e.type == 'mouseover') {
        var idAttr = $(this).attr('value');
        alert(idAttr);
    } 
  });


  </script>

My PHP code trying to print my variable:我的 PHP 代码试图打印我的变量:

<?php
  $idAttr = $_POST['idAttr'];
  echo $idAttr;
  ?>

try to send data like this(data have to be a plain object or string)尝试发送这样的数据(数据必须是普通对象或字符串)

data: {idAttr : idAttr} 

and get in php并进入 php

$idAttr = $_POST['idAttr']

Here is my HTML & JS Code!这是我的 HTML 和 JS 代码!

    <div id="submitfakta" value="abdulrehman" style="height: 200px; width: 200px; background-color: red;"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
  <script>
  $('#submitfakta').live('mouseover mouseout', function(e) {
    if (e.type == 'mouseover') {
        var idAttr = $(this).attr('value');
        //alert($(this).attr('id'));
        $.ajax({
            type: "POST",
            url: 'profile.php',
            data: {idAttr : idAttr },
            success: function(data)
            {
                alert("success! " + data);
            }
        });

    }
  });
  </script>

AND HERE IS MY profile.php CODE:这是我的 profile.php 代码:

<?php 
echo $_POST["idAttr"]; exit; 

You have to send the data as您必须将数据发送为

data: {var1: value1, var2 : value2}

You have the following, which is incorrect你有以下,这是不正确的

data: idAttr : idAttr

You can also pass that data like in jquery,您还可以像在 jquery 中一样传递该数据,

data : "idAttr ="+ idAttr;

And on PHP Side在 PHP 端

$idAttr = $_POST['idAttr'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM