avghdev avghdev - 5 months ago 42
Javascript Question

PHP isset not working after jQuery Post

I'm trying to make a voting system where users can vote as many times as they want, and it will update the vote # (without refreshing) every time they click the vote button.

My problem is that when I try to do a $.post call (I've tried the other methods to), the variable isn't getting set (I think), SO nothings getting queried to my database whenever I click.

Here's the code:

List.php

<?php

//Require login file -- Fatal error if not found
require_once('login.php');

//Connect w/ MySQL database [login variables]
$con = new mysqli($hn, $un, $pw, $db);

//If connection error -> kill application and display error
if ($con->connect_error) die('Connect Error (' . $con->connect_error . ') '. $con->connect_error);

if (isset($_POST)) {

}

//heading
echo <<<_END
<!DOCTYPE html>
<html>
<head>
<title>Testing</title>
<!--Page Style-->
<link type="text/css" rel="stylesheet" href="Style.css">

<!--jQuery-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>

<!--RateYo | http://prrashi.github.io/rateYo/-->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/rateYo/2.0.1/jquery.rateyo.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/rateYo/2.0.1/jquery.rateyo.min.js"></script>

<!--Font Awesome-->
<script src="https://use.fontawesome.com/d785d4b203.js"></script>

</head>
<body>
_END;

//retrieve databse items
$query = "SELECT * FROM items ORDER BY difficulty DESC";
$result = $con->query($query);
if (!$result) die($con->error);

$rows = $result->num_rows;


//display items
for ($j = 0; $j < $rows; ++$j) {
$result->data_seek($j);
$row = $result->fetch_array(MYSQLI_ASSOC);

//random color
$rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
$color = '#'.$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];

echo '<div class="listitem" data-id="'.$row['id'].'" data-votes="'.$row['votes'].'"style="background-color:'.$color.' "><div class="bcontain"><img class="badge" src="img/badge1.png" /></div><div class="goal">'.$row['goal'].'</div>';
echo '<div class="vote"><span id="vnumb">'.$row['votes'].'</span> votes <i class="fa fa-sort-asc fa-3x" id="voteup"></i><span id="uptext">up</span></div>';
echo '<div class="diff">Difficulty: '.$row['difficulty'].'</div></div> <br><br>';

}

echo <<<_END
<script>
$('.vote i').click(function(){

var self = $(this); // cache
var parent = self.parent().parent(); // grab grand parent .item
var id = parent.data('id'); // grab post id from data-postid
var votes = parent.data('votes'); // grab score form data-score

parent.data('votes', ++votes);

//change html
parent.find('#vnumb').html(votes);

//send info to database
$.post("ajaxvote.php", {postid : id}, function() {
alert("success");
});
})
</script>
_END;

//closing tags
echo <<<_END
</body>
</html>
_END;

//close connection
$result->close();
$con->close();

function get_post($conn, $var) {
return $conn->real_escape_string($_POST[$var]);
}

?>


ajaxvote.php

<?php
require_once('login.php');


//Connect w/ MySQL database [login variables]
$con = new mysqli($hn, $un, $pw, $db);

//If connection error -> kill application and display error
if ($con->connect_error) die('Connect Error (' . $con->connect_errno . ') '. $con->connect_error);



if (isset($_POST['postid'])) {
$postId = $con->real_escape_string($_POST['postid']);

echo '<script>alert("hello");</script>';

# query into db table to know current voting score
$query = "SELECT votes FROM items WHERE id = '$postId' LIMIT 1";
$result = $con->query($query);
if(!$result) echo "INSERT failed: $query<br>" . $con->error . "<br><br>";

# increase or dicrease voting score
if ($con->fetch_array($result)) {
$data = $con->fetch_array($result);
$vote = ++$data['votes'];

# update new voting score
$con->query("UPDATE items SET votes = '$vote' WHERE id = '$postId' ");

}
}


$con->close();
?>

Answer

Alright so I figured out the problem.

if ($con->fetch_array($result)) {
  $data = $con->fetch_array($result);

should be

if ($result->num_rows) {
  $data = $result->fetch_array(MYSQLI_ASSOC);

partial credit to @juliusbin for editing the if parameter

The other problem was that 1. I was not calling the fetch_array method on a query I was calling it on the connection itself and 2. I was passing the result to fetch array instead of telling it what type of an array I wanted.

This, kids, is why you don't blindly follow online coding tutorials.

Comments