Solved

submit form without refresh using Jquery and Ajax

Posted on 2011-02-23
2
973 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

687 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