Solved

submit form without refresh using Jquery and Ajax

Posted on 2011-02-23
2
959 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

864 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now