Page 1 of 1

I'm Miserable. Trying To Pass Jscript Var To PHP Query

Posted: Tue Dec 22, 2009 12:28 pm
by jbh
Hey, we have a page with 6 surveys and, for now, one block of PHP code that generates JSON. The goal of the JSON is to take the results of that question and display it to the user (We have jquery functions that successfully work with the JSON)

The JSON loads fine and the display of the results is working. The problem is that with 6 questions displayed, the JSON only recognizes the question_id of the last question when looping through a db.

In other words, if I choose survey 3, it doesn't recognize it. I am able to capture the vote using Jquery POST. But when I want the JSON to reflect THAT vote and the live snapshot of results for the CHOSEN question, it can't read it. Instead, it only reads the question ID of the last question looped from a database. How do I get my PHP code, which generates the JSON, to query the results of the question ID of the CHOSEN question instead of the last one that's looped through the DB?

Here is what I have:

1.) I CAN grab the q_id via javascript. I just can't append it to the darn PHP query that spits out JSON when the page loads
That's another issue, the darn thing should be generating upon click. I can probably work that out but what I can't work out is
passing the variable to the PHP json generating query:

Here is the code for the css 'submit button'

Code: Select all

<a href="#" rel="#mies1" id="upny_sresbtn1" ><span>Submit!</span></a>
The PHP code generates this:

Code: Select all

var upny_iniresults = { 
 
"q_cdata" : [
 
        { "a_id" : "9", "a_txt" : "Yes", "a_perc" : "3.45", "t_r" : "1" },
        { "a_id" : "10", "a_txt" : "No", "a_perc" : "13.79", "t_r" : "4" },
        { "a_id" : "11", "a_txt" : "Not Sure", "a_perc" : "24.14", "t_r" : "7" },
        { "a_id" : "26", "a_txt" : "Maybe", "a_perc" : "24.14", "t_r" : "7" }
 
};
 
What I am not showing, for sake of not overloading you, is that I successfully can read the click of a submit button and add the vote. the voting process works perfectly, it's just that the JSON won't load the live results of that question from the db.

How do I do THAT?

Thanks

PS. I can modify code examples/show more upon request. I just don't want to overload anybody or confuse anybody. TY so much for your time.
PPS. The form you see is listed as 'first form'. What I am not really showing, in detail, is that I can track the submit of a button and
find out which qid was chosen. What I can't do is take that var

Re: I'm Miserable. Trying To Pass Jscript Var To PHP Query

Posted: Tue Dec 22, 2009 12:40 pm
by jbh
One thing I want to add. I am considering replacing the PHP function that generates JSON
with an ajax/jquery post which will hit an external PHP file that can take a querystring and output JSON
I know how to submit to it and I know already that the external php page WILL generate the json

I only refuse to use this method because I don't know how to guarantee it will load, such as when I hard code
this solution. That's my problem. Maybe this .jpost solution is best? If so, how would I make sure
that the json from that external php loads just as it does now when I hard code json via php?

Thanks

Re: I'm Miserable. Trying To Pass Jscript Var To PHP Query

Posted: Tue Dec 22, 2009 2:28 pm
by kaszu
I have no idea what you said or where you are having the problem.

Is PHP returning incorrect data? If so, please post your PHP code. If it's javascript, the post your javascript code. And please explain what you mean by:
JSON only recognizes
what do you mean by "recognizes"?
JSON only recognizes the question_id of the last question when looping through a db
JSON (JavaScript Object Notation) is data, so what are you referring to when saying "JSON", is it javascript?

Edit: output generated by PHP (what you have shown) is not a correct JSON, it's javascript (also invalid, because is missing closing ] ), but not JSON.

Re: I'm Miserable. Trying To Pass Jscript Var To PHP Query

Posted: Tue Dec 22, 2009 2:54 pm
by jbh
Forgive me. Since this is a unique situation it's hard for me to articulate it. Here is what I meant (code will follow).

Initially, a polling system was built to have one question per page. A Querystring would be read, such as qid=3 (Question ID = 3)
and the following PHP code would generate JSON. Our jquery functions would read that JSON (nothing more than the options for the question including the # of answers for each answer) and load them into a pop up window that displays the results and animates them.

This was working pretty well until we learned that the home page will have *six* questions. Since there are no query strings this time, since nobody is linking to a question, I can't get my PHP script to determine which question was answered.

The PHP code used to genreate the JSON is below. The goal is to get it to be 'smart enough' to say "Ok, out of 6 possible surveys, only generate the results based on the question_id of the submitted question"

Code: Select all

var upny_iniresults = { 
"q_cdata" : [
 
<?
 $jsonvar1='
        { "a_id" : "[cid]", "a_txt" : "[choice]", "a_perc" : "[percent]", "t_r" : "[responses]" },
';
$root=$_SERVER["DOCUMENT_ROOT"];
 
[b]$sql="Select count(ID) as total_votes from xxxresults where question_id='$qid'";[/b]
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$total_votes=$row["total_votes"];
$sql="Select ID,choice from xxx_choices where question_id='$qid' order by ID ASC";
 
$jsonvar1="";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result))
{
 
$cid=$row["ID"];
$choice=$row["choice"];
 
# Let's count the # of options
 
[b]$sql2="Select count(choice_id) as responses from xxx_results where choice_id='$cid' AND question_id='$qid'";[/b]
query($sql2);
$row2=mysql_fetch_array($result2);
$responses=$row2["responses"];
 
@$percent=$responses / $total_votes;
$percent=$percent * 100;
$percent=number_format($percent,2);
 
# Let's set the json variable now - the template
$jsonvar1.='
        { "a_id" : "[cid]", "a_txt" : "[choice]", "a_perc" : "[percent]", "t_r" : "[responses]" },
';
 
$jsonvar1=str_replace("[cid]",$cid,$jsonvar1);
$jsonvar1=str_replace("[choice]",$choice,$jsonvar1);
$jsonvar1=str_replace("[percent]",$percent,$jsonvar1);
$jsonvar1=str_replace("[responses]",$responses,$jsonvar1);
}
$jsonvar1=substr_replace($jsonvar1 ,",",-1);
$jsonvar1=str_replace(",,","",$jsonvar1);
 
echo $jsonvar1;
?>
        
    ]
 
};
So, this logic worked fairly well when it's *one* question per page because the variable $qid was the querystring on a page such as
survey.php?qid=3

So we'd load up the php query and it'd generate the JSON needed to display the survey results upon submission. Doing that with 6 questions, with unique qid's, is an issue for me.

What I am not showing is that upon 'submit', I add a vote to vote.php.

Code: Select all

$('#upny_sresbtn1').click(function () { 
var formData2 = $("#firstform").serialize();  
$.post("vote.php", $("#firstform").serialize()); // this tallies the 'vote' when they answer the question (it's a survey, really)
var url2 = 'http://www.yy.com/answer_question/return_results.php?' + formData2
 
// var url2 was created when I tried to load that JSon into a jquery function but that's not what I will focus on for now.
    });
So, the upny_sresbtn1 'submit button' gets clicked, I successfully can capture the question id in the formData2 var, as it's a querystring that offers this result:

qid=1&choice_1=32&user_id=&token=

So it DOES grab the proper qid but I can't put THAT value dynamically into the PHP code that spits out the JSON I need.
Since the person submits the vote and I pop up the results, obviously I need to load the results that include their vote
Since the PHP can't read the proper question ID...it's not working. Does this help explain my issue?

Thanks for your time.

Re: I'm Miserable. Trying To Pass Jscript Var To PHP Query

Posted: Mon Jan 04, 2010 9:51 am
by jbh
Friendly bump hoping somebody has experience with situations like this.

I've lost so much time trying to figure it out, it's miserable.

Re: I'm Miserable. Trying To Pass Jscript Var To PHP Query

Posted: Mon Jan 04, 2010 9:59 am
by AbraCadaver
You haven't really shown enough code or explained it well enough to see the problem, but two things that stick out:

1. $qid is used, but I don't see it defined anywhere
2. Why not use json_encode() instead all that wacky code

Re: I'm Miserable. Trying To Pass Jscript Var To PHP Query

Posted: Mon Jan 04, 2010 10:43 am
by jbh
Ironically, I posted more detail on another board and forgot to do so here with that update. My apologies.

Here is the idea:

1.) Multiple surveys on a page each with a unique question_id

2.) I loop through a db for 6 questions and load them on that page. User submits a survey response
and the idea is to show a css generated popup (overlay using the overlay plugin) that shows the results
of that survey.

Technically, this works fine. The problem is that we use JSON to display the survey results
and feed the 'pop up' to show the bar graph that represents their total votes and % of votes.
The issue is that the previous designer didn't take into consideration multiple questions on one page.

While the JSON data loads well if you use one question per page (A querystring shows the question ID and we use php
to grab that q_id and pull out the db results to create json) it won't work with more than one.

SO, the goal is to do this:

A.) .getJSON function to hit load_json.php _ qid (the value of the qid field in the survey form)

B.) Pass that Json to this function:

Code: Select all

 
 
function upny_liRes() {
 
    var j = 0;
    var a_html = new Array();
    
 
    for(i = 0;i < upny_iniresults.q_cdata.length;i++) {
 
        var t_html = '';
    
        t_html += '<div class="upny_rlistttl">';
        t_html += upny_iniresults.q_cdata[i].a_txt;
        t_html += '</div><div class="upny_rlistcbr" style="width:1px;" id="a_';
        t_html += upny_iniresults.q_cdata[i].a_id;
        t_html += '"><div id="ap_';
        t_html += upny_iniresults.q_cdata[i].a_id;
        t_html += '">0';
        //t_html += upny_iniresults.q_cdata[i].a_perc;
        t_html += '%</div></div><div class="upny_clear"></div>\n';
 
        a_html[j] = t_html;
        
        j++;
        
    }
 
    $('#upny_resqanscntr').html(a_html.join('<div class="upny_dttdline"></div>'));
    
    upny_aniRes(upny_iniresults.q_cdata);
    
}
 
When we load the json during onload, we use this format, which allows the above function to display it properly:

Code: Select all

var upny_iniresults = { 
 
    "q_cdata" : [
 
        { "a_id" : "4322", "a_txt" : "Popular Music", "a_perc" : "100" },
        { "a_id" : "4323", "a_txt" : "Rhythm & Blues", "a_perc" : "70" },
        { "a_id" : "4324", "a_txt" : "Rock & Roll", "a_perc" : "50" },
        { "a_id" : "4325", "a_txt" : "Classical & Orchestra", "a_perc" : "25" }
 
    ]
 
See? It's a variable that holds pre-loaded json. The idea is to simulate that exact json structure
in load_json.php and pass it back to upny_lires function

That's all I want to do. I know how to pass the question ID to the external PHP page and I know how to
get the external php page to create a json string. What I cannot do is get that data to pass through the upny_lires function :(

TY for your time.

PS. Why don't I use json_encode? Not sure, because all I know is that as long as json is loading
through upny_liRes, the rest of the functions work. I am open to any way that allows an external php page
to take my querystring, create json and feed it back successfully to upny_lires. I do that and I am finally set. TY, again.