Page 1 of 1

getting 500 on ajax call, any hints?

Posted: 2022-08-05 23:13
by D Oliveira
hey guys, can anyone help me figure out why it is returning 500 error?

js

Code: Select all

		var pass_val = $j('#tender1-container').val();
          	var tblname = 'workbook';

			$j.ajax({
				url: 'hooks/ajax/ajax-po.php',
				data: { ajax_pass: pass_val, ajax_pass1: tblname },
				success: function(data){
					if (data) {
						console.log(data);
						var tbl = jQuery.parseJSON(data);

            				if(tbl){

              				$j('#ct_name').val(tbl.ct_name);
              				$j('#ct_subdiv').val(tbl.subdivname);

              				};

					
					}else{

						console.log('no data');
					
					}
				}
			});
php

Code: Select all


<?php
 

	$currDir = dirname(__FILE__) . '/../..'; // use /.. for hooks
	include("$currDir/defaultLang.php");
	include("$currDir/language.php");
	include("$currDir/lib.php");

	$con = mysqli_connect($dbServer, $dbUsername, $dbPassword);
	mysqli_select_db($con,$dbDatabase);

	/* grant access to all users who have acess to the orders table */

	$mi = getMemberInfo();
	$admin_config = config('adminConfig');

	$ajax_pass = makesafe($_REQUEST['ajax_pass']);
	$ajax_pass1 = makesafe($_REQUEST['ajax_pass1']);


	$query_start = mysqli_query($con,"SELECT * FROM '{$ajax_pass1}' WHERE ID = '{$ajax_pass}'");

	$query_done = mysqli_fetch_array($query_start);

	
    echo json_encode($query_done);


?>





Re: getting 500 on ajax call, any hints?

Posted: 2022-08-06 12:37
by D Oliveira
this is weird because sqlValue() works but that not efficient ( field by field.... ) using mtsqli_fetch_array() would save way more time returning all values, if anyone knows how to fix this please share, in the meanwhile the following code works but you will have to query every field...

Code: Select all


	$query_start = "SELECT field FROM {$ajax_var1} WHERE ID = {$ajax_var2} ";

	$query_done = sqlValue($query_start);

    	echo $query_done;



Re: getting 500 on ajax call, any hints?

Posted: 2022-08-06 13:02
by D Oliveira
still not ideal but that way you dont need multiple ajax files:

Code: Select all



	$ajax_pass = makeSafe($_REQUEST['ajax_pass']);
	$ajax_pass1 = makeSafe($_REQUEST['ajax_pass1']);
	
	$ajax_pass2 = makeSafe($_REQUEST['ajax_pass2']);
	$ajax_pass3 = makeSafe($_REQUEST['ajax_pass3']);


	$query_start = "SELECT ct_name FROM {$ajax_pass1} WHERE ID = {$ajax_pass} ";

	$query_done = sqlValue($query_start);



	$query_start2 = "SELECT subdivname FROM {$ajax_pass3} WHERE ID = {$ajax_pass2} ";

	$query_done2 = sqlValue($query_start2);



	 $arr = array(
          "ct_name" => $query_done1,
          "subdivname" => $query_done2
      );

    echo json_encode($arr);


Re: getting 500 on ajax call, any hints?

Posted: 2022-08-06 15:09
by pbottcher
Hi David,

not sure, but can you check that you pass a value to the ajax call

Code: Select all

		var pass_val = $j('#tender1-container').val();
		console.log("value : "+pass_val);
          	var tblname = 'workbook';

			$j.ajax({
				url: 'hooks/ajax/ajax-po.php',
				data: { ajax_pass: pass_val, ajax_pass1: tblname },
				success: function(data){
					if (data) {
						console.log(data);
						var tbl = jQuery.parseJSON(data);

            				if(tbl){

              				$j('#ct_name').val(tbl.ct_name);
              				$j('#ct_subdiv').val(tbl.subdivname);

              				};

					
					}else{

						console.log('no data');
					
					}
				}
			});
also for you php why not use the standard calls?

Code: Select all

<?php
 

	$currDir = dirname(__FILE__) . '/../..'; // use /.. for hooks
	include("$currDir/defaultLang.php");
	include("$currDir/language.php");
	include("$currDir/lib.php");

	/* grant access to all users who have acess to the orders table */

	$mi = getMemberInfo();
	$admin_config = config('adminConfig');

	$ajax_pass = makesafe($_REQUEST['ajax_pass']);
	$ajax_pass1 = makesafe($_REQUEST['ajax_pass1']);


	$resp = sql("SELECT * FROM '{$ajax_pass1}' WHERE ID = '{$ajax_pass}'");
  
	$query_done = db_fetch_assoc($resp);

	
    echo json_encode($query_done);


?>

Re: getting 500 on ajax call, any hints?

Posted: 2022-08-06 17:16
by D Oliveira
Hi there, thank you for replying =) I'm still getting status 500 with this code, it seems to only work when using sqlValue() all other functions keep returning 500

Code: Select all

jquery-3.5.1.min.js:2          
GET http://localhost/tigercontrol2/hooks/ajax/ajax-po.php?ajax_pass=6&ajax_pass1=workbook_gutter 500 (Internal Server Error)

Re: getting 500 on ajax call, any hints?

Posted: 2022-08-06 17:46
by pbottcher
ok,

sorry, did not pay attention to the query

use

Code: Select all

$resp = sql("SELECT * FROM {$ajax_pass1} WHERE ID = '{$ajax_pass}'");
instead

Code: Select all

$resp = sql("SELECT * FROM '{$ajax_pass1}' WHERE ID = '{$ajax_pass}'");

Re: getting 500 on ajax call, any hints?

Posted: 2022-08-06 17:59
by jsetzer
Don't know if it helps on that specific problem:

sql('SQL COMMAND', $eo); requires $error variable as 2nd parameter. Maybe 500 is being raised due to missing parameter in given PHP version (behaviour could be different depending on PHP version).

If I remember right, this is different from sqlValue()-function with optional $error variable. This could possibly explain why it is working with sqlValue()-, but not with sql()-function call.

Re: getting 500 on ajax call, any hints?

Posted: 2022-08-06 18:02
by jsetzer
Just checked code (of AG 22.14)

sql()-Function

Requires 2nd parameter $o

Code: Select all

function sql($statement, &$o) {
    // ...
}
sqlValue()-function

Optional 2nd parameter, defaults to NULL

Code: Select all

function sqlValue($statement, &$error = NULL) {
    // ...
}


Re: getting 500 on ajax call, any hints?

Posted: 2022-08-06 18:06
by D Oliveira
Thank you both for helping, that did it!!! removing the quote and adding second parameter =)))

Re: getting 500 on ajax call, any hints?

Posted: 2022-08-06 20:04
by pbottcher
perfect,

true, copy paste sometimes is just not enough :-)