Solved

submit form without refresh using Jquery and Ajax

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

705 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

22 Experts available now in Live!

Get 1:1 Help Now