php - how to pass a hidden value using post with AJAX - Stack Overflow

I want only registered users to vote. I do not want to change all my codes. Easiest way to do for me no

I want only registered users to vote. I do not want to change all my codes. Easiest way to do for me now is set something to true if user logged in and pass that in the hidden field.

How can i pass a hidden value on here?

This is what i have got so far

$(function() {

$(".vote").click(function() 
{

var name = $(this).attr("name");
var id = $(this).attr("id");

var dataString = 'id='+ id ;
var parent = $(this);


if(name=='up')
{

$(this).fadeIn(200).html('<img src="dot.gif" align="absmiddle">');
$.ajax({
   type: "POST",
   url: "up_vote.php",
   data: dataString,
   cache: false,

   success: function(html)
   {
    parent.html(html);

  }  });

}

return false;
    });

});

  <div class="box1">
    <div class="up"><a href="" class="vote" name="up" id="'.$row['id'].'">'.$row['plus'].'</a></div>
  </div>

up_vote.php

if($_POST['id'])
{

$id=$_POST['id'];
.
.
. 
}

I want only registered users to vote. I do not want to change all my codes. Easiest way to do for me now is set something to true if user logged in and pass that in the hidden field.

How can i pass a hidden value on here?

This is what i have got so far

$(function() {

$(".vote").click(function() 
{

var name = $(this).attr("name");
var id = $(this).attr("id");

var dataString = 'id='+ id ;
var parent = $(this);


if(name=='up')
{

$(this).fadeIn(200).html('<img src="dot.gif" align="absmiddle">');
$.ajax({
   type: "POST",
   url: "up_vote.php",
   data: dataString,
   cache: false,

   success: function(html)
   {
    parent.html(html);

  }  });

}

return false;
    });

});

  <div class="box1">
    <div class="up"><a href="" class="vote" name="up" id="'.$row['id'].'">'.$row['plus'].'</a></div>
  </div>

up_vote.php

if($_POST['id'])
{

$id=$_POST['id'];
.
.
. 
}
Share Improve this question asked Jan 2, 2012 at 0:40 eMReeMRe 3,2576 gold badges37 silver badges51 bronze badges 3
  • 1 Why not have the php do the checking and disable voting by not echoing the vote <a hrefs> if the user isn't logged in? – squareborg Commented Jan 2, 2012 at 0:49
  • i have to change too many codes. I can not just disable voting. – eMRe Commented Jan 2, 2012 at 0:54
  • Oh, the joys of spaghetti-code [PHP] :-) – user166390 Commented Jan 2, 2012 at 1:49
Add a ment  | 

3 Answers 3

Reset to default 8

Wrong approach. Javascript is executed on the client side. You never trust the client, because the client is controlled by the user. Use the session, which is stored server-side.

(What will keep the user from copy+pasting the page code, modifying it, and executing it?)

Add your hidden input:

<input type="hidden" id="loggedIn" value="whatever" />

Set up variable

var id = $(this).attr("id");
var loggedIn = $("#loggedIn").val(); //will be the value of loggedIn

Change this line in $.ajax()

data: {loggedIn: loggedIn, id: id}

simply by appending the hidden field to the dataString

dataString += 'foo=bar'

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743780664a4505736.html

相关推荐

  • php - how to pass a hidden value using post with AJAX - Stack Overflow

    I want only registered users to vote. I do not want to change all my codes. Easiest way to do for me no

    7天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信