Page 1 of 1

quick ajax form validation question...(solved)

Posted: Sun Sep 20, 2009 11:22 am
by scarface222
Hey guys I have simple jquery ajax form which posts to a seperate processing page and works fine except for the fact that if there is an error, I do not get the message on the page I post the form from. Is there a way to post the message back to the base page?

This is the processing script, works fine except the alert message

Code: Select all

<?php
 
 
include("includes/connection.php");
 
if (isset($_POST['t']))
{
$td=$_POST['t'];
$time=time();
//seconds
$timeout=$time-15;
$time_check="SELECT * FROM bump WHERE time>$timeout AND t ='$td'";
$time_result=mysql_query($time_check) or die('Error, check query failed');
$count = mysql_num_rows($time_result);
    
if ($count>=1){
    echo '  <script>
alert("You can only submit every 15 seconds");
</script>';
return;
}
else{
$query= "INSERT into submit VALUES ('$time', '$td')";
$query1=mysql_query($query);
}
}
 
?>

Re: quick ajax form validation question...

Posted: Sun Sep 20, 2009 1:17 pm
by Darhazer
If you call this with Ajax, you will return the output to the calling script, and the calling script should do something with it, for example to eval it.

P.S. you may be interested in reading about JSON

Re: quick ajax form validation question...

Posted: Sun Sep 20, 2009 2:11 pm
by scarface222
Thanks but how is the eval function going to get the message to my desired page where I made the initial post to the processing page. I will check JSON out.

Re: quick ajax form validation question...

Posted: Mon Sep 21, 2009 12:14 pm
by scarface222
Does anyone else have the knowledge to explain how to accomplish this?

Re: quick ajax form validation question...

Posted: Mon Sep 21, 2009 12:29 pm
by Mirge

Re: quick ajax form validation question...

Posted: Mon Sep 21, 2009 2:56 pm
by scarface222
Ok I do not want to load the remote page because that is redundant after the post. I did not see a way to accomplish what I asked on that link you sent me, correct me if I am wrong. I just want to know if its possible to get the error message back onto the page where the request was sent in the first place. There is first a jquery ajax post request that sends the data to the processing script I posted, then that processes fine except the error message. I just want to pass that along to the original page. If anyone has an idea please let me know and briefly explain because I am new to programming and still learning.

Re: quick ajax form validation question...

Posted: Mon Sep 21, 2009 3:16 pm
by Mirge
scarface222 wrote:Ok I do not want to load the remote page because that is redundant after the post. I did not see a way to accomplish what I asked on that link you sent me, correct me if I am wrong. I just want to know if its possible to get the error message back onto the page where the request was sent in the first place. There is first a jquery ajax post request that sends the data to the processing script I posted, then that processes fine except the error message. I just want to pass that along to the original page. If anyone has an idea please let me know and briefly explain because I am new to programming and still learning.
It is on the page I sent... when you use $.ajax()... your callback function has access to the data sent back from the script you sent an AJAX request to.

Re: quick ajax form validation question...

Posted: Mon Sep 21, 2009 3:37 pm
by scarface222
I am having trouble figuring this one out. No data is being sent back, just put into a database. I want the message back conditionally if the error is satisfied. Can you please outline what change I should make in this function. Also when you say callback do you mean success?

Code: Select all

<script type="text/javascript">
$(document).ready(function(){
    $("form#submit").submit(function() {
 
    // we want to store the values from the form input box, then send via ajax below
    
     var topic     = $('#t').attr('value');
 
        $.ajax({
            type: "POST",
            url: "bprocess.php",
            data: "t="+ t,
            success: function(){
                $('form#submit').hide();
                startCountDown(15, 1000, myFunction);
                $('#countDown').fadeIn();
                setTimeout(function(){
                $('form#submit').fadeIn();
                }, 15000);
                
                
 
                            }
        });
    return false;
    });
});

Re: quick ajax form validation question...

Posted: Mon Sep 21, 2009 4:03 pm
by Mirge
# success: function(){
# $('form#submit').hide();
# startCountDown(15, 1000, myFunction);
# $('#countDown').fadeIn();
# setTimeout(function(){
# $('form#submit').fadeIn();
# }, 15000);

success: function(text) { alert("Data returned from script: " + text); }

Re: quick ajax form validation question...

Posted: Mon Sep 21, 2009 7:06 pm
by scarface222
Thanks man but is there a way to get it to work with the
# success: function(){
# $('form#submit').hide();
# startCountDown(15, 1000, myFunction);
# $('#countDown').fadeIn();
# setTimeout(function(){
# $('form#submit').fadeIn();
# }, 15000);}

it only works if i delete that and sub your line. Also is there a way to only get it to pop up when the 15 second error comes. I tried using this: function(text) {text}
and deleting the <script> tags in my processing document because I thought it would just sub that code in that space when the error returned then make the window pop up.

Re: quick ajax form validation question...

Posted: Mon Sep 21, 2009 7:12 pm
by Mirge
scarface222 wrote:Thanks man but is there a way to get it to work with the
# success: function(){
# $('form#submit').hide();
# startCountDown(15, 1000, myFunction);
# $('#countDown').fadeIn();
# setTimeout(function(){
# $('form#submit').fadeIn();
# }, 15000);}

it only works if i delete that and sub your line. Also is there a way to only get it to pop up when the 15 second error comes. I tried using this: function(text) {text}
and deleting the <script> tags in my processing document because I thought it would just sub that code in that space when the error returned then make the window pop up.
You're not quite understanding... that's OK though, as you're at least trying.

# success: function(){
# $('form#submit').hide();
# startCountDown(15, 1000, myFunction);
# $('#countDown').fadeIn();
# setTimeout(function(){
# $('form#submit').fadeIn();
# }, 15000);

I would do something along the lines of:

success: function(response) {
if(response == "OK") {
// do your countdown etc stuff.
} else {
alert("error occurred. here it is: " + response);
}
}

Of course, that is a hacked together method and you really should look into JSON from PHP for anything more complex. Your script would have to print "OK" (and only that) in order for the response to contain "OK"... etc etc.

Re: quick ajax form validation question...

Posted: Mon Sep 21, 2009 9:37 pm
by scarface222
Thanks a lot man, I put it together and it worked. Appreciate your patience. Anyway, on a side note, what exactly is JSON in basic terms? I just want a basic definition so I have an idea if I have to start reading about it. I went to the site briefly but didn't really understand its purpose.

Re: quick ajax form validation question...

Posted: Mon Sep 21, 2009 9:39 pm
by Mirge
scarface222 wrote:Thanks a lot man, I put it together and it worked. Appreciate your patience. Anyway, on a side note, what exactly is JSON in basic terms? I just want a basic definition so I have an idea if I have to start reading about it. I went to the site briefly but didn't really understand its purpose.
Very good! Glad you got it resolved, and you did it mostly on your own too which reinforces your learning!

JSON stands for "Javascript Object Notation". More can be read about it at: http://en.wikipedia.org/wiki/JSON

As your scripts get more complex, JSON is fantastic!