从复选框形式制作数组 [英] Make array from checkbox form

查看:24
本文介绍了从复选框形式制作数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和我的朋友正在制作一个网站,根据您的兴趣编辑新闻故事.有没有简单的方法来获取复选框数据并从选定的复选框中创建一个数组?这是我们的表格

My friend and I are making a website that compiles news stories based on your interests. Is there and easy way to take the checkbox data and make an array out of the selected checkboxes? Here is our form

<form action="signup.php" method="POST">
    Name: <input type="text" name="name" /> <br />
    Username: <input type="text" name="username"> <br />
    Password: <input type="password" name="pwd" /> <br />
    Email: <input type="text" name="email" /> <br />

    <p>By filling out this we will be able to find news articles that will interest you</p>        <br />
    Politics<input type="checkbox" name="interest[]" value="Politics" /> <br />
    Entertainment<input type="checkbox" name="interest[]" value="Entertainment" /> <br />
    Tech <input type="checkbox" name="interest[]" value="Tech" /> <br />
    Health<input type="checkbox" name="interest[]" value="Health" /> <br />
    Living<input type="checkbox" name="interest[]" value="Living" /> <br />
    Travel <input type="checkbox" name="interest[]" value="Travel" /> <br />
    World<input type="checkbox" name="interest[]" value="World" /> <br />
    Leisure<input type="checkbox" name="interest[]" value="Leisure" /> <br />
    Finance<input type="checkbox" name="interest[]" value="Finance" /> <br />
    Celebrity Gossip<input type="checkbox" name="interest[]" value="Gossip" /> <br />
    Movies<input type="checkbox" name="interest[]" value="Movies" /> <br />
    Sports<input type="checkbox" name="interest[]" value="Sports" /> <br />

    <input type="submit" value="Submit">
</form>

我们将如何使用这些数据创建一个 php 数组?

how would we make a php array using this data?

推荐答案

HTML 标记:

<form method="get">
    <input type="checkbox" name="options[]" value="Politics"/> Politics<br/>
    <input type="checkbox" name="options[]" value="Movies"/> Movies<br/>
    <input type="checkbox" name="options[]" value="World "/> World<br/>
    <input type="submit" value="Go!" />
</form>

在php代码中:

$checked = $_GET['options'];
for($i=0; $i < count($checked); $i++){
    echo "Selected " . $checked[$i] . "<br/>";
}

这篇关于从复选框形式制作数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆