简体   繁体   English

jquery没有向php发送按钮选择

[英]jquery not sending button choice to php

I have a search function that will accept a search string and send it to a php file for parsing a database column. 我有一个搜索函数,它将接受搜索字符串并将其发送到php文件以解析数据库列。 I'd also like users to choose which aspect of the website they'd like to search (comics, artwork, or both). 我也希望用户选择他们想要搜索的网站的哪个方面(漫画,艺术品或两者)。 Comic and Artwork or stored in two separate tables. 漫画和艺术品或存储在两个单独的表中。

This is a function that will accept an input search string from the html below and send it to a php file. 这是一个接受来自下面的html的输入搜索字符串并将其发送到php文件的函数。

<script type="text/javascript">

        function search(searchString) {     
            //var site = $("#site").val();
            $.get("./scripts/search.php", {_input : searchString},
                function(returned_data) {
                    $("#output").html(returned_data);
                }
            );
        }

And this is javascript to accept a choice to search "comics", "artwork" or "all". 这是javascript接受搜索“漫画”,“艺术品”或“所有”的选择。

        function searchChoice(choice) {     
            alert("Choice: " + choice);
            $.get("./scripts/search.php", {_choice : choice}
           );
        }

</script>

HTML: HTML:

    <!--Search filtering for comics, artwork, or both-->
<span class="search"><b>Search for: </b> </span>

<div class="btn-group" data-toggle="buttons-radio">
<span class="search">
    <button type="button" class="btn" id="comics" onclick="searchChoice(this.id)">Comics</button> 
    <button type="button" class="btn" id="artwork" onclick="searchChoice(this.id)">Artwork</button> 
    <button type="button" class="btn" id="all" onclick="searchChoice(this.id)">All</button> 
</span>
</div>

<br/>
<br/>

<!--Search functionality-->
<span class="search">
    <input type="text" onkeyup="search(this.value)" name="input" value="" />
</span>

<br />
<span id="output"><span class="sidebarimages">  </span></span>

PHP excerpt: PHP摘录:

$input = (isset($_GET['_input']) ? ($_GET['_input']) : 0); 
$siteChoice = (isset($_GET['_choice']) ? ($_GET['_choice']) : "all");

You can see the javascript correctly alerting out "Choice: comics" when comics button is selected, but the php side, echo "</br>Choice: " . $siteChoice; 你可以看到javascript在选择漫画按钮时正确警告“选择:漫画”,但是php端, echo "</br>Choice: " . $siteChoice; echo "</br>Choice: " . $siteChoice; , is echo'ing out "all", which is incorrect. ,是回声“全部”,这是不正确的。

在此输入图像描述

Any ideas would be greatly appreciated! 任何想法将不胜感激!

As mentioned @E_p, that is the problem ... another option is to create a variable and store the data there ... try this: you don't need change the html 正如提到的@E_p,这就是问题......另一种选择是创建变量并将数据存储在那里...试试这个:你不需要改变html

   var mySearchString = 0;
   var myChoice = 'all';


function search(searchString) {     
        mySearchString = searchString;
        GetSearch();
        }

function searchChoice(choice) {     
            myChoice = choice;
            GetSearch();
            }

function GetSearch(){

 $.get("./scripts/search.php", {_input : mySearchString, _choice : myChoice},
        function(returned_data) {
            $("#output").html(returned_data);
        }
    );

}

You do not keep state for _choice. 你没有为_choice保持状态。

When search is called it does not pass it to a server. 调用搜索时,它不会将其传递给服务器。

You need to change buttons to option and in search function pass both. 您需要将按钮更改为选项,并在搜索功能中通过两者。 to a server at the same time 同时到服务器

Replace the buttons with radio buttons and use form.Serialize() 用单选按钮替换按钮并使用form.Serialize()

<form id="searchform">
    <input type="radio" name="_choice" value="comics" />Comics<br/>
    <input type="radio" name="_choice" value="artwork" />Artwork<br/>
    <input type="radio" name="_choice" value="all" />All<br/>


    <input type="text" onkeyup="search()" name="_input" value="" />
</form>

Javascript 使用Javascript

  function search() {     
        //var site = $("#site").val();
        $.get("./scripts/search.php", $('#searchform').serialize(),
            function(returned_data) {
                $("#output").html(returned_data);
            }
        );
    }

The .serialize() function converts form input to JSON so you don't have to type manually, No more parameter, and no two functions, just one to do them all .serialize()函数将表单输入转换为JSON,因此您不必手动输入,不再需要参数,也不需要两个函数,只需一个来完成所有操作

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

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