Solved

submit without page refresh using jquery/php

Posted on 2011-03-08
9
605 Views
Last Modified: 2012-05-11
I'm trying to get my form to submit without refreshing page using jquery.  I used this code on a login page and it works fine.  BUT, when I try to use to update info in mySQL database, the jquery is not even functioning when i click Update button on form.  Now when i test form to see if it works when submitting via "action" in form tag, the update is successful...Please take a look at what I'm doing wrong....

<html>
<head><title></title>

<script src="js/jquery.js" type="text/javascript" language="javascript"> </script>

<script type="text/javascript">
$(document).ready(function()
{
     $("#contentform").submit(function()
{
$.post("useredit_update.php",{ id:$('#id').val(),fname:$('#fname').val(),lname:$('$lname').val(),rand:Math.random()},function(data)
{
if(data=='yes')
{
$("$msgbox").fadeTo(200,0.1,function()
{
$(this.html('Update Successful').addClass('messageboxok').fadeTo(900,1);
});
}
else
{
$("#msgbox").fadeTo(200,0.1,function()
{
$(this).html('Error with Update...').addClass('messageboxerror').fadeTo(900,1);
})l
}
})l
return false;
})l
})l
</script>

<link rel="stylesheet" href="css/user_edit.css" type="text/css" media="screen" />

</head>

<?php

$id=$_GET['id'];
include("db_connect.php");

query statement here....

while loop here....
}
?>
<body>

<form id="contentform" name="contentform" 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 $fname;?>"/></td>
</tr>
<tr>
<td colspan="2"><label for="lname">Last Name</label></td>
<td colspan="2"><input type="text" name="lname" id="lname" tabindex="1" value="<? echo $lname;?>"/></td>
</tr>
--- other form fields listed---
<tr>
<td colspan="2"><input type="submit" id="submit" name="submit" value="Update Record" /></td>
<td colspan="2"></td>
</tr>
</table>
</form>
<div class="buttondiv">
<span id="msgbox" style="display:none"></span>
</div>
</body>
</html>

As I stated above, using the jquery code at top of form worked fine when I used it in a login form, but now it seems the jquery code isnt even being acknowledge at all. I did test to make sure the useredit_update.php file did update successfully thru a normal form method/action post.
0
Comment
Question by:melvint91
  • 5
  • 3
9 Comments
 
LVL 82

Expert Comment

by:hielo
Comment Utility
>> ...
>> $(this).html('Error with Update...').addClass('messageboxerror').fadeTo(900,1);
>> })l  <==Garbage Character here
>> }
>> })l  <==Garbage Character here
>> return false;
>> })l  <==Garbage Character here
>> })l <==Garbage Character here
>> </script>
>> ...
clean the "garbage" characters pointed out above.
0
 
LVL 82

Expert Comment

by:hielo
Comment Utility

<script src="js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function()
{
     $("#contentform").submit(function()
	{
		var params=$('#contentform').serialize()+'&rand='+(new Date()).valueOf() ;
		$.post("useredit_update.php",params,function(data)
		{
			if(data=='yes')
			{
				$("$msgbox").fadeTo(200,0.1,function()
				{
					$(this.html('Update Successful').addClass('messageboxok').fadeTo(900,1);
				});
			}
			else
			{
				$("#msgbox").fadeTo(200,0.1,function()
				{
					$(this).html('Error with Update...').addClass('messageboxerror').fadeTo(900,1);
				});
			}
		});
	return false;
	});
});
</script>

Open in new window

0
 

Author Comment

by:melvint91
Comment Utility
hielo: excuse the garbage character...it's not in the actual code, I had to retype all code so you had something to see.  I'm working from a classified server and I cant copy and paste.  I will test your last solution and reply with a success or failure outcome.
0
 

Author Comment

by:melvint91
Comment Utility
heilo:  your solution wasn't successful.  There has to be something wrong because I copied my original code I posted from another script I used for my Login form process and it worked like a charm.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 82

Expert Comment

by:hielo
Comment Utility
On the code you provided you have:
$.post("useredit_update.php",...);

Is the code you provided the code for useredit_update.php? I noticed that your php code is using:
$id=$_GET['id'];

BUT your jquery is sendting a POST request, NOT a GET request.

0
 
LVL 82

Expert Comment

by:hielo
Comment Utility
Regarding my SECOND post,
On line 12, change $("$msgbox") to $("#msgbox")

On line 14, change $(this.html to $(this).html
0
 

Author Comment

by:melvint91
Comment Utility
Still trying to get this to work...
0
 

Expert Comment

by:shubham-aggarwal
Comment Utility
That should work .There were some syntax / Selector error on line 11 and 17 .
$(document).ready(function() {
    $("#contentform").submit(function() {

        $.post("useredit_update.php", {
            id: $('#id').val(),
            fname: $('#fname').val(),
            lname: $('#lname').val(),
            rand: Math.random()
        },
        function(data) {
            if (data == 'yes') {
                $("$msgbox").fadeTo(200, 0.1,
                function() {
                    $(this).html('Update Successful').addClass('messageboxok').fadeTo(900, 1);
                });
            } else {
                $("#msgbox").fadeTo(200, 0.1,
                function() {
                    $(this).html('Error with Update...').addClass('messageboxerror').fadeTo(900, 1);
                });
            }
        });
        return false;
    });
});

Open in new window

0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
Comment Utility
copy and paste the following and give it a try:
<script src="http://code.jquery.com/jquery-1.5.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
     $("#contentform").bind('submit',function()
	{
		var params=$('#contentform').serialize()+'&rand='+(new Date()).valueOf() ;
		$.post("useredit_update.php",params,function(data)
		{
			if( /\byes\b/i.test(data) )
			{
				$("#msgbox").fadeTo(200,0.1,function()
				{
					$(this).html('Update Successful').addClass('messageboxok').fadeTo(900,1);
				});
			}
			else
			{
				$("#msgbox").fadeTo(200,0.1,function()
				{
					$(this).html('Error with Update...').addClass('messageboxerror').fadeTo(900,1);
				});
			}
		});
	return false;
	});
});
</script>

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
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)
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…

743 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

17 Experts available now in Live!

Get 1:1 Help Now