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?

Recommended Answers

All 6 Replies

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.

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){
...
}

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.

Let the script output print_r($_POST); so you can see what it contains.

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.