Page 1 of 1

Simple Jquery POST Question

Posted: Sun Mar 28, 2010 12:11 pm
by jbh
Hey,

I have a page with 30 survey questions. When the 'submit answer' button is clicked on for each question, 3 things happen:

1.) A vote is sent to the database
2.) process.js is called, which creates a pop up, bar graph and displays the vote results for that question.
3.) They cacn filter results when the pop up is open, so they can view results via demographic

Part 3 works fine. The issue is making sure part 1 (code below) is COMPLETE before part 2 operates. The reason this is critical is because
sometimes, after answering a question, the bar graph loads the results WITHOUT including the most recent vote. This is because my $post function doesn't call a function before it calls on step 2. How can I solve this problem?

Below is the code. I am new to Jquery and this project has taken a lot out of me as I had to learn jquery due to our developer being fired and I am desperate to finish this...below is the code as well as an explanation as to what is going on. In short, I just need a function to be called from the $post command that activates before we call the include file (process.js) and filter.js. Something invisible and silent would be ideal...like a delay.

Code: Select all

<script language="Javascript">

$('#upny_sresbtn_25').click(function () {

var formData3 = $("#firstform_25").serialize();

$.post("vote.php", $("#firstform_25").serialize());

// The goal is to have a function that prevents the following code from loading
// until the vote is actually submitted to the database. I use php page to load the questions
// and include the 2 javascript files. The system works, other than not having a delay to ensure
// process.js loads AFTER a vote is confirmed sent to the db.

<?
$root=$_SERVER["DOCUMENT_ROOT"];

include("$root/js/forms/process.js");
include("$root/js/forms/filter.js");


?>
});

</script>
Thanks for your time.

Re: Simple Jquery POST Question

Posted: Sun Mar 28, 2010 12:27 pm
by jbh
One solution I am working with is replacing the $post line with this:

$.post("vote.php", $("#firstform_25").serialize(),
function(data){
//alert("Data Loaded: " + data);
}

);

It would alert the data and then load the pop up window. Clearly, we cannot have an alert so I commented it out.
I will update if it seems to do the trick. I just wonder, logically, if you feel this is all that is needed? This way an actual
function is loaing after the $post (vote command) loads before hitting process.js etc

Am I on the right track in your opinion?

Thanks.

PS. Doesn't seem to do anything. How do I just create a bit of a 'delay' so I only load process.js when the vote is confirmed to have
been submitted? Having the pop up with the bar graph without counting the recent result is a killer.

Re: Simple Jquery POST Question

Posted: Wed Mar 31, 2010 3:45 pm
by jbh
Is there a place on planet earth where a person can find out how to do this?
I've lost so much money on this project and I just need to be pointed in the right direction. The docs at jquery
is the only place? Nobody has done something like this?

Just make 2 js files load after a function loads...anywhere I ask I get crickets. This must be rocket science.

Re: Simple Jquery POST Question

Posted: Wed Mar 31, 2010 4:17 pm
by jbulaswad
jbh,

You are on the right path in that you do need to specify a callback parameter in the jQuery post call. Reference: $.post(url, data, callback).

With the limited amount of information I have about what is contained within process.js and filter.js or what file vote.php is I can tell you that you most likely need conditions around your includes. Is the code you posted contained within vote.php? What is contained within process.js that does not allow you to execute it within your callback?

I can assist you further given you provide additional information.