javascript - Send js variable to a php .ajax -


i trying define id# of choice , declaring variable send "action.php", file connects db , inserts values. div-block 5 id's in html:

 <div class="rate">  <div id="1" class="b-1 rate-btn"></div>  <div id="2" class="b-2 rate-btn"></div>  <div id="3" class="b-3 rate-btn"></div>  <div id="4" class="b-4 rate-btn"></div>  <div id="5" class="b-5 rate-btn"></div>  </div> 

anim.js intercepts click event , declares variable "therate" clicked "id":

$('.rate-btn').click(function(){      var therate = $(this).attr('id'); $('.rate-btn').removeclass('rate-btn-active');  (var = therate; >= 0; i--) { $('.b-'+i).addclass('rate-btn-active'); $.ajax({  type : "post",  url  : "action.php",  data : therate,  success:function(){alert(therate)}  });  }; }); 

second part of above code sends "therate" var "action.php". unfortunately id doesn't=( success:function(){alert(therate)} shows me id# on every choice no problem. "action.php" in same folder "anim.js". have tried "/action.php" - no luck. problem anim.js not send "therate" "action.php". i'm sure stupid , newbie problem don't recognize it=( please, show me problem! thanks.

knowing php part of script lot. decide data returned client. typically goes this:

php

$therate = $_post['therate']; $response = array(); if(isset($therate)){     $response['status'] = 'success';     $response['therate'] = $therate; } else {     $response['status'] = 'failure';     $response['message'] = 'variable therate not set.' }  echo json_encode($response); 

jquery

$.ajax({    type : "post",    url  : "action.php",    data : {'therate': therate},    success:function(data){        var o = $.parsejson(data);        if(o.status == 'success'){            alert(o.therate);        } else {            alert(o.message);        }    } }); 

notice addition of adding key identifier data sending server. allows pull post data serverside easily. notice 'data' in success function argument. actual data returned server. notice below can parse json due using json_encode array passed client.

i hope helps! let me know if have questions.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -