简体   繁体   English

GET ajax请求发送到相同的php文件

[英]GET ajax request sent to the same php file

Currently I have one php file createNewForm.php. 目前,我有一个php文件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. 在其中,我有一个复选框列表,当我选中每个复选框时,我希望我的php文件(createNewForm.php)中的sql查询重新填充一个列表。

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. 我不太担心SQL方面的问题,因为该部分正在工作。

Below is my javascript for sending my checked values (Animals,Plants,Food) to itself: 以下是我的JavaScript,用于将检查值(动物,植物,食物)发送给自己:

createNewForm.php (javascript portion) createNewForm.php(JavaScript部分)

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) createNewForm.php(php部分)

<?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. 当通过Javascript设置php的结果并将SQL语句修改为我想要的值时,我也从php中回显了结果,但是它也连接了从php生成的整个HTML,而没有更改实际页面上的内容。 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. 相反,如果您需要将其“写入页面”,则创建一个div来放置消息。

HTML 的HTML

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

JS JS

var msg= document.getElementById('msg);

msg.innerHTML = result;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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