quick ajax form validation question...(solved)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

quick ajax form validation question...(solved)

Post 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);
}
}
 
?>
Last edited by scarface222 on Mon Sep 21, 2009 10:03 pm, edited 1 time in total.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: quick ajax form validation question...

Post 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
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: quick ajax form validation question...

Post 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.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: quick ajax form validation question...

Post by scarface222 »

Does anyone else have the knowledge to explain how to accomplish this?
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: quick ajax form validation question...

Post by Mirge »

scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: quick ajax form validation question...

Post 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.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: quick ajax form validation question...

Post 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.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: quick ajax form validation question...

Post 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;
    });
});
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: quick ajax form validation question...

Post 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); }
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: quick ajax form validation question...

Post 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.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: quick ajax form validation question...

Post 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.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: quick ajax form validation question...

Post 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.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: quick ajax form validation question...

Post 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!
Post Reply