Link to home
Start Free TrialLog in
Avatar of Sheldon Livingston
Sheldon LivingstonFlag for United States of America

asked on

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 <
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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.
https://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.
Avatar of Sheldon Livingston

ASKER

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.
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?
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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"];
Thanks for the help folks