Beniamino_Baggins Beniamino_Baggins - 2 years ago 111
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?


<form role="form"
<div class="row">
<div class="col-sm-9">
<h1 id="search-filter-heading"
class="heading form-group">FILTER YOUR SEARCH.
<div class="col-sm-6 col-md-3">
<div id="search-button" class="form-group">
<input id="search-input"
placeholder="SEARCH KEY WORD">
<div id="search-icon"></div>
<div class="row">
echo RegionSelectTemplate::display();
echo TownSelectTemplate::display();
echo SuburbSelectTemplate::display();
echo CategorySelectTemplate::display();
<div class="row">
<div class="col-sm-12">
<input type="submit"
class="with-border clickable">

Handling the form submit event:

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

/* 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>";
$output = "<script>console.log( 'Debug Objects: " . $data . "' );</script>";
echo $output;

if( $_POST["region"] || $_POST["town"] ) {

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">
echo HeadTemplate::display();
echo NavbarTemplate::display();
echo LandingPageTemplate::display();
echo SearchFilterPageTemplate::display();
echo SearchResultsPageTemplate::display();
echo TvrScriptsTemplate::display();

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

Answer Source

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) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download