?
Solved

submit form without refresh using Jquery and Ajax

Posted on 2011-02-23
2
Medium Priority
?
977 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 1000 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 1000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

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 …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
Suggested Courses

762 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