dongyata3336 2015-05-12 23:17
浏览 108

GET ajax请求发送到同一个php文件

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

  • 写回答

1条回答 默认 最新

  • douciwang6819 2015-05-13 01:50
    关注

    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;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题