The following warnings occurred:
Warning [2] Undefined variable $unreadreports - Line: 26 - File: global.php(961) : eval()'d code PHP 8.2.25 (Linux)
File Line Function
/global.php(961) : eval()'d code 26 errorHandler->error
/global.php 961 eval
/showthread.php 28 require_once





× This forum is read only. As of July 23, 2019, the UserSpice forums have been closed. To receive support, please join our Discord by clicking here. Thank you!

  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AJAX post bug
#1
I have a situation where I post form to php parser via ajax call:

Example
$.post('parser.php', {
post_var1:var1,
post_var2:var
},

function(data)
{
console.log("Response: "+data);
});


then in the parser.php I submit this data to db. So I need to include init.php, header.php and securePage in the parser.

Example
<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/users/init.php';
require_once $abs_us_root.$us_url_root.'users/includes/header.php';
if (!securePage($_SERVER['PHP_SELF'])){die();}

if($f_var1 = $_POST['post_var1'] ) $fields[var1] = $f_var1;
if($f_var2 = $_POST['post_var2'] ) $fields[var2] = $f_var2;

$success = $db->insert('devices',$fields);

if(success) echo "1";
else echo "0";
?>


This should be pretty straight forward right?

Well the problem is that when I get echo from parser.php, the console.log prints the whole header (users/includes/header.php) + my echo 0 or 1.

I tryed to flush() and ob_flush() and then echo with no success. Problem occurs when I include header.php. If header.php is not included, the echo works OK. But then I'm stuck without DB class...

I also searched header.php and not found any of the echo routines. Also header.php is still original userSpice.

Also may I say that the header is echoed back regardless of what the parser.php is doing. Even if I comment out db->insert and $fields I'm stuck with that.

But there is one more interesting thing. If I access parser.php directly, the echo does show correct. Only 0 is echoed back (or 1 if the db->insert is successful).

Regards,
Jakob
  Reply
#2
If all you need is to open the connection from the database, instead of including header.php you can simply copy-paste the db code (and anything else you'd like) and it won't include the html. If you need any of the helper functions, you can still include_once those files (helpers.php and us_helpers.php) and you'll still be able to use the functions.

As for your mystery, when you view parser.php directly it sounds like the html is still there but since there are no tags in your body it looks like only your 0 or 1 is shown. If you view the source code you'll probably still find the
Code:
<html><head></head><body>
tags from the header. If this isn't the case I'd love to help you figure out why.
  Reply
#3
Thanks @karsen. DB class wasn't working if I only included helpers.php and us_helpers.php I get: PHP Fatal error: Call to a member function insert() on null in parser.php on line 10.

However, after investigating header.php, I found that header.php does call ob_start() function at the begining, so I googled it and found that I can clean ob (output buffer) with ob_end_clean(). So I added last function after including header.php and echo now works perfectly!

Thanks again!
  Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)