0

what function should I use to obtain a "Y" if the checkbox has been selected or "N" if it has not. This is to add into a SQL statement? I'm guessing it is an if/else but I can't figure it out.

Here is my checkboxes which some may be selected individually:

                        <label class="timetable-q">Data Projector</label>
                        <input type="hidden" id="proj_check" name="proj_check" value="Y"/>
                        <input type="checkbox" id="proj_check" name="proj_check" value="N"/>
                        <label class="timetable-q">Whiteboard</label>
                        <input type="hidden" id="white_check" name="white_check" value="Y"/>
                        <input type="checkbox" id="white_check" name="white_check" value="N"/>
                        <label class="timetable-q">OHP</label>
                        <input type="hidden" id="ohp_check" name="ohp_check" value="Y"/>
                        <input type="checkbox" id="ohp_check" name="ohp_check" value="N"/>
                        <label class="timetable-q">Wheelchair Access</label>
                        <input type="hidden" id="wheel_check" name="wheel_check" value="Y"/>    
                        <input type="checkbox" id="wheel_check" name="wheel_check" value="N"/>                                  
                        <label class="timetable-q">Lecture Capture</label>
                        <input type="hidden" id="cap_check" name="cap_check" value="Y"/>
                        <input type="checkbox" id="cap_check" name="cap_check" value="N"/>
                        <input type="submit" value="submit">
                        </p>
                        </form>

Here is my PHP:

<?php 
                        $numeroOption= $_POST['numero'];
                        $selectOption = $_POST['parkname'];
                        $query = "SELECT * FROM `ROOMS` WHERE `Capacity` < '$numeroOption' AND `Park` LIKE '$selectOption%'"; 
                        $result = mysql_query($query);
                        echo $result;
                        if ($result == FALSE) die ("could not execute statement $query<br />");
</php>

Thanks for your help!

James
  • 63
  • 6
  • 2
    Checkboxes will appear in your `$_POST` array. – Jay Blanchard Dec 01 '15 at 17:37
  • 3
    [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Dec 01 '15 at 17:37
  • 2
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Dec 01 '15 at 17:37
  • 1
    Your inputs all need `name` attributes. – Jay Blanchard Dec 01 '15 at 17:39
  • To reiterate the sentiment by @JayBlanchard - Do not use this code in any website that you care about. It is vulnerable to SQL injection. Use stored procedures in your DBMS whenever possible. – Stefan H Dec 01 '15 at 17:39
  • @JayBlanchard How can I $_POST with a 'Y' or 'N' result based on if it has been selected? – James Dec 01 '15 at 17:56
  • Add a value to each of your checkboxes. Unchecked checkboxes *do not appear* in the POST array. – Jay Blanchard Dec 01 '15 at 18:03
  • @JayBlanchard Thanks for your help! How do i then add this into my SQL statement? – James Dec 01 '15 at 18:13
  • 1
    You really do need to go take some basic PHP tutorials and work through them. The values of the checkboxes, being in the POST array, can then be used as variables in your query. – Jay Blanchard Dec 01 '15 at 18:37

1 Answers1

0

You do not need to create a hidden lable. Check box does not post value if it is not selected. So you just need to check in php weather $_POST is set or not for example

 <input type="checkbox" id="proj_check" name="proj_check" value="Y"/>
 <input type="checkbox" id="white_check" name="white_check" value="Y"/>

and On php page you have to check like

if(isset($_POST['proj_check']))
   $proj_check = $_POST['proj_check'];
else
   $proj_check = "N";

then you can use $proj_check as per your need.

Manish Shukla
  • 1,355
  • 2
  • 8
  • 21