简体   繁体   English

将html锚标记值从javascript传递到PHP

[英]Passing html anchor tag value from javascript to PHP

I have seen solutions close to my problem but none of them solves it. 我已经看到接近我的问题的解决方案,但是没有一个解决方案。

Here is my html for the anchor tag: 这是我的html标签:

<div class="rating rating2" align="center">         
    <a onclick="myAjax(5)" value="5">★</a>
    <a onclick="myAjax(4)" value="4">★</a>
    <a onclick="myAjax(3)" value="3">★</a>
    <a onclick="myAjax(2)" value="2">★</a>
    <a onclick="myAjax(1)" value="1">★</a> 
</div>

Here is my JS code: 这是我的JS代码:

function myAjax(star) {
    $.ajax({

      url:"find.php",  
      method:"POST", 
      data:{ star : star },
      success: function( data ) {
        console.log( data );
      }
    });
}

And my corresponding PHP code in find.php: 和我在find.php中对应的PHP代码:

<?php

  if(isset($_POST['star']))
  {
    $s=$_POST['star'];
    echo $s;
  }

?>

And of course I have stored the files in the same directory.I am not getting any output.Any help will be much appreciated. 当然,我将文件存储在同一目录中,但没有任何输出,我们将不胜感激。

I'm assuming your target is the value attribute of each link.Hence try something like this. 我假设您的目标是每个链接的value属性,因此请尝试如下操作。

<script>
function myAjax(star) {
$.ajax({

url:"find.php",  
method:"POST", 
data:{ star : star },
success: function( data ) {
console.log( data );
}
});
}

$("a").click(function () {
    var result = $(this).attr('value');

    myAjax(result)
})


</script>

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

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