PNS
PNS

Reputation: 770

returning from a form submit call to a .php file

I have a questionnaire in a form. In the end of it the submit button is pressed that is supposed to call a .php file that inserts data into a database through its action information and afterwards show the last page that contains something like "thank you for participating etc." via the onsubmit info.

problem is that the last page is shown before the .php file is shown which means it is visible only for like half a second and then the php script is carried out which ends up showing a blank page.

The php script works it inserts data into the questionnaire correctly so there should be no mistakes syntax-wise.

any ideas if I have to exit the cript or something and return to the .html file or what could be wrong?

Upvotes: 2

Views: 10439

Answers (5)

Kitsune
Kitsune

Reputation: 9341

A few ways you could accomplish that.

You could make the php file it submits send out the data for the "thank you for participating" page (if you're fine with simply going to another page).

Alternatively, if you want to stay on the same page but just show the "thank you" notification, I would use JavaScript to disable the default action (e.preventDefault(); in the event handler) for the "submit" button on the forum, then also use JavaScript to use AJAX to submit the data.

An example (using JQuery), which won't change your page and perform the submit in the background, and display the "thank you" when done, on the current page.

$("a#confirmSubmit").click(function(e) {
    e.preventDefault(); // Prevents the submit button from changing pages

    data = {
        Name: $("input#Name").attr("value")
                    // Add other data here also
    };

    $.post("/page/to/submit/to.php", data, function(d) {
        //Write the code here to show the "thank you" notification.
        //It will show upon completion here.
    });
});

If you want to check for errors with inserting into the DB, you could check the value of the data of the AJAX call, to conditionally show the error. You can then return the user to the exact same form they were already on, with all the data still there, and show them an error message.

Upvotes: 1

Ruofeng
Ruofeng

Reputation: 2350

The way I deal with this is putting the html contents into the php file.

<?php 

if (!isset($_POST["submit"])) { // if page is not submitted to itself echo the form
?>

<html> 
<head> 
<title>survey</title> 
</head> 
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
    ...... (your form) ......
    <input type="submit" value="submit" name="submit"><br />
</form><br />
</body>
</html>

<?
} 
else { 

    $db = new PDO('...');
    $db->exec(...);
    echo "Thank you!";
}
?>

Upvotes: 1

rackemup420
rackemup420

Reputation: 1567

on your opening form tag add action="submit.php"

then once it goes to that page when the submit button is hit add this to the bottom of that php page:

header("Location: successfull.html");

Upvotes: 4

MrJ
MrJ

Reputation: 1938

Why not submit the form to process.php then process it:

if(isset($_POST)){
    $name = $_POST['name'];
    // etc etc

    // if all error checks pass, then echo out - thanks for taking part in our survey!
}

What you're doing is submitting it, and it seems you're getting javascript to say 'thank you' but it is being submitted before this thank you message can be displayed - no harm in echoing this out on your .php page!!

Update

You mention about redirecting to a page afterwards, but this can be done by:

header("Location: where/to/go.php");
exit;

But you can't do this with the above (echoing out a success) since it will redirect straight away.

Upvotes: 1

prodigitalson
prodigitalson

Reputation: 60413

IT sounds like what youre doing is showing the message with Javascript via the onsubmit event - this happens before the request is even set to the server and the php script. Youd either need to do an ajax form submission and then display the message when the request completes or make the php script redirect to the success message page when it is done.

But this is all just speculation without seeing any code... you should post some :-)

Upvotes: 2

Related Questions