0

So all i need to do is refresh a variable displayed on a php page which is stored in a MySQL db. This value is an int which is subtracted by 1 everytime the submit button from a form is clicked. As i've opted to use AJAX to post the form the page isn't being refreshed, therefore the value isn't being updated along with the form submission.

$qry = mysql_query("SELECT codes_remaining FROM users WHERE email= '".$_SESSION['email']."'");
while($row = mysql_fetch_array($qry)) {
    if ($row['codes_remaining'] ==1 ) 
    {
    echo "You have ".$row['codes_remaining'].' code remaining';
    }
    else {
echo "You have ".$row['codes_remaining'].' codes remaining';
}
}

So this code just displays how many "codes" a person has left. I need this value to be refreshed once the submit button has been clicked from the form on the same page.

I'm using the following JavaScript to not refresh the page.

$("#form-submit").click(function(e) { 
        e.preventDefault();
        $.ajax({
        cache: true,
        type: 'POST',
        url: 'process-register.php',
        data: $("#form-register").serialize(),
        success: function(response) {
            $("#output-div").html(response);
        }
        });
    }); 

Thanks, LS

3
  • 1
    Your PHP code appears to be vulnerable to SQL injection attacks. Commented Sep 17, 2011 at 22:24
  • So what exactly are you stuck with? Commented Sep 17, 2011 at 22:31
  • Well, I need the value to be refreshed once a submit button has been clicked. As I said, I'm using AJAX to process the form so at the moment the page doesn't refresh therefore the value doesn't refresh.. Commented Sep 17, 2011 at 22:41

3 Answers 3

1

If you'd like to update the value, do it like this (jQuery is easiest):

$(".submit").click(function(event) { 
     event.preventDefault();
     $(this).load('file.php',function(val){
        $('#output').text(val);
     });
 });

And in file.php:

<?php
    connect_to_db();
    $returned = get_info_from_db();
    echo $returned;
?>

The jQuery will grab the info on file.php and put it into #output.

Sign up to request clarification or add additional context in comments.

Comments

1

Maybe It's just me, but why not use jQuery .load function?

    $("#form-submit").click(function(e) { 
        e.preventDefault();
        $(this).load('process-register.php');
    });

Maybe not ethical nor the correct way of doing this but everytime you click on #form-submit, it loads that file and therefore processes it everytime. Also note that if you load a file that uses MySQL connection yes has no mysql_connect or mysql_select_db configured, it obviously won't work. I've had that for quite some times.

Comments

0

In your 'success', you could possibly just throw in

$("#WhereYouWantTheOutput").load("process-register.php");

That way whenever your submit succeeds, it'll also load the output for you. Just replace #WhereYouWantTheOutput with the name of where you want the output placed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.