1

I need to make a moderate system like the one in fmylife.com. Basically the problem I have is loading the MySQL query using Ajax (without page refreshing) in to a div. MySQL query

$sql = mysql_query(“SELECT * FROM posts WHERE active =’0’” LIMIT 1) or die (mysql_error());
$row = mysql_fetch_array($sql);

HTML

<div class=”post-body”><?php echo $row[‘sitepost’];?></div>

this data should be reloaded when pressing "yes" or "no" buttons. thanks in advance.

1
  • @ Sarfraz - thats what i like to know. the Ajax code. @ T-Shirt Dude yes im trying from so many days now. at last i came here. Commented Jun 17, 2012 at 10:30

3 Answers 3

5

@mgraph is close but if you want to do this on a button click

in post.php:

//$UserOption will be Yes/No for button clicks or empty string for first load of the page

$UserOption = $_REQUEST['UserClicked'];

//Id will be set if a vote has been clicked or empty string if it's the first load
$Id = $_REQUEST['Id'];

//(Do something with $UserOption)

$sql = mysql_query("SELECT * FROM posts WHERE active ='0' LIMIT 1") or die (mysql_error());
$row = mysql_fetch_array($sql);
echo $row['sitepost'];

JS:

<script type="text/javascript">
$(document).ready(function(){
   update(null,null);
})

function update(Id, Vote) {
    $(".post-body").eq(0).load("post.php?UserClicked=" + Vote + "&Id=" + Id);
}
</script>

HTML:

<div class="post-body"></div>


<button type="button" onclick="update(1, 'Yes');">Yes</button>
<button type="button" onclick="update(1, 'No');">No</button>



<button type="button" onclick="update(2, 'Yes');">Yes</button>
<button type="button" onclick="update(2, 'No');">No</button>

Alternate HTML/JS

<script type="text/javascript">
$(document).ready(function(){
    $("#yes").click(function(){
        update('Yes');
    })
    $("#no").click(function(){
        update('No');
    })
    update();
})

function update(Vote) {
    $(".post-body").eq(0).load("post.php?UserClicked=" + Vote);
}
</script>

<div class="post-body"></div>
<button id="yes" type="button">Yes</button>
<button id="no" type="button">No</button>
Sign up to request clarification or add additional context in comments.

5 Comments

this is great but where is the click event?
Actually, I've just hard-coded it onto the buttons in the html... You could always give the buttons/divs an Id and attach a click using jQuery. See my revisions for an alternate way of attaching the click
@ Basic thats really nice. thank you. one more thing i like to ask can post a id alone with the vote. other wise mysql query will out put the same result isn't it?
Sure - presumably you've got multiple buttons for yes/no - so I'd suggest you go back to the function-on-button method I first proposed. I'll edit it as appropriate
Note that if the buttons are actually going to be generated server-side on each page refresh you'd actually need to return the HTML for them, including the Id in the post body. Also, don't forget to implement a mechanism to prevent a user clicking twice - eg disabling the button when clicked or similar (but that's beyond the scope of this Q). Also, look into using mysqli_ functions instead of mysql_ they're better maintained and allow "Parameterised Queries" which are important to prevent SQL Injection attacks
5

in post.php:

$sql = mysql_query("SELECT * FROM posts WHERE active ='0' LIMIT 1") or die (mysql_error());
$row = mysql_fetch_array($sql);
echo $row['sitepost'];

JS:

<script type="text/javascript">
$(document).ready(function(){
   $("#yes").click(function(){
      $(".post-body").eq(0).load("post.php");
   })
})
</script>

HTML:

<div id="yes">YES</div>
<div class="post-body"></div>

1 Comment

thanks how can i make it to work when a button click (click event)? say button id is "yes"
4

You can add $("html, body").animate({ scrollTop: 0 }, 500); . Thereby, when you clicked YES button, scroll move up to top. It looks nice.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.