TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-11-2010, 05:50 PM   #1 (permalink)
The Contributor
 
Join Date: Feb 2007
Posts: 64
Thanks: 9
Killswitch is on a distinguished road
Default Jquery ajax forms in PHP MVC app

I was wondering about this since I am very new to javascript and Jquery in general.

My app uses the MVC approach. Using the ajax. method for submitting a form in Jquery means I need to make the request to a php page. I have tried sending it to site.com/mycontroller/mymethod but it always fails.

If I send it over to a seperate PHP page outside of the MVC app however, it works. Strangely though, even if that page does nothing but return false it still says it works.

Maybe I am not doing this right, but here is the jquery...

Code:
$(document).ready(function(){

	// Hide status message by default
	$(".error").hide();
	$("#error").hide();
	$("#success").hide();

	// Create content
	$("#submit").click(function(){		
	
		// Hide status message by default
		//$("#error").hide();
		//$("#success").hide();
		
		var hasError = false;
		
		// Check that we have a title
		var title = $("#title").val();
		if(title == '') {
			$("#title_error").show();
			hasError = true;
		}	
		// Check that we have a url
		var url = $("#url").val();
		if(url == '') {
			$("#url_error").show();
			hasError = true;
		}	
		// Check that the full story and short story was not left blank
		var short = $('#short').val();
		var full = $('#full').val();
		if(short == '' && full == '') {
			$("#short_error").show();
			$("#full_error").show();
			hasError = true;
		}	
		// Get form data
		var formData = $('#create_form').serialize(); 
		
		// Check that we dont have errors
		if(hasError == false) {
			$(this).hide();
			
			$.ajax({
				type: "POST",
				url: "http://localhost/framework2/public/js/ajax/php/content_create.php",
				data: formData,
				cache: false,
				success: function(){
					$('#success').fadeIn("slow");
				},
				error: function() {
					$('#error').fadeIn("slow");
				}
			});
		}
		else {
			$('#error').fadeIn("slow");
			$(this).show();
		}
		return false;
	});						   
});
Anyone here help with this? I followed a mixture of tutorials to learn how to do this, but honestly most of this was thrown together without the tutorial (all the error checking stuff mostly).

Am I missing anything here? Shouldn't returning false trigger the error option in Jquery? Also, is it possible to send the request to a method in my MVC app? I would much prefer to keep this in the APP so I don't have to get a new instance of the database and have access to all my helpers.
Killswitch is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and AJAX 9three Javascript, AJAX, E4X 0 09-27-2009 09:26 PM
Using AJAX and PHP to check filemtime($filename) buildakicker General 4 05-21-2009 10:15 PM
Help on PHP MYSQL Forms Input deesudesu Absolute Beginners 1 03-20-2009 10:50 AM
AJAX ticker in jQuery Orc Javascript, AJAX, E4X 3 02-09-2009 05:31 AM
Rating System: AJAX, MYSQL, PHP Orc General 6 02-26-2008 05:09 PM


All times are GMT. The time now is 11:25 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design