0

I''m trying to process a form without page reload using jQuery post as shown below:

<script type="text/javascript">
$('#settingsForm').submit( function(){  $('input[type=submit]', this).attr('disabled', 'disabled ');});

function scheck(){

    var oldanswer = $("#oldanswer");
    var newquestion = $("#newquestion");
    var newanswer = $("#newanswer");
    var url = "settingsupdate.php";

    //This part of the code works fine. Errors are displayed normally
    if(oldanswer.val() == ""){
        $('#output').html('Oops you left a field empty. Please fill it').show().fadeOut(6000);
    }
    else if (newquestion.val() == ""){
        $('#output').html('Oops you left a field empty. Please fill it').show().fadeOut(6000);
    }
    else if (newanswer.val() == ""){
        $('#output').html('Oops you left a field empty. Please fill it').show().fadeOut(6000);
    }

    else{
        $('#FormProcessor').show();

    $.post(url,{oldans: oldanswer.val(), newquest: newquestion.val(), newans: newanswer.val()},        function (data){
        $("#security_check").slideup("fast");
        $("#output").html(data).show().fadeOut(10000);

        document.settingsForm.oldanswer.value = "";
        document.settingsForm.newquestion.value = "";
        document.settingsForm.newanswer.value = "";
        $('#FormProcessor').hide();
        });
    }
}

    </script>

settingsupdate.php (simplified)

if(isset($_POST['submitset'])){ //the submit button
    $oldans = htmlspecialchars($_POST['oldans']);
    $newquest = htmlspecialchars($_POST['newquest']);
    $newans = htmlspecialchars($_POST['newans']);

    echo $oldans;
}

When i submit the form, the processor image appears ($('#FormProcessor').show();) and nothing happens. The page just remains static. Nothing is echoed from the php script back to the page. The check for empty fields as shown above however, works normally, which means the form is posted but the jQuery post call isn't working. What's my error?

Edited by dhani09: improved formating

3
Contributors
6
Replies
32
Views
3 Years
Discussion Span
Last Post by dhani09
0

Since you post with jQuery instead of the submit button click, isset($_POST['submitset']) will always be false because it is not included in your post data.

0

You are checking the $_POST['submitset'] in the PHP code

if(isset($_POST['submitset']))

But, in the $.post you are not including that value

$.post(url,{oldans: oldanswer.val(), newquest: newquestion.val(), newans: newanswer.val()}, function (data) {
...
}

So if(isset($_POST['submitset'])) always returns false

Try this:

$.post(url,{oldans: oldanswer.val(), newquest: newquestion.val(), newans: newanswer.val(), submitset: "true"}, function (data){
...
}
0

I adjusted it to what you suggested but still no luck. I changed if(isset($_POST['submitset'])) to if(isset($_POST['newans'])) and still no output. I then removed entirely the line:
if(isset($_POST['submitset'])){} but there's still no output.

0

print_r($_POST) outputs Array() when i load the script alone. But theres no output when i click the submit button in the form.

1

Found the error in the console. It was with the slide function. I incorrectly typed slideup() instead of slideUp(). It works now.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.