Ajax can't get data from PHP script

Hello guys, I’m working on this project that uses a live table edit style (works perfectly) but trying to include the twitter style follow and unfollow in one of the columns (The last one precisely) to update a row record via ajax to another php script (the update script). The php works well but I’ve having problems returning the data to ajax to post to another php script.

Below shows the major part of the script (The php and ajax). I included a php comment on the important area of interest.


$query_pag_data = "SELECT * FROM applicant_result WHERE year='$year' AND class= '$class' ORDER by candidate_no ";

$uid=strip_tags($id);

$result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error());


$finaldata = "";
$tablehead= '';
$tablehead= "<tr>
		<th class='data'>#</th>
		<th  class='data'>Applicant ID</th>
		<th class='data'>Year</th>
		<th class='data'>Class</th>
		<th class='data'>$subject_1</th>
		<th class='data'>$subject_2</th>
		<th class='data'>$subject_3</th>
		<th class='data'>$subject_4</th>
		<th class='data'>$subject_5</th>
		<th class='data'>Total</th>
		<th class='data'>Status</th>
		<th class='data'>Interview</th>
	    </tr>";

while($row = mysql_fetch_array($result_pag_data))
{


$id=htmlentities($row['candidate_no']);
$subject_1=htmlentities($row['subject_1']);
$subject_2=htmlentities($row['subject_2']);
$subject_3=htmlentities($row['subject_3']);
$subject_4=htmlentities($row['subject_4']);
$subject_5=htmlentities($row['subject_5']);
$total=htmlentities($row['total']);
$status=htmlentities($row['interview']);
$uid= strip_tags($row['candidate_no']);

/* HELLO FORUMITES, THIS IS THE MAJOR AREA OF FOCUS HERE */


if($status!=0){$button="
<span id='loading<?php echo $uid; ?>'></span>
			<span class='button following' id='following<?php echo $uid; ?>' onClick='follow_or_unfollow(<?php echo $uid; ?>,'following');'>Following</span>
			
			<span style='display:none;' class='button follow' id='follow<?php echo $uid; ?>' onClick='follow_or_unfollow(<?php echo $uid; ?>,'follow');'>Follow</span>
";}

                  else{

$button="
<span id='loading<?php echo $uid; ?>'></span>
			<span class='button follow' id='follow<?php echo $uid; ?>' onClick='follow_or_unfollow(<?php echo $uid; ?>,'follow');'>Follow</span>
			
			<span class='button following' style='display:none;' id='following<?php echo $uid; ?>' onClick='follow_or_unfollow(<?php echo $uid; ?>,'following');'>Following</span>
";}



$tabledata.="<tr id='$id' class='edit_tr'>

<td class='edit_td' >
<span class='text'>$counter</span>

</td>
<td class='edit_td' >
<span class='text'>$id</span>

</td>
<td class='edit_td' >
<span class='text'>$year</span>
<input type='hidden' value='$year' class='editbox' id='six_input_$id' />
</td>
<td class='edit_td' >
<span class='text'>$class</span>
<input type='hidden' value='$class' class='editbox' id='seven_input_$id' />
</td>

<td class='edit_td' >
<span id='one_$id' class='text'>$subject_1</span>
<input type='text' value='$subject_1' class='editbox' id='one_input_$id' />
</td>

<td class='edit_td' >
<span id='two_$id' class='text'>$subject_2</span>
<input type='text' value='$subject_2' class='editbox' id='two_input_$id'/>
</td>

<td class='edit_td' >
<span id='three_$id' class='text'>$subject_3 </span>
<input type='text' value='$subject_3' class='editbox' id='three_input_$id'/>
</td>

<td class='edit_td' >
<span id='four_$id' class='text'>$subject_4</span>
<input type='text' value='$subject_4' class='editbox' id='four_input_$id' />
</td>

<td class='edit_td' >
<span id='five_$id' class='text'>$subject_5</span>
<input type='text' value='$subject_5' class='editbox' id='five_input_$id' />
</td>
</td>
<td class='edit_td' >
<span class='text'>$total</span>

</td>


<td class='edit_td' >
<span class='text'>$status</span>

</td>
		
	 	
<td>
$button			
</td>

</tr>";
$counter++;

}
$finaldata = "<table width='100%'>".$tablehead." ".$tabledata. "</table>"; // Content for Data




echo $finaldata;

/*  NOW THE AJAX */


<script type="text/javascript">
//Changes the Following text to Unfollow when mouseover the button
$(document).ready(function()
{
    $('.following').hover(function()
    {
        $(this).text('Unfollow');
    },function()
    {
        $(this).text("Following");
    });
 });

 //Perform the Following and Unfollowing work
function follow_or_unfollow(id,action)
{
    var dataString = "id=" + id;
    $.ajax({
        type: "POST",
        url: "follow_or_unfollow.php",
        data: dataString,
        beforeSend: function()
        {
            if ( action == "following" )
            {
                $("#following"+id).hide();
                $("#loading"+id).html('<img src="loading.gif" align="absmiddle" alt="Loading...">');
            }
            else if ( action == "follow" )
            {
                $("#follow"+id).hide();
                $("#loading"+id).html('<img src="loading.gif" align="absmiddle" alt="Loading...">');
            }
            else { }
        },
        success: function(response)
        {
            if ( action == "following" ){
                $("#loading"+id).html('');
                $("#follow"+id).show();

            }
            else if ( action == "follow" ){
                $("#loading"+id).html('');
                $("#following"+id).show();
            }
            else { }
        }
    });
}
</script>


The main problem here seems to be that the intended echoed php variables won’t be echoed at all, and that there are too many levels of quotation marks being used.


if($status!=0){$button="
<span id='loading<?php echo $uid; ?>'></span>
			<span class='button following' id='following<?php echo $uid; ?>' onClick='follow_or_unfollow(<?php echo $uid; ?>,'following');'>Following</span>
 
			<span style='display:none;' class='button follow' id='follow<?php echo $uid; ?>' onClick='follow_or_unfollow(<?php echo $uid; ?>,'follow');'>Follow</span>
";}

You can make your life easier with this by using double quotes for HTML, and either single quotes for javascript. That might mean using heredoc syntax for the PHP, or using escaped quotes for JavaScript quotes or some other preferred technique that PHP users will be aware of.

If it were my decision on things though, I would eviscerate all inline scripting events from your HTML code and instead use some scripting at the end of the page to attach those events.

I’ll pass this thread over the PHP channel for their expertise in this matter.