Samuel Meddows
Samuel Meddows

Reputation: 36712

Submitting a from via JavaScript with out page refresh

I am currently submitting a simple PHP form via JavaScript using

document.add_driver.submit();

This method how ever redirects the browser to the address of the form action ie: <form name="add_driver" id="add_driver" action="../scripts/add_driver_script.php" method="post">

which then needs to again be redirected back to the initial page.

I am wondering how I would go about using JavaScript to still submit the form but with out the page redirecting.

Cheers.

Upvotes: 0

Views: 89

Answers (2)

Jason Mitchell
Jason Mitchell

Reputation: 289

The JavaScript: "document.add_driver.submit();" is just "Clicking" the submit button, which will cause it to redirect to the ../scripts/add_drive_script.php page.

It sounds like you're wanting to do this in the background, I'd suggest using jQuery, which is a JavaScript library with any AJAX related functions built in.

Rather than downloading the jQuery library to server to test it out, you can add the following into your pages header:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>

A simple function that you might use would be:

<script>

$("#add_driver").submit(function() {
    $.post("../scripts/add_driver_script.php", $("#add_driver").serialize());
});

</script>

Upvotes: 1

alex
alex

Reputation: 490163

Serialise the form's inputs, and then send it to your action with AJAX.

Research AJAX, and/or use a library to make things easier.

Upvotes: 1

Related Questions