Simple ajax fix?

I'm trying to see if a user is attempting to use an Order ID that already exists.

My ajax code
$.ajax({
  type: "GET",
  url:'validateOrderNumber.php', 
  data: "companyID=" + $("#companyID").val() + ",orderID='" + $("#orderID").val() + "'", 
  dataType: 'json',
  success: function(data)
  {
    //Process results... should be either found or not found
  },
  error: function(XMLHttpRequest, textStatus, errorThrown) { 
    alert("Status: " + textStatus); alert("Error: " + errorThrown); 
  }       
});

Open in new window


The validateOrderNumber.php page:
<?php
	include 'DBConnectionStuff.php';

	$query = "select * from orders where customerID = " . $_GET["companyID"] . " and orderID = " . $_GET["orderID"];
	$objConnection = new mysqli(Connection stuff);
	
	$result=mysqli_query($objConnection,$query);

	if (!$result) trigger_error('QUERY FAIL', E_USER_ERROR);

	if(mysqli_num_rows($result) > 0){
		echo json_encode("found");
	}else{
		echo json_encode("Not found");
	}

	mysqli_close($objConnection);
?>

Open in new window


I get a parseerror... unexpected token <
Sheldon LivingstonConsultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
I see you're still using unescaped data in your query string.  Please check that - you really don't want that risk hanging over your application!

Have a look at this article for an example of how to send AJAX requests to a server-side script.
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/A_10712-The-Hello-World-Exercise-with-jQuery-and-PHP.html

If you can post the rest of the client-side app (all HTML and JavaScript) I'll try to show you how to adapt it to work correctly.
0
Sheldon LivingstonConsultantAuthor Commented:
I'm afraid that I really don't care about security, etc at this point.

I've spent a few hours just trying to get this to work.  If and when it works then I'll try and worry about other stuff.  

It amazes me as to how difficult this really is...

Anyway... this seems like a fairly simple task that shouldn't take more than 15 - 20 hours (of googling) to accomplish... yet it appears that it will.
0
Ray PaseurCommented:
Let's deconstruct the problem.  What's the central issue here?  

Is it getting the data from the client to the server?  If so, a simple "bounce request" script like the one in the article can help.

Is it getting the query to work correctly?  If so, you should be able to test the query from the browser address bar.

Or is it something else?
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Ray PaseurCommented:
Also, if you're not using Chrome Dev Tools, you might want to learn about them - makes debugging interactive applications much easier!  Some links:
https://developer.chrome.com/devtools
http://discover-devtools.codeschool.com/
https://www.codeschool.com/courses/discover-devtools
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Slick812Commented:
greetings classnet, , Yes, understanding the javascript AJAX send and receive process, can be very difficult at first for most people. In your code work for the Jquery Ajax you have, the data type as JSON -
     dataType: 'json',

For your type of return, this is not the dataType to use, Because it is rough to see any PHP errors, that have cause your AJAX to mal-function.

you get this Error - "parseerror... unexpected token < "

In my experience, the PHP engine has thrown some sort of warning or fatal error, which it outputs with a HTML tag (many times a <b> tag), and this will prevent ANY and ALL JASON from being recognized.

For deveopment you might try this code -
$.ajax({
  type: "GET",
  url:'validateOrderNumber.php', 
  data: {companyID: $("#companyID").val() , orderID: $("#orderID").val()},
// No DataType set, uses the Default
  success: function(received)
  {
    alert("success text= "+received);
  },
  error: function(XMLHttpRequest, textStatus, errorThrown) { 
    alert("Status: " + textStatus); 
alert("Error: " + errorThrown); 
alert("returned string: " +XMLHttpRequest..responseText);
  }       
});

Open in new window


and see the alerts for development and learning.
Please notice that I used the Proper way to do a the send data as -
     data: {companyID: $("#companyID").val() , orderID: $("#orderID").val()}

This uses a javascript Object notation, and then the Jquery will do the necessary and proper way to build the URL parameter string
0
Slick812Commented:
Oh, I forgot to mention, you should change the PHP  page output from -
    echo json_encode("found");

Which makes no sense to me, an NOT USE the json_encode( ) , maybe like-

    echo $_GET["orderID"]." : Found a Row";
    else
    echo "Not found for : ".$_GET["orderID"];
0
Sheldon LivingstonConsultantAuthor Commented:
Thanks for the help folks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.