简体   繁体   English

通过单击或下拉按钮更改MySQL查询

[英]Change MySQL query with button click or drop-down

I have a dropdown list being populated by the code below. 我有一个下拉列表,其中包含以下代码。 I want to be able to change the type = x from a user input button(s) to predetermined options. 我希望能够将类型= x从用户输入按钮更改为预定选项。 I don't know if this is possible, especially without a page refresh as the drop-down is already populated from the initial query. 我不知道是否可行,尤其是在没有页面刷新的情况下,因为从初始查询中已经填充了下拉菜单。

I need a dynamic query based on user selected without page refresh. 我需要一个基于所选用户的动态查询,而无需刷新页面。 Maybe I need to look at another language, such as Angular. 也许我需要看看另一种语言,例如Angular。

<?php
echo "<b>Start:<b/>";
echo "<select id='start' class=''>";

$list_query = mysqli_query($server, "SELECT `id`, `street`, `description`, `lat`, `lng`, `note`, `type` FROM `markers` WHERE `type` = 'red' ORDER BY `street`");
while($run_list = mysqli_fetch_array($list_query)){
    $u_id = $run_list['id'];
    $u_street = $run_list['street']; 
    $u_desc = $run_list['description'];  
    $u_note = $run_list['note'];  
    $u_type = $run_list['type'];  
    $u_lat = $run_list['lat'];   
    $u_lng = $run_list['lng'];  

    echo "<option value='$u_lat,$u_lng'>$u_street</option>";
}

echo "</select>";
?>

Update 更新资料

The following returns "red" in the Div with ID of "callback". 以下在ID为“ callback”的Div中返回“ red”。 Instead of this how do I get this to update the query string? 取而代之的是如何获取更新查询字符串的方法呢?

Example Button: 示例按钮:

<button id="b1" value="red" onclick="submit_1()">button 1</button>

Ajax: 阿贾克斯:

function submit_1() {
var b1 = document.getElementById("b1").value;
console.log(b1);

var dataString = 'b1=' + b1;

$.ajax({
    type: "POST",
    url: "php/getdata.php",
    data: dataString,
    cache: false,
    success: function(data) {
        $("#callback").html(data);
    }
});
}

Simple getdat.php: 简单的getdat.php:

<?php
$b1 = $_POST['b1'];

if ($b1 == "red") {
    echo $b1;
} else {
    echo 'Not '. $b1;
}
?>

Use AJAX. 使用AJAX。 It's entirely possible with PHP and javascript. PHP和javascript完全有可能。 Basically, you can send off the choice with AJAX (onChange), use the "GET" method, and on success you return the HTML you want to replace (or the corresponding data, then change your HTML according to that). 基本上,您可以使用AJAX(onChange)发送选择,使用“ GET”方法,并在成功后返回要替换的HTML(或相应的数据,然后根据该HTML进行更改)。

EDIT Here is an example: 编辑这是一个例子:

Basically you do some action and then call code like this: 基本上,您需要执行一些操作,然后调用如下代码:

$.ajax({
        url: <?php echo $your_action_url/user_choice;?>,
        type: 'GET',
        success: function(data) {
          $("#dropdown").replaceWith(data);
        }
      });

where the "data" is the dropdown information stored in a view page (I am using MVC in this particular example). 其中“数据”是存储在视图页面中的下拉信息(在此特定示例中,我正在使用MVC)。 Basically I am replacing the existing dropdown with a dropdown containing the right information. 基本上,我将使用包含正确信息的下拉列表替换现有的下拉列表。 You would have the php function return the html that you want to replace the dropdown with. 您将需要php函数返回要替换下拉列表的html。

使用Ajax在服务器的请求查询字符串中添加类型。

$list_query = mysqli_query($server, "SELECT `id`, `street`,`description`, `lat`, `lng`, `note`, `type` FROM `markers` WHERE `type` =  $_GET['type'] ORDER BY `street`");

try to learn ajax angular.js this library very simple use 尝试学习ajax angular.js这个库非常简单的使用

http://phpenthusiast.com/blog/ajax-with-angular-and-php http://phpenthusiast.com/blog/ajax-with-angular-and-php

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

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