Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 407
  • Last Modified:

AJAX Newbie Question

Ok, so I'm very new to AJAX within Joomla.  I can get JavaScript to manipulate lots of things within Joomla, but I can't seem to use AJAX correctly with a database.

Here's what I'm trying to do:

1) I need to have the user enter a material number
2) There is a quick check to see if that material number is in use.
3) A message pops-up and says that the material number is already in use.

Here's the code I currently have:

JavaScript Side  (the function ff_material_num...etc...is necessary as I'm using this within Breezing Forms):

 
function ff_material_num_action(element, action)
 {
   switch (action) {
     case 'change':
    jQuery.ajax({ 
                   //starts the ajax request
    type: "POST",            //define method "GET" or "POST"
    url: "http://10.36.112.12/manufacturing/components/com_jumi/files/management/materialNumber_request.php",            //the PHP file to call for DB queries or other processing
    data: {code: ff_getElementByName('material_num').value},     //values to pass to the PHP file
 
   
   success: function(response){           //succes callback function starts here
       if (response != ''){
         alert('Thanks');      
         
       }
       else alert('Sorry');
       } 
   });
 
      break;
     default:;
   } // switch
 } // ff_material_num_action

Open in new window


Here's my server side code:
 
<?php

require_once('db.php');

$materialNumber = $_POST['material_num'];

$count=mysql_num_rows(mysql_query("SELECT * FROM jos_a_materialnum where material_num = $materialNumber"));

if ($count == 1) {
echo 'yes';	
} else if ($count == 0) {
echo 'no';	
} else {
echo 'nothing to report';	
}


//$test = 'test';
//echo $test;

?>

Open in new window


What do I need to do to be able to get this to validate correctly or at least bring back a response. Right now it just brings back: 'undefined'.

Any help is much appreciated as I'm just getting my feet wet with this.

Ultimately I would like to bring back a string as a response...and then display that string, can I do that as well?
0
Andrew Derse
Asked:
Andrew Derse
  • 9
  • 5
1 Solution
 
Ovid BurkeCreative DirectorCommented:
Was it you, that defined the function 'ff_meterial_num_action()'?
0
 
Andrew DerseIT ManagerAuthor Commented:
Yes, it's actually part of breezing forms.  This is needed to talk with the rest of the form, the database, and Joomla...

Forget the function stuff...I just need to know how to use this code to get a response from the php script:
 
jQuery.ajax({ 
                   //starts the ajax request
    type: "POST",            //define method "GET" or "POST"
    url: "http://10.36.112.12/manufacturing/components/com_jumi/files/management/materialNumber_request.php",            //the PHP file to call for DB queries or other processing
    data: {code: ff_getElementByName('material_num').value},     //values to pass to the PHP file
 
   
   success: function(response){           //succes callback function starts here
       if (response != ''){
         alert('Thanks');      
         
       }
       else alert('Sorry');
       } 
   });

Open in new window


I just need to get a response like 'yes' or 'no' but I'm getting undefined...
0
 
Andrew DerseIT ManagerAuthor Commented:
I know I can do this without Breezing Forms, but I like using it because it builds my forms fast and dumps them into a database and emails the end user all-at-once...and I don't have to write much code for it.

I'm thinking about just not using it for this application...
0
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!

 
Kim WalkerWeb Programmer/TechnicianCommented:
I don't know jQuery, but I don't see anything in the ajax code that resembles a value named "material_num" in the data that's being passed. It appears that you are extracting a value from the form field named "material_num" and passing it as a value named "code." But again, I don't know jQuery.

There appears to be only three possible responses: yes, no, and nothing to report. Even if the ajax fails, you can only get responses "Thanks" or "Sorry" in a window alert. What are you seeing that leads you to believe the response is undefined? Are you using a tool to see the actual ajax response?
0
 
Andrew DerseIT ManagerAuthor Commented:
The AJAX script alerts me and says "Sorry"...for everything...

So I then change the:

alert('sorry');      to this:

alert(msg);

and it returned an alert message as 'undefined' or completely blank.

The material_num is coming from a form field...but even if I remove that...I just want to go out to the PHP script and return the echo...

so if you change the code to this:  
 
jQuery.ajax({ 
                   //starts the ajax request
    type: "POST",            //define method "GET" or "POST"
    url: "http://10.36.112.12/manufacturing/components/com_jumi/files/management/materialNumber_request.php",            //the PHP file to call for DB queries or other processing 
   
   success: function(response){           //succes callback function starts here
       if (response != ''){
         alert('Thanks');      
         
       }
       else alert('Sorry');
       } 
   });

Open in new window


And This:
 
<?php

echo 'test';

?>

Open in new window


I want to display the text 'test' to the user...how can I do that?
0
 
Kim WalkerWeb Programmer/TechnicianCommented:
Again, I don't know jQuery. But I would guess that alert(response) would alert you with the message "test" from the php file. Unless you have defined a variable somewhere name msg, alert(msg) SHOULD display "undefined" in the alert box.
0
 
Andrew DerseIT ManagerAuthor Commented:
Sorry, I am going between two different scripts now with this:

The alert(response) is also the same as alert(msg) for me...I keep changing between msg and response to see if anything will work...and nothing does.

So: alert(response) brings back 'undefined'.

If I were to code it like this:
 
jQuery.ajax({ 
                   //starts the ajax request
    type: "POST",            //define method "GET" or "POST"
    url: "http://10.36.112.12/manufacturing/components/com_jumi/files/management/materialNumber_request.php",            //the PHP file to call for DB queries or other processing 
   
   success: function(msg){           //succes callback function starts here
       if (msg != ''){
         alert(msg);      
         
       }
       else alert(msg);
       } 
   });

Open in new window


I would still get 'undefined'
0
 
Kim WalkerWeb Programmer/TechnicianCommented:
Is your php script "materialNumber_request.php" on a public server. When I try to load the page to see what's being returned, it times out. You should be able to load that page and get a simple text page with the word "test" in it. Or at least a blank page the the word "test" in the source code.
0
 
Andrew DerseIT ManagerAuthor Commented:
It's on a local server...internally...and it has been returning 'test' for me...

Just not in the AJAX functionality of it...
0
 
Kim WalkerWeb Programmer/TechnicianCommented:
It works for me. Here is my entire html test file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<title>Untitled Document</title>
		< script type="text/ecmascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
		< script type="text/ecmascript">
		function runtest() {
			jQuery.ajax({				//starts the ajax request
				type: "POST",			//define method "GET" or "POST"
				url: "./materialNumber_request.php",            //the PHP file to call for DB queries or other processing 
				success: function(msg) {		//succes callback function starts here
					if (msg != '') {
						alert(msg);
					} else {
						alert(msg);
					}
				}
			});
		}
		</script>
	</head>
	
	<body onload="javascript: runtest();">
		Test
	</body>
</html>

Open in new window


and my php code:
<?php

echo 'this is a test';

?>

Open in new window

0
 
Andrew DerseIT ManagerAuthor Commented:
Yes that works, but can we get it to work within Joomla?
0
 
Andrew DerseIT ManagerAuthor Commented:
OK, so this is what the code looks like now and what I see from my end...

Main HTML code...(This is inside of Joomla and Breezing Forms) :
 main
PHP File code:
 test.php
PHP Page:
 PHP Page
Main Page Response blank alert:
 blank
0
 
Kim WalkerWeb Programmer/TechnicianCommented:
Sorry, I don't know anything about Joomla either. I did find the following but it doesn't make any sense to me since I don't know joomla. About half way down the page is a section on jQuery.

http://www.packtpub.com/article/using-javascript-effects-with-joomla

Perhaps you should abandon this question and open an new one. I'll avoid the new question and maybe someone else will respond.
0
 
Andrew DerseIT ManagerAuthor Commented:
Nope I'll actually give you points for this.  Just need to code your solution above outside of Nreezing Forms and it will work...I need 1 day though.
0
 
Andrew DerseIT ManagerAuthor Commented:
Thanks.  I created a custom form outside of Breezing Forms and it worked just fine within Joomla.

Thanks again!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 9
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now