Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have a HTML SELECT/OPTION value as NULL using PHP?

The following is a snippet from my HTML form which pulls the options from the table rows accordingly.

What I want to do is have the first option value to be NULL so when no choice is made, NULL is entered when the form is submitted to the database.

    <td>Type</td>     <td>:</td>     <td><select name="type_id" id="type_id" class="form">     <option value=""></option>         <?php              $sql = mysql_query("SELECT type_id, type FROM $tbl_add_type") or die(mysql_error());             while ($row = mysql_fetch_array($sql))                 {                     echo "<option value=".$row['type_id'].">" . $row['type'] . "</option>";                 }         ?>     </select>*</td> 

Is this possible? Or could someone suggest an easier/better way of doing this?

Thanks

Update: Thanks for the answer, I'm using the method detailed below to convert it to NULL.

if ($_POST['location_id'] === '')                     {                         $_POST['location_id'] = 'NULL';                     } 

However when trying to use this NULL value with the following query it does not work.

UPDATE addresses SET location_id='NULL' WHERE ipid = '4791' 

I know it needs to be location_id=NULL instead but don't know how to do this...

Update 2: this is how my queries work:

if ($_POST['location_id'] === '') { $_POST['location_id'] = 'NULL'; // or 'NULL' for SQL }  $notes=isset($_POST['notes']) ? mysql_real_escape_string($_POST['notes']) : ''; //$location_id=isset($_POST['location_id']) ? mysql_real_escape_string($_POST['location_id']) : ''; $ipid=isset($_POST['ipid']) ? mysql_real_escape_string($_POST['ipid']) : '';   $sql="UPDATE addresses      SET notes='$notes', location_id='$location_id'     WHERE ipid = '$ipid'";   mysql_query($sql) or die(mysql_error()); 
like image 828
Bernard Avatar asked Jan 09 '12 23:01

Bernard


People also ask

How do you set a select null value?

To set selected value of a select drop down to null with React, we can set the state we use as the value of the value prop of the select drop down to null . to set onChange to a function that calls setSelected to e. target. value which is the selected value if it's truthy.

WHAT IS NULL value in HTML?

The value null represents the intentional absence of any object value. It is one of JavaScript's primitive values and is treated as falsy for boolean operations.

How do you get the selected value of dropdown in PHP without submit?

php $selected_option=$_POST['option_value']; //Do what you need with this value, then echo what you want to be returned. echo "you have selected the option with value=$selected_option"; ?>

How do you hide an option in HTML?

You can specify either 'hidden' (without value) or 'hidden="hidden"'. Both are valid. A hidden <option> element is not visible, but it maintains its position on the page. Removing the hidden attribute makes it re-appear.


2 Answers

No, POST/GET values are never null. The best they can be is an empty string, which you can convert to null/'NULL'.

if ($_POST['value'] === '') {     $_POST['value'] = null; // or 'NULL' for SQL } 
like image 195
deceze Avatar answered Sep 21 '22 06:09

deceze


All you need is a check on the post side of things.

if(empty($_REQUEST['type_id']) && $_REQUEST['type_id'] != 0)     $_REQUEST['type_id'] = null; 
like image 22
Jason Avatar answered Sep 21 '22 06:09

Jason