javascript - How to get id and type in php foreach loop and send to server via ajax? -
so have 2 files index.php , changelikedislike.php. think issue javascript trying type , id not know how go that. javascript function being called in foreach->li->divs. working before this: data: datastring,
added schoolid data of ajax it's data: {datastring:datastring, schoolid:schoolid},
index.php
<?php $last_id = 0; foreach ($list $rs) { $last_id = $rs['id']; // keep last id paging ?> <li> <div style="width:100%; color:#000;"> <?php echo '<div class="product_like thumb-div"><img src="like.png" class="rating-image " onclick=changelikedislike("like","'.$rs['id'].'")> <br><span id="product_like_'.$rs['id'].'">'.$rs['plike'].'</span></div>';?> <?php echo '<div class="product_dislike"><img src="dislike.png" class="rating-image" onclick=changelikedislike("dislike","'.$rs['id'].'")><br> <span id="product_dislike_'.$rs['id'].'">'.$rs['pdislike'].'</span></div>';?> </div> </li> <?php } ?> <script type="text/javascript"> //begin , dislike function changelikedislike(type,id){ var datastring = 'id='+ id + '&type=' + type; $.ajax({ type: "post", url: "changelikedislike.php", data: {datastring:datastring, schoolid:schoolid}, cache: false, success: function(result){ if(result){ console.log('working'); } } });//end ajax }
schoolid works in data not datastring in ajax how go grabbing those. php file reference:
//checks if school page id brought , stores variable if (isset($_post['schoolid'])) { $schoolidfinal = $_post['schoolid']; }else { echo "nope"; } if (isset($_post['type'])) { $type = $_post['type']; }else { echo "nope type"; } if (isset($_post['id'])) { $id = $_post['id']; }else { echo "nope id"; }
my page echoing out "nope type nope id"
please change
data: {datastring:datastring, schoolid:schoolid},
to
data: {type:type, id:id, schoolid:schoolid},
to match params php script.
data
can either query string (like datastring
) or json struct (like {type:type, id:id, schoolid:schoolid}
).
see documentation of jquery.ajax()
: "the data option can contain either query string of form key1=value1&key2=value2, or object of form {key1: 'value1', key2: 'value2'}."
Comments
Post a Comment