Jump to content

[SOLVED] PHP & checkboxes


Miko

Recommended Posts

Hello,

 

I'm making a form with a search input + 3 different checkboxes.

So making the form is absolutely no problem, the search input either.

 

The thing that I want is that a user can select one of the 3 checkboxes and in function of that (+ search input) they will search in the database.

 

Anyone an idea how to pull this together?

 

Thanks,!

Link to comment
https://forums.phpfreaks.com/topic/154837-solved-php-checkboxes/
Share on other sites

You're probably giving them all different names then - very common mistake and easy to make.

 

<input type="radio" name="myradio" value="1"> Cheese
<input type="radio" name="myradio" value="2"> Onion
<input type="radio" name="myradio" value="3"> Cheese & Onion

 

When you get the value of the radio button depending on which one is selected will return the correct value. Grouped radio buttons must all be named the same.

Let's make a form:

<form action="<?php echo $_SERVER['PHP_SELF']; method="post">
Name: <input type="text" name="username"><br>
Favourite Food:
<input type="radio" name="myradio" value="1"> Cheese<br>
<input type="radio" name="myradio" value="2"> Onion<br>
<input type="radio" name="myradio" value="3"> Cheese & Onion<br><br>
<input type="submit" name="subsend" value="Answer Questions">

 

Depending on the ACTION part of the FORM declaration (POST or GET) depends on how you pick up the data.

 

POST sends data in packets where as GET appends the data onto the URL. We've used POST so we'll read it using this:

$uname=$_POST['username'];
$food=$_POST['myradio'];
if (isset($_POST['subsend'])) {
  echo 'User '.$uname.' selected option '.$food;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.