I have a table with a varchar column. I use a select form with values 01, 02, 03, 04 .... 10, 11, 12 etc to store data in this column. My problem is that, when I submit the form, the leading 'Zero' is removed. How can I maintain the leading zero?
**
NB. I have checked other similar posts but they don't have the
solution for my issue. I have tried var/char/text and still it's not
working hence why I posted this question if there's another way to get this done.
**
<form method=post enctype="multipart/form-data">
<select name="dd">
<option selected="selected">Day</option>
<option value='01'>01</option>
<option value='02'>02</option>
<option value='03'>03</option>
<option value='04'>04</option>
<option value='05'>05</option>
<option value='06'>06</option>
<option value='07'>07</option>
<option value='08'>08</option>
<option value='09'>09</option>
<option value='10'>10</option>
<option value='11'>11</option>
</select>
<select name="mm">
<option selected="selected">Month</option>
<option value='01'>01</option>
<option value='02'>02</option>
<option value='03'>03</option>
<option value='04'>04</option>
<option value='05'>05</option>
<option value='06'>06</option>
<option value='07'>07</option>
<option value='08'>08</option>
<option value='09'>09</option>
<option value='10'>10</option>
<option value='11'>11</option>
</select>
</form>
MySQL is as follows;
$caption = $_POST['dd'].-$_POST['mm'];
$sql = "INSERT INTO pages SET caption = '$caption'";
Use sprintf to format your value:
$caption = sprintf("%02d-%02d", $_POST['dd'], $_POST['mm']);
The key here is to work with the data as strings and don't do math on it like -$_POST['mm'] which will treat it as a number. In numbers leading zeros are always omitted because they always exist. 10 is actually something like 0x0000000a as far as the CPU is concerned.
Related
I have a small Project website that I am learning to code with and in this website there is a Select Dropdown Menu of Music Genres
Here is a Video to help you visualise whats happening.
youtu.be/t4cJ7TCHiN8 (of the Problem)
And another video of how I can Paste "House" into the URL and get different MYSQL results
youtu.be/zf0S9WadQXo
I select a genre post the form and the reload shows me the results.
The value of the selected Option is then automatically selected by Jscript
So the Search area and its Values remain the same as before.
I have Mainly used DocumentGetElementById or jQuery to Select the value, and I am echoing PHP variables into the DocumentGetElementById function.
There is no problems with 99.9% of the values and selectable genres, they all are automatically selected without any problems.
Just 1 Problem, The value "House" Will not select.
No matter how I try it just will not select. I have of course tried other methods, including jQuery and no matter how I alter my code or method of execution.
Nothing seems to make the value "House" populate.
Here is the code. And images to help visualise the problem.
Thank you for any help you may provide.
CODE.
<select id='genregrab' name='genre' class='menusmall'>
<optgroup label='Genre'>
<option value="">All Genres</option>
<option value="EDM">EDM</option>
<option value="DnB">DnB</option>
<option value="Drum">Drum And Bass</option>
<option value="Breakbeat">Breakbeat</option>
<option value="Downtempo">Downtempo</option>
<option value="Dance">Dance</option>
<option value="Pop">Pop</option>
<option value="Disco">Disco</option>
<option value="Nu-Disco">Nu-Disco</option>
<option value="House">House</option>
<option value="Hard">Hard House</option>
<option value="Tech">Tech House</option>
<option value="Techno">Techno</option>
<option value="Deep">Deep House</option>
<option value="Future">Future House</option>
<option value="Tribal">Tribal House</option>
<option value="Tropical">Tropical House</option>
<option value="Progressive">Progressive</option>
<option value="Bass">Future Bass</option>
<option value="Bounce">Future Bounce</option>
<option value="Industrial">Industrial</option>
<option value="Electronic">Electronic</option>
<option value="Psychedelic">Psychedelic</option>
<option value="Trance">Trance</option>
<option value="Psy">Psy Trance</option>
<option value="Minimal">Minimal</option>
<option value="Ambient">Ambient</option>
<option value="Chillout">Chillout</option>
<option value="Synthwave">SynthWave</option>
<option value="Retro">Retro</option>
<option value="Hip">Hip-Hop</option>
<option value="Trip">Trip-Hop</option>
<option value="Glitch">Glitch-Hop</option>
<option value="Rap">Rap</option>
<option value="Afrobeat">AfroBeat</option>
<option value="Grime">Grime</option>
<option value="Trap">Trap</option>
<option value="Trapstep">TrapStep</option>
<option value="Dubstep">DubStep</option>
<option value="Drumstep">DrumStep</option>
<option value="Reggea">Reggea</option>
<option value="RnB">RnB</option>
<option value="Rock">Rock</option>
<option value="Metal">Metal</option>
<option value="Soul">Soul</option>
<option value="Country">Country</option>
<option value="Folk">Folk</option>
<option value="Jazz">Jazz</option>
<option value="Blues">Blues</option>
<option value="Funk">Funk</option>
<option value="World">World</option>
<option value="Gospel">Gospel</option>
<option value="Latin">Latin</option>
<option value="Ethnic">Ethnic</option>
<option value="Bollywood">BollyWood</option>
<option value="Moombahton">Moombahton</option>
<option value="Orchestral">Orchestral</option>
<option value="Classic">Classical</option>
<option value="Cinematic">Cinematic</option>
</optgroup>
</select>
Item Selected & POSTs Picked up By JScript to re-select the option
$(function(){
var genre = document.getElementById('genregrab');
var genredata = "<?php echo $genre ?>";
genre.value=genredata;
});
(Of course i initially tried the following)
document.getElementById("genregrab").value="<?php echo $genre ?>";
However unlike the others , the word "House" will not be selected.
I think its to do with the word or something, as every other word will work fine.
Same process just selecting another genre, and it works fine
As you can see , the Trance genre was selected
Just the word "House" has the problem.
And the Result after the Jscript
However , Select any other word and it works
As you can see in the image below
Any Ideas , Anybody?
I forgot to add the below code , from the GET and the MYSQL Results.
Its possible the sanitizing is effecting the variables.
The GET
if (isset($_GET['genre'])) {
$genre=$_GET['genre'];
$genre = filter_var($genre, FILTER_SANITIZE_STRING);
$genre = strip_tags($genre);
$genre = str_replace(['"',"'"], "", $genre);
} else {$genre="";}
And the MYSQL variable - Which also gets posted to the URL for GET
$genre=$row['genre'];
$genre = str_replace(['"',"'"], "", $genre);
if ($genre == "null" | $genre==" "){$genre="";}
$genresend = explode(',',trim($genre))[0];
I have requirement to get date between particular years so I have put two dropdown “from year” and “to Year” but I want to make validation for that “to dropdown” value must be great than “form dropdown”
Below is my html code
<div class="graph-filter">
<label>From</label>
<select onchange="change_data_year();" id="from_data_year" name="from_data_year" class="graph-select">
<option selected="" value="2016">2016</option>
<option value="2015">2015</option>
<option value="2014">2014</option>
<option value="2013">2013</option>
</select>
<label>To</label>
<select id="to_data_year" name="to_data_year" class="graph-select">
<option selected="" value="2016">2016</option>
<option value="2015">2015</option>
<option value="2014">2014</option>
<option value="2013">2013</option>
</select>
<input type="submit" value="Submit">
Try like this
function change_data_year(){
$('#to_data_year option').prop('disabled',false);
var from_data_year=$('#from_data_year').val();
var to_data_year=$('#to_data_year').val();
$('#to_data_year option').filter(function() {
return $(this).val() < from_data_year;
}).prop('disabled', true);
}
I hope that is like you need it.
I am having an issue fetching the values from a dropdown list to check a condition with the mysql query. It is using codeigniter. Let me post the code I have included below:
VIEW:
<select name="month" id="month">
<option>Select Month</option>
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
The above values should be equal to the month in the date column of the table. I will post my model below:
MODEL:
$month = $this->input->post('month');
$this->datatables->select('order_no,order_date,naturescrm_customers.name,total,naturescrm_users.username,naturescrm_order.id,
naturescrm_order_status.status_name')
->unset_column('naturescrm_order.id')
->add_column('Actions', btn_edit('agent/order/edit/' . '$1').' '.get_view('$1','agent/order/view/') ,'naturescrm_order.id')
->from($this->_table_name)
->join("naturescrm_customers","naturescrm_customers.id=naturescrm_order.customer_id","left")
->join("naturescrm_order_status","naturescrm_order_status.status_id=naturescrm_order.order_status","left")
->join("naturescrm_users","naturescrm_users.id=naturescrm_order.user_id","left")
->where("DATE_FORMAT(order_date, '%m') = $month");
$this->db->order_by("order_date","desc");
There is my code in the model and in the view, So when I Select a month in the dropdown, the values being fetched in the database should reflect based on the month. Any help will be much appreciated. Thanks
I have a drop down input form. I want the selection to be put into an array, and then sent to another page to be displayed. But for some reason, it is not printing.
Let me show you my code so that you can get a better understanding.
Here's what my drop down input form looks like
<label>Start Month:</label>
<select>
<option name="sMonth[]" value="Jan">Jan</option>
<option name="sMonth[]" value="Feb">Feb</option>
<option name="sMonth[]" value="March">March</option>
<option name="sMonth[]" value="April">April</option>
<option name="sMonth[]" value="May">May</option>
<option name="sMonth[]" value="June">June</option>
<option name="sMonth[]" value="July">July</option>
<option name="sMonth[]" value="Aug">Aug</option>
<option name="sMonth[]" value="Sept">Sept</option>
<option name="sMonth[]" value="Oct">Oct</option>
<option name="sMonth[]" value="Nov">Nov</option>
<option name="sMonth[]" value="Dec">Dec</option>
</select>
On the NEXT php page:
<?php $sMonth=$_POST['sMonth']; // start month ?>
<?php echo $sMonth[0]; ?>
But it doesn't print anything.
NOTE = I'm aware that from the drop down menu, the selection will be put into the 1st elemental position of the array. I want to print the first element. If I can print the first element, I will be able to print the rest of the elements.
The problem is with the SELECT, this should contain the name
<select name="sMonth[]" multiple>
<option value="Jan">Jan</option>
<option value="Feb">Feb</option>
etc.
Try this:
Here you made mistake of HTML.
In dd control, SELECT statement contains name property && OPTION statement contains value property. So just change this and your code will working properly.
i.e.
<select name="sMonth[]">
&&
remove name from option tag.
Thanks
<label>Start Month:</label>
<select name="sMonth">
<option value="Jan">Jan</option>
<option value="Feb">Feb</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="Aug">Aug</option>
<option value="Sept">Sept</option>
<option value="Oct">Oct</option>
<option value="Nov">Nov</option>
<option value="Dec">Dec</option>
</select>
<?php $months = $_POST['sMonth']; // start month ?>
<?php print_r($months) ?>
The way you create select dropdown is wrong.
WHY?
Because name tag is not placed in the option tag.
Option tag is only suppossed to carry the value.
Plece your name tag into the selectr tag and you will see your variable is passed into the php.
try this:
<label>Start Month:</label>
<select name="sMonth">
<option value="Jan">Jan</option>
<option value="Feb">Feb</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="Aug">Aug</option>
<option value="Sept">Sept</option>
<option value="Oct">Oct</option>
<option value="Nov">Nov</option>
<option value="Dec">Dec</option>
</select>
//in your php capture your month as below
<?php $sMonth=$_POST['sMonth']; // start month ?>
var_dump($_POST['sMonth']);
//you will see your selected value.
I have this code in html -
<select name="uyear">
<option value="0">YEAR</option>
<option value="1980">1980</option>
<option value="1981">1981</option>
<option value="1982">1982</option>
<option value="1983">1983</option>
<option value="1984">1984</option>
<option value="1985">1985</option>
<option value="1986">1986</option>
<option value="1987">1987</option>
<option value="1988">1988</option>
<option value="1989">1989</option>
<option value="1990">1990</option>
<option value="1991">1991</option>
<option value="1992">1992</option>
<option value="1993">1993</option>
<option value="1994">1994</option>
<option value="1995">1995</option>
<option value="1996">1996</option>
<option value="1997">1997</option>
<option value="1998">1998</option>
<option value="1999">1999</option>
<option value="2000">2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
</select>
Now i have certain values stored in DB i have to fetch the value from the DB and then show the stored year as selected BUT i cannot write this year drop-down list in php. Now how can i show selected value in the above drop-down WITHOUT writing it in php code??
EDIT : its just one drop down that i have shown here..i have huge numbers of drop down written in html and if i go to change them in php then it will take 3-4 days as there r n numbers of drop down on this page. Thats why i need a solution with which i neednot change the select tag ino php code ans still can show selected as per DB value.
Have PHP write at another place:
<body onload="document.getElementsByName('uyear')[0].selectedIndex=$myDearIndex">
If PHP is out, you'll have to do something in JavaScript... and you may need to do a value-search to select the correct index. I submit that it may be as much of a pain doing a function and then scriptlet for each select as it would be just re-writing your select output loops in PHP.
Untested:
<body onload="setDefaultSelections()">
<script>
function setDefaultSelection(selectName, default) {
var select = document.getElementsByName(selectName)[0];
for (i = 0; i < select.length; i++) {
if (select[i].value == default) {
select.selectedIndex = i;
break;
}
}
}
function setDefaultSelections() {
setDefaultSelection('uyear', '<? $uyear ?>');
setDefaultSelection('ucountry', '<? $country ?>');
...
}
</script>