PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 5th, 2013, 07:18 AM
notflip's Avatar
notflip notflip is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 148 notflip User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 39 m 9 sec
Reputation Power: 1
Can Ajax check if a Query succeeded?

I'm using Ajax to update a text in my database but I'm wondering if php can send a response back with Ajax if the Query is succesfull

Code:
        $.ajax({
          type: 'POST',
          url: 'test.php',
          data: 'text=' + text,


and on my test.php page

Code:
if($_POST){
	try {
		$insert_query = $db->prepare('UPDATE fields SET content = :content WHERE name = "homepage_mission" ');
		$insert_query->execute(array('content' => $_POST['text']));
	}
	catch(PDOException $e){
		print $e->getMessage();
		die();
	}
}


Help would be loved! thanks

Reply With Quote
  #2  
Old February 5th, 2013, 07:26 AM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,879 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 2 Weeks 1 Day 56 m 52 sec
Reputation Power: 581
It is not clear what you want to accomplish. If the query is initiated by a form submit then PHP can report success/failure directly. If the query is initiated by an Ajax call then the answer is yes.
__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.

Reply With Quote
  #3  
Old February 5th, 2013, 07:28 AM
Bronzesoda Bronzesoda is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 1 Bronzesoda User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 28 sec
Reputation Power: 0
Code:
$.ajax({
  type: 'POST',
  url: "test.php",
  data: { example: "text" },
  success: function(msg) {
    alert('success');
  }
});

Reply With Quote
  #4  
Old February 5th, 2013, 07:32 AM
Northie's Avatar
Northie Northie is offline
Square Peg in a Round Hole
Click here for more information.
 
Join Date: Oct 2007
Location: North Yorkshire, UK
Posts: 3,420 Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 10 h 27 m 11 sec
Reputation Power: 3896
yes

whatever your php page outputs is sent back to the client (think normal page, but asked for by javascript).

Now, it looks like you are using jQuery, which has a myriad of options for the ajax method.

many of the call back functions take this responded info as an argument.

A favourite for beginners is "complete", eg

Code:
$.ajax({
          type: 'POST',
          url: 'test.php',
          data: 'text=' + text,
          complete:function(a,b) {
          	if(b == 'success') {
          		var d = a.responseText; //this is the data the server 'sent' back
          		
          		//process d
          		
          		//eg $("body").append(d);
          		
          		//or
          		
          		//var json = eval("("+a+")") //if someone knows a cleaner way, please let me know! (dataType:JSON has never worked for me)
          		
          	} else {
          		//something went wrong
          	}
          }
});
__________________
PHP OOPS! <?php DB::Execute(SQL::makeFrom($_GET))->fetchArray()->FormatWith(Template::getInstance('default'))->printHtml(); ?>

PDO vs mysql_* functions: Find a Migration Guide Here

[ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]

Reply With Quote
  #5  
Old February 5th, 2013, 07:44 AM
notflip's Avatar
notflip notflip is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 148 notflip User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 39 m 9 sec
Reputation Power: 1
Quote:
Originally Posted by Bronzesoda
Code:
$.ajax({
  type: 'POST',
  url: "test.php",
  data: { example: "text" },
  success: function(msg) {
    alert('success');
  }
});


What is this 'msg' variable? I don't quite understand how or what PHP is sending back? Could you give me an example please?

Thanks!

Reply With Quote
  #6  
Old February 5th, 2013, 07:46 AM
notflip's Avatar
notflip notflip is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 148 notflip User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 39 m 9 sec
Reputation Power: 1
Quote:
Originally Posted by Northie

many of the call back functions take this responded info as an argument.

});[/CODE]


Aha! i found it thanks to this. So it doesn't matter what the name of the argument is, it's always from the server. how nice! thanks !

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Can Ajax check if a Query succeeded?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap