Beniamino_Baggins Beniamino_Baggins - 7 months ago 21
PHP Question

PHP echo to index.php from a different php file than index.php

I have a form in index.php that sends a JSON object to "getProducts.php" and I want to get the data back to index.php somehow either by an echo to the screen or logging to the console. But I have tried both techniques and nothing gets logged or goes onto the screen. I have the PHP debugger running and in both cases, it echoes out real populated values but nothing makes it to the screen or the console. Why does echoing from my getProducts.php not make it to the screen or the console?

index.php:

<form role="form"
id="search-filter-form"
action="scripts/back_end/getProducts.php"
method="post">
<div class="row">
<div class="col-sm-9">
<h1 id="search-filter-heading"
class="heading form-group">FILTER YOUR SEARCH.
</h1>
</div>
<div class="col-sm-6 col-md-3">
<div id="search-button" class="form-group">
<input id="search-input"
placeholder="SEARCH KEY WORD">
</input>
<div id="search-icon"></div>
</div>
</div>
</div>
<div class="row">
<?
echo RegionSelectTemplate::display();
echo TownSelectTemplate::display();
echo SuburbSelectTemplate::display();
echo CategorySelectTemplate::display();
?>
</div>
<div class="row">
<div class="col-sm-12">
<input type="submit"
value="FIND ME SOME VEGAN GOODIES"
id="go-button"
class="with-border clickable">
</input>
</div>
</div>
</form>


Handling the form submit event:

$("#search-filter-form").submit(function(event) {
/* stop form from submitting normally */
event.preventDefault();

/* get some values from elements on the page: */
var $form = $( this ),
url = $form.attr( 'action' );

/* Send the data using post */
var posting = $.post( url, { region: s.regionSelect._selectedAreaName, town: s.townSelect._selectedAreaName } );

/* Alerts the results */
posting.done(function( data ) {
console.log('post success');
});
});


Here is getProducts.php:

<?
function debug_to_console( $data ) {

if ( is_array( $data ) )
$output = "<script>console.log( 'Debug Objects: " . implode( ',', $data) . "' );</script>";
else
$output = "<script>console.log( 'Debug Objects: " . $data . "' );</script>";
echo $output;
}

if( $_POST["region"] || $_POST["town"] ) {
debug_to_console($_POST);
exit();
}
?>


Not sure if it's worth mentioning that the index.php code up top is a template. Index.php in code looks like this:

<!DOCTYPE html>
<?
header("X-Frame-Options: DENY");
header("Content-Security-Policy: frame-ancestors 'none'", false);
require 'scripts/back_end/views/template_engine.php';
require 'scripts/back_end/views/view.php';
?>
<html lang="en-US">
<head>
<?
echo HeadTemplate::display();
?>
</head>
<body>
<?
echo NavbarTemplate::display();
echo LandingPageTemplate::display();
echo SearchFilterPageTemplate::display();
echo SearchResultsPageTemplate::display();
echo TvrScriptsTemplate::display();
?>
</body>
</html>


But the end html output for the index.php form is as it is up the top of the question

Answer

You need to do what you want with the response in the posting.done() callback function. This will add it to the DOM, which will render the HTML and execute the script.

post.done(function(data) {
    $("#somediv").html(data);
});