提交位于同一页面上的表单而不刷新页面

so here it is a confusing battle. i want to submit a form that is on the same page as my php validation.

html

 <form id="form">
  //fields
  <input type="submit" id="submit">
  </form>

<?php
/ /check if form is good
?>

jquery

$("#actvpost").submit(function(){
    //code
  return false;
});

when i hit submit the page still refreshes how can i have it to just send the data without using an external page

i can do it without jquery but it will refresh the page, i would like to attach jquery to so that it does not refresh the page.

Maybe:

$("#actvpost").submit(function (event) {
  event.preventDefault();
  // here you may by your self send request and get response
});

And maybe you would use something like jQuery Form Plugin and use ajaxForm function.

The id of your form should be actvpost not form, as you are referencing actvpost in your jQuery selector.

Your submit function should not return false, but should instead make use of event.preventDefault:

$("#actvpost").submit(function( event ){
    event.preventDefault();
    // your code
});

The event object

All events are passed an event object as their first argument, on trigger. This object contains many useful properties about the event, such as event.target and event.type (which returns click, keyup, ect...). jQuery also extends this object with useful methods such as preventDefault()

Although it is not necessary, I do recommend putting the event.preventDefault() early, if not first in your handler. This, again, not imperative, but a good practice if you plan on writing more complex code in the future. Organization is key, everything should follow some sort of common convention. It is much easier to solve problems when you code with any convention, let alone a common convention.

Essentially, you cannot run PHP without a new HTTP request, so alternatively, you may want to use solely javascript validation before submit, with server side validation as a last line of defense. To submit the form to your PHP script, you would want either:

  • Use AJAX (jQuery makes this simple)
  • Submit to an iframe, while listening on its "load" event for a response in Javascript

Both of which make a new HTTP request and make the response available in javascript.

Replace:

<form id="form">

With:

<form id="actvpost">