0

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

2 Answers 2

2

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);
});
Sign up to request clarification or add additional context in comments.

2 Comments

In the context of getProducts.php, what is data?
It's everything the script echoed.
0

Sounds like you need to throw in some ajax in with the jquery you are already using

$("#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' );

    $.ajax({
        type: "POST",
        url: "getProducts.php?",
        data: "region="+ s.regionSelect._selectedAreaName+
        "&town="+ s.townSelect._selectedAreaName,       
        success: function(data) {
            alert(data);
            //or
            window.console&&console.log(data);
        }
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.