So this stems from a problem yesterday that quickly spiraled out of control as the errors were unusual. This problem still exists but the question was put on hold, here, and I was asked to reform a new question that now relates to the current problem. Then I asked a question here and again things quickly moved off topic. I decided to accept the answer that solved the original problem and move on. I now believe we have narrowed the problem down to a very specific question.
This problem now makes 0 sense to me. I have the following code
jQuery
$('#projects').click(function (e) {
$.trim(aid);
alert(aid);
$.ajax({
url:'core/functions/projects.php',
type: 'post',
data: {'aid' : aid},
done: function(data) {
// this is for testing
}
}).fail (function() {
alert('error');
}).always(function(data) {
alert(data);
$('#home_div').hide();
$('#pcd').fadeIn(1000);
$('#project_table').html(data);
});
});
PHP
<?php
include "$_SERVER[DOCUMENT_ROOT]/core/init.php";
if(isset($_POST['aid'])) {
$aid = $_POST['aid'];
try {
$query_projectInfo = $db->prepare("
SELECT projects.account_id,
projects.project_name,
projects.pm,
//..irrelevant code
FROM projects
WHERE account_id = ?
");
$query_projectInfo->bindValue(1, $aid, PDO::PARAM_STR);
$query_projectInfo->execute();
$count = $query_projectInfo->rowCount();
if ($count > 0) {
echo "<table class='contentTable'>";
echo "<th class='content_th'>" . "Job #" . "</th>";
echo "<th class='content_th'>" . "Project Name" . "</th>";
//..irrelevant code
while ($row = $query_projectInfo->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<td class='content_td'>" . "<a href='#'>" . $row['account_id'] . "</a>" . "</td>";
echo "<td class='content_td'>" . $row['project_name'] . "</td>";
//..irrelevant code
echo "</tr>";
}
echo "</table>";
}
} catch(PDOException $e) {
die($e->getMessage());
}
} else {
echo 'could not load projects table';
}
?>
When I run this code by pressing '#projects' I get 2 alerts. This first alert says '6', which is the value of the variable 'aid' and is expected. The second alert is blank.
Now here is where I get extremely confused. If I simplify things and get to the root of the problem by changing my PHP
file to look like this
<?php
if(isset($_POST['aid'])) {
$aid = $_POST['aid'];
echo $aid;
} else {
echo 'fail';
}
The response is now '6'! Which means the file is receiving my $_POST['aid'] variable and correctly setting it inside PHP
. Now if I change the code back, again I receive nothing.
However if I change the original PHP
file to this
<?php
include "$_SERVER[DOCUMENT_ROOT]/core/init.php";
$aid = '6';
try {
$query_projectInfo = $db->prepare("
SELECT projects.account_id,
projects.project_name,
projects.pm,
//..irrelevant code
FROM projects
WHERE account_id = ?
");
$query_projectInfo->bindValue(1, $aid, PDO::PARAM_STR);
$query_projectInfo->execute();
$count = $query_projectInfo->rowCount();
if ($count > 0) {
echo "<table class='contentTable'>";
echo "<th class='content_th'>" . "Job #" . "</th>";
echo "<th class='content_th'>" . "Project Name" . "</th>";
//..irrelevant code
while ($row = $query_projectInfo->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<td class='content_td'>" . "<a href='#'>" . $row['account_id'] . "</a>" . "</td>";
echo "<td class='content_td'>" . $row['project_name'] . "</td>";
//..irrelevant code
echo "</tr>";
}
echo "</table>";
}
} catch(PDOException $e) {
die($e->getMessage());
}
?>
Then run the PHP
file directly the query is successful and the page loads the table its dynamically creating. Which makes my head spin so much I want to puke.
So in review. The aid variable is set to 6, there is no question. If I simplify my PHP
file it receives the $_POST['aid'] variable, takes the data, sets the PHP
$aid variable and echo's it back. However if I try and insert it into my query to inflate my table I get nothing. Now if I change the PHP
file to not rely on the $_POST['aid'] variable it works. So what the heck is going on here? The PHP
file works without the post and the post works without the table inflation query.
I really hope someone can help me figure this out because I am just totally lost
EDIT
I changed my php file around for testing. It now looks EXACTLY like this. Sorry for the wall
<?php
include "{$_SERVER['DOCUMENT_ROOT']}/TrakFlex/core/init.php";
if(isset($_POST['aid'])) {
$aid = $_POST['aid'];
echo $aid;
try {
$query_projectInfo = $db->prepare("
SELECT projects.account_id,
projects.project_name,
projects.pm,
projects.apm,
projects.est_start,
projects.est_end,
projects.contact,
projects.trips,
projects.tasks,
projects.perc_complete,
projects.bcwp,
projects.actuals,
projects.cpi,
projects.bcws,
projects.bac,
projects.comments,
projects.status,
projects.project_revenue,
projects.profit_margin,
projects.pm_perc,
projects.audited
FROM projects
WHERE account_id = ?
");
$query_projectInfo->bindValue(1, $aid, PDO::PARAM_STR);
$query_projectInfo->execute();
echo "<table class='contentTable'>";
echo "<th class='content_th'>" . "Job #" . "</th>";
echo "<th class='content_th'>" . "Project Name" . "</th>";
echo "<th class='content_th'>" . "PM" . "</th>";
echo "<th class='content_th'>" . "APM" . "</th>";
echo "<th class='content_th'>" . "Est. Start" . "</th>";
echo "<th class='content_th'>" . "Est. End" . "</th>";
echo "<th class='content_th'>" . "Contact" . "</th>";
echo "<th class='content_th'>" . "Trips" . "</th>";
echo "<th class='content_th'>" . "Tasks" . "</th>";
echo "<th class='content_th'>" . "% Complete" . "</th>";
echo "<th class='content_th'>" . "BCWP" . "</th>";
echo "<th class='content_th'>" . "Actuals" . "</th>";
echo "<th class='content_th'>" . "CPI" . "</th>";
echo "<th class='content_th'>" . "BCWS" . "</th>";
echo "<th class='content_th'>" . "BAC" . "</th>";
echo "<th class='content_th'>" . "Comments" . "</th>";
echo "<th class='content_th'>" . "Status" . "</th>";
echo "<th class='content_th'>" . "Project Revenue" . "</th>";
echo "<th class='content_th'>" . "Profit Margin" . "</th>";
echo "<th class='content_th'>" . "PM%" . "</th>";
echo "<th class='content_th'>" . "Audited" . "</th>";
while ($row = $query_projectInfo->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<td class='content_td'>" . "<a href='#'>" . $row['account_id'] . "</a>" . "</td>";
echo "<td class='content_td'>" . $row['project_name'] . "</td>";
echo "<td class='content_td'>" . $row['pm'] . "</td>";
echo "<td class='content_td'>" . $row['apm'] . "</td>";
echo "<td class='content_td'>" . $row['est_start'] . "</td>";
echo "<td class='content_td'>" . $row['est_end'] . "</td>";
echo "<td class='content_td'>" . $row['contact'] . "</td>";
echo "<td class='content_td'>" . $row['trips'] . "</td>";
echo "<td class='content_td'>" . $row['tasks'] . "</td>";
echo "<td class='content_td'>" . $row['perc_complete'] . "</td>";
echo "<td class='content_td'>" . $row['bcwp'] . "</td>";
echo "<td class='content_td'>" . $row['actuals'] . "</td>";
echo "<td class='content_td'>" . $row['cpi'] . "</td>";
echo "<td class='content_td'>" . $row['bcws'] . "</td>";
echo "<td class='content_td'>" . $row['bac'] . "</td>";
echo "<td class='content_td'>" . $row['comments'] . "</td>";
echo "<td class='content_td'>" . $row['status'] . "</td>";
echo "<td class='content_td'>" . $row['project_revenue'] . "</td>";
echo "<td class='content_td'>" . $row['profit_margin'] . "</td>";
echo "<td class='content_td'>" . $row['pm_perc'] . "</td>";
echo "<td class='content_td'>" . $row['audited'] . "</td>";
echo "</tr>";
}
echo "</table>";
} catch(PDOException $e) {
die($e->getMessage());
}
} else {
echo 'could not load projects table';
}
?>
The response I get in my browser is now this
6<table class='contentTable'><th class='content_th'>Job #</th><th class='content_th'>Project Name</th><th class='content_th'>PM</th><th class='content_th'>APM</th><th class='content_th'>Est. Start</th><th class='content_th'>Est. End</th><th class='content_th'>Contact</th><th class='content_th'>Trips</th><th class='content_th'>Tasks</th><th class='content_th'>% Complete</th><th class='content_th'>BCWP</th><th class='content_th'>Actuals</th><th class='content_th'>CPI</th><th class='content_th'>BCWS</th><th class='content_th'>BAC</th><th class='content_th'>Comments</th><th class='content_th'>Status</th><th class='content_th'>Project Revenue</th><th class='content_th'>Profit Margin</th><th class='content_th'>PM%</th><th class='content_th'>Audited</th></table>
Which means two things. The aid variable is being passed and set. However when I run it in my query it fails. But if I put $aid = '6'; then bind aid as a value it works.
WHY?
2nd EDIT
So through the help of others we found out this was a js
error not a php
one. The variable is being set to 6 in the js
but it has some hidden characters and I think I found them. When I look in firebug I see this as the source
aid=%0D%0A6
which is supposed to be a line break right? I think that's what's causing the errors.
This is how I'm getting aid
var title;
var aid;
$(".sa").click(function (e) {
title = $(this).text();
$.post('core/functions/getAccountId.php', {
title: title
})
.done(function(data) {
aid = data;
$("#acc_title").html(title);
$('#accountsSelectDiv').hide();
$('#acc_home').fadeIn(1000);
$('#home_div').show();
})
.fail(function(jqXHR, status, error) {
alert(error);
});
});
This might be because of how I'm getting it, I have no idea. How do I remove the extra characters in the source though? That is what I think the problem is