【问题标题】:Getting the value of a button from a php script and posting the value to MySQL database从 php 脚本中获取按钮的值并将值发布到 MySQL 数据库
【发布时间】:2014-10-28 21:15:44
【问题描述】:

我已经为此工作了好几天,我已经用 Google 搜索并重新搜索过,但我似乎无法准确找到要查找的内容。我有一个由 php 脚本填充的菜单选项卡,如下所示:

<?php include 'db_connector.php';

  $result = mysqli_query($con,"SELECT * FROM os_scope");

   while($row = mysqli_fetch_array($result)) {

    echo "<button type='radio'>"."<li>"."<div class='title'>"."<a href='#myPanel' >".$row['name']."</a>"."</div>"."</li>"."</button>";

            }
  ?>

结果的输出是这样的:

Billing    
Custom
Customer
Product

当我选择其中任何一个,即计费时,它会打开#mypanel

我希望每次选择一个项目(即账单)时,应该将值“账单”发布到面板,然后我可以使用该值来查询 MySQL 数据库。其余物品也一样。

任何建议将不胜感激。提前谢谢你。

【问题讨论】:

  • &lt;button type='text'&gt;?
  • 抱歉应该是
  • 按钮没有像收音机这样的有效类型。看到这个:w3schools.com/tags/att_button_type.asp 你是说&lt;input type="radio" name="something" value="somevalue" /&gt; 吗?无论如何,您的 HTML 语法也很糟糕。
  • 这个问题太模糊了。请澄清您要做什么。我想这个解决方案是基于 ajax 的,但你还没有标记 javascript
  • &lt;button type='submit'&gt; 当然,但不是text 也不是radio - 输入 类型,是的。

标签: php mysql radiobuttonlist


【解决方案1】:

没有&lt;button type='radio'&gt;这样的东西

使用&lt;input type='radio' value='value'&gt;

试试这个:

echo "<li><input type='radio' value='".$row['name']."' /><div class='title'><a href='#myPanel' >".$row['name']."</a></div></li>";

此外,没有理由到处这样做:"." 让字符串成为字符串。这就是字符串最擅长的。

【讨论】:

  • 虽然这一切都是正确的,但它并没有回答这个问题(这个问题太模糊了,无法回答)。我没有投反对票,但这确实应该是评论
  • @Adelphia,非常感谢您的建议,我尝试使用您给我的代码,但我的打开没有打开,我也不想将它们作为输入按钮,有没有只是将它们作为按钮但仍然获得我想要的功能的方式???同样,任何建议都非常有价值,感谢您的帮助。
  • @Annie 您可以使用切换按钮,但没有原生 HTML 元素。你必须使用一些JS。 jsfiddle.net/davidelrizzo/DYJkG
【解决方案2】:

这正是我想要的,

<!DOCTYPE html>
<html>
<body>

<?php include 'db_connector.php';

 $result = mysqli_query($con,"SELECT * FROM os_scope");

 while($row = mysqli_fetch_array($result)) {

 $row['name'];

echo "<input type='radio' name='os' value=".$row['name']." id='myRadio'>";


                }
?>

<p>Click the "Try it" button to display the value of the radio button.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
    function myFunction() {
        var x = document.getElementsByName('os');
        var rate_value;
        for(var i = 0; i < x.length; i++){
            if(x[i].checked){
                rate_value = x[i].value;
                break;
            }
        }
        document.getElementById("demo").innerHTML = rate_value;

    }
</script>

</body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多