Solved

submit form without refresh using Jquery and Ajax

Posted on 2011-02-23
2
963 Views
Last Modified: 2012-06-22
I've tried to get my form submittal to work using jquery/ajax but It's not working and I know it has to be the Jquery because the form submit works when I submit it via form action="Useredit_update.php"... but I want to show update was successful in same page..please help...see code below:

Usredit.php

<script type=”text/javascript”>
$(document).ready(function(){
Var id = (“#id”).val();
Var fname = (“#fname”).val();
….other form fields;
Var dataString = ‘id=’ + id + ‘&fname=’ + fname + ‘&other form fields=’;
$.ajax({
Type: “POST”;
url: “useredit_update.php”,
data: $(dataString).serialize(),
success: function(msg) {
$(“#display_result”).text( msg );
}
});
Return false;
});
});
</script>
<?php

…php code here to pull data from database

?>

<form id=”usercontent” name=”usercontent” method=”post” action=””>
<table>
<input type=”hidden” name=”id” id=”id” value=”<? Echo $id; ?>”>
<tr>
<td colspan=”2”><label for=”fname”>First Name</label></td>
<td colspan=”2”><input type=”text” name=”fname” id=”fname” tabindex=”1” value=”<? Echo $lname; ?>” /></td>
</tr>
…… other tr displaying remaining rows of data coming from db…..
</table>

</form>
<div id=”display_result”>

======================

Useredit_update.php

<?php
//form fields
$ud_id = trim($_POST[‘id’]);
$ud_fname = trim($_POST[‘fname’]);
$ud_lname = trim($_POST[‘lname’]);
// other fields listed but I didn’t retype to show example

//connect to db --- I have no problem with db connection and query updating when ran by itself and without ajax/jquery interaction
Include(“db_connect.php”);
 $query=”UPDATE table-name SET fname=’$ud_fname’, --rest of fields—
WHERE id=’$ud_id’”;

Mysql_query($query) or die (‘Error updating database’);
Echo “Record Updated Successfully”;
//close db
Mysql_close();

?>


0
Comment
Question by:melvint91
2 Comments
 
LVL 15

Accepted Solution

by:
dirknibleck earned 250 total points
ID: 34964091
You want your ajax call to be captured by the submit event:

$("form#usercontent").submit(function(event){
     $.ajax({
Type: “POST”;
url: “useredit_update.php”,
data: $(dataString).serialize(),
success: function(msg) {
$(“#display_result”).text( msg );
}
});

     event.preventDefault();
});

Open in new window


But you'd probably be best served by googling jquery.forms.js and using this plugin instead.
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 250 total points
ID: 34965595
Javascript keyword is case sensitive : << return >> and not << Return >>, var and not Var and so on...

Another thing, the parameter in the ajax call is << type >> and not << Type >>

Confirm it's useredit_update.php and not Useredit_update.php
$(document).ready(function() {

  $("#usercontent").submit(function(event){
     event.preventDefault();
     $.ajax({ "type":"POST", "url":"useredit_update.php", "data": $("#usercontent").serialize(), "success": function(msg) { $("#display_result").html( msg ); }});

  });

});

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question