Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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>

how would we make a php array using this data?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
230 views
Welcome To Ask or Share your Answers For Others

1 Answer

the HTML markup:

<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>

and in the php code:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...