简体   繁体   中英

GET ajax request sent to the same php file

Currently I have one php file createNewForm.php. In it, I have a list of checkboxes, when I check each checkbox, I want the sql query in my php file (createNewForm.php) to repopulate a list.

So lets say I have checkboxes 'Animals', 'Plants', 'Food' and I check 'Animals' then list will populate with Chicken, Cow, Dog. If I check 'Food', it will include 'Chicken','Cow','Dog','Pizza' where 'Chicken' is in both Animals and Food (sorry if you are vegetarian haha).

I am not so much worried about the SQL side of things as that part is working.

Below is my javascript for sending my checked values (Animals,Plants,Food) to itself:

createNewForm.php (javascript portion)

var $checkedSites = [];
var $checkedSitesString = '';

$(document).ready(function() {

    $('.checkedSites').change(function() {

        if($(this).is(':checked')) {

            $checkedSites.push($(this).val());
        }
        else
        {
            $checkedSites.splice($.inArray($(this).val(),$checkedSites),1);
        }

        $checkedSitesString = "'" + $checkedSites.join("','") +"'";


        var sitesChosen = 'sitesChosen='+$checkedSitesString;
        $.ajax({
                type: "GET",
                url: "createNewForm.php",
                data: sitesChosen,
                cache: false,
                success: function(result){
                alert(result);
                }
                });

    });
});

createNewForm.php (php portion)

<?php
    if (isset($_GET["sitesChosen"]) && !empty($_GET["sitesChosen"]) && $_GET["sitesChosen"] != '') { //Checks if action value exists
                $sitesChosen = $_GET["sitesChosen"];

                $productTypePopulationSQL = "SELECT distinct productname 
                                            FROM productType 
                                            WHERE active = 1
                                            and sitetypeid in (".$sitesChosen.");";
              }
            else
            {

                $productTypePopulationSQL = "SELECT distinct productname 
                                            FROM productType 
                                            WHERE active = 1;
                                            ";

            }
?>

Currently the output, as the code is written will give me the complete set regardless of which one is checked for sanity purposes. I have also echo'ed the result from my php when it is set through Javascript and my SQL statement is revised to what I want it to be but it also concatenates the entire HTML generated from my php without changing the content on the actual page. In addition this echo, for some reason is generated as an alert, instead of being written to the page.

Thank you!

Norman

You really did not ask a question. That would be nice.

as far as the alert that is what your code says to do.

alert(result);

If, instead, you need it "written to the page", create a div to put the message in.

HTML

<div id="msg"></div>

JS

var msg= document.getElementById('msg);

msg.innerHTML = result;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM