フォーム送信(ページの再読み込み)のデフォルトの動作を防ぐには、次のevent.preventDefault()ように使用する必要があります
$("#data").submit(function(event){
event.preventDefault();
//some code .....
});
また、ページを更新せずにフォームデータを投稿するphpには、次のようなjQueryで使用可能なajaxメソッドのいずれかを使用する必要があります.post()(メソッドを使用してフォーム値を送信しますHTTP POST) 。
$("#data").submit(function(event){
event.preventDefault();// prevent page reload
// Now post the form using Ajax
// $(this).serialize() will return an array of all form fields as
// name value pair
$.post('some_script.php',$(this).serialize(),function(data){
// Just to check if everything is working well
console.log('Form Submitted Successfully.');
// do whatever you want with the data
});
});
phpスクリプトがデータをjson形式で返す場合は、phpcontent-Typeを使用してヘッダーを設定するか、jQueryに4番目のパラメーターのようにasJSONを指定して返されるデータを処理させることができます。dataTypeJSON
$.post('some_script.php',$(this).serialize(),function(data){
// Just to check if everything is working well
console.log('Form Submitted Successfully.');
// do whatever you want with the data
},'json');