简体   繁体   中英

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:

<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:

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:

<?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.

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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