【问题标题】:Sending Data From Down Down Form In PHP/HTML [duplicate]在 PHP/HTML 中从向下表单发送数据 [重复]
【发布时间】:2016-06-07 16:35:45
【问题描述】:

我一直在尝试制作一个标签,它将发送其结果并将其存储为一个 php 变量,我可以通过 id 订购我的 mysql 项目,任何帮助将不胜感激,非常感谢!

<!DOCTYPE html>
<html>
    <title>Test</title>
</head>
<body>
  <select>
    <option>Newest To Oldest</option>
    <option> Oldest To Newest></option>
    <?php
       $selection = 'ORDER BY id ASC';
       $sql = "SELECT id, threadName, message FROM threads".$selection."";
    ?>
  </select>
</body>
</html>

请记住,这只是我总共拥有的代码的一个片段^

【问题讨论】:

  • 下拉菜单是否有提交按钮,是否在form 标签内?

标签: php


【解决方案1】:

您的代码没有form 标签,也没有submit 按钮。您可以使用@Mark M 参考的问题中的链接。

现在,让我们来看看如何使用组合框来做到这一点。

您的 HTML 表单如下所示:

<form method="GET">
  <select name="sort" id="sort">
      <option value="1" <?php if($selection=="ORDER BY id DESC"){echo "selected";} // if orderd by newest, selecte this ?> >Newest To Oldest</option> 
      <option value="2" <?php if($selection=="ORDER BY id ASC"){echo "selected";} // if orderd by newest, selecte this ?> >Oldest To Newest</option>
  </select>
  <input type="submit" name="submit"/>
</form>

那么您的 PHP 脚本将如下所示:

<?php 

$selection=isset($_GET['sort']) ? $_GET['sort'] : ""; 

if($selection == "1"){$selection="ORDER BY id DESC";}       // if selection equals to 1 then sort by newest
else if($selection == "2"){$selection="ORDER BY id ASC";}   // else if selection equals to 2 then sort by oldest
else{$selection="ORDER BY id DESC";}                        // else just sort by newest

$sql="SELECT id, threadName, message FROM threads ";        // don't forget space the end of sql
$sql.=$selection;

//echo $sql; //$sql will look like "SELECT id, threadName, message FROM threads ORDER BY id ASC "

//mysqli_query then fetch_assoc then do your loop
?>

如果您打算查询combo-box 的更改,您可以借助Javascript(jQuery) 和Ajax 来完成。看看这个和平的代码:

<script>
$(document).on('change', '#sort', function(){ // if combo-box changes do a Ajax call
    $.ajax({
        url:"url/to/script.php",              // url to your php script
        type:"GET",                           // method to use
        data:{sort: $("#sort").val()},        // parameters to send
        success: function(data){              // if this ajax call(not script) is successful then 
            $('#selector').html(data);        // add the returned data to the DOM
            }
        });
    });
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-10
    • 2022-08-19
    • 2020-03-12
    • 2013-10-17
    相关资源
    最近更新 更多