Im using php and im checking if a textbox is empty (this textbox contains a date field)
I have something like this
$startPeriod = date("Y-m-d",strtotime($startPeriod));
if(empty($startPeriod){
// do something
}
this if statement does not work because when a date field is empty it gets defaulted to "1969-12-31"
How can i fix this so that I can actually check if the date field textbox was empty?
Wrap it with a If condition
if($startPeriod != "") {
$startPeriod = date("Y-m-d",strtotime($startPeriod));
if(empty($startPeriod){
// do something
}
}
You should check it before itself like this.
if($startPeriod != ""){
$startPeriod = date("Y-m-d",strtotime($startPeriod));
// do something
}
if($startPeriod != "")
{
$startPeriod = date("Y-m-d",strtotime($startPeriod));
if(empty($startPeriod)
{
// your code
}
}
Related
I have an html form that depending on the checkbox my java-script change it from GET to POST. This piece of code is currently working.
My question is I have a variable on the server side that I need to get. The html variable is sent either as POST or GET, but not sure how to retrieve that variable regardless or what method the html uses. I know how to get the variable as either POST or GET manually, but Not sure how to go about accomplishing this automatically. Any suggestions?
$myVariable = $_GET['htmlVariable'] or $myVariable = $_POST['htmlVariable']
1) Use $_GET if you know that data is coming via URL parameter
if (isset($_GET['htmlVariable'] && $_GET['htmlVariable'] != '') {
$htmlVariable = $_GET['htmlVariable'];
}
2) Use $_POST if you know that data is coming via HTTP POST method
if (isset($_POST['htmlVariable'] && $_POST['htmlVariable'] != '') {
$htmlVariable = $_GET['htmlVariable'];
}
3) If you don't know use $_REQUEST
if (isset($_REQUEST['htmlVariable'] && $_REQUEST['htmlVariable'] != '') {
$htmlVariable = $_GET['htmlVariable'];
}
It sounds like you're looking to check if one of the two methods is found. If it is, use that method, and if not, use the other method. To achieve this you can use isset(), and note that you'll also want to check that the string isn't empty:
if (isset($_GET['htmlVariable'] && $_GET['htmlVariable'] != '') {
$myVariable = $_GET['htmlVariable'];
}
else if (isset($_POST['htmlVariable'] && $_POST['htmlVariable'] != '') {
$myVariable = $_POST['htmlVariable'];
}
else {
$myVariable = 'something else';
}
Also note that the order in which you check for the methods may make a difference, depending on your logic.
I have Ajax posting to a php script. One of the posts is a . In the php script, I check if an option in the dropdown was selected. If not, I fill in a default value. This is then submitted to a database. See my below code for checking if an was selected:
if($_POST['dropdownValue'] == null){}
99.9% of the time, this works. I don't select an option, and the default value is returned and this is stored in my database. But now I'm finding NULL rows in my database that are filled with the value of $_POST['dropdownValue']. Should I be using the function is_null()? Or isset()? I saw another post that said to check it with $_POST['dropdownValue'] == ''. Would that be better?
you could try using the isset for this i think
if(isset($_POST['dropdownValue']) && ($_POST['dropdownValue'] != null)
{
'insert that data into my base'
}
Let's just use isset:
if( isset($_POST['fromPerson']) )
{
$fromPerson = '+from%3A'.$_POST['fromPerson'];
echo $fromPerson;
}
I have a form field in my website which i use for a table in my database. i'm using checkboxes for a true/false value. In my case my checkboxes send out an empty string if left unchecked.
I made an if else statement to make sure the empty string is made boolean. however, my if else statements only works for the first one ($ringe). the last two ($halskaeder and $armbaand) does not seem to work, as my database does not register any inputs.
I'm guessing my if else statements has flaws or syntax errors, i'm just too hopeless at PHP to figure out what's wrong.
if ($_REQUEST['ringe'] == ""){
$ringe = '0';
}
else {
$ringe = '1';
}
if ($_REQUEST['halskaeder'] == ""){
$halskaeder = '0';
}
else {
$halskaeder = '1';
}
if ($_REQUEST['armbaand'] == ""){
$armbaand = '0';
}
else {
$armbaand = '1';
}
Any help is highly appreciated!
UPDATE!!!
So I realized my issue is that an unchecked checkbox in html doesn't send and signal. So i have to change that no matter what
AFAIK the check box is not sent if it's not checked. You can do something like:
$default = array(
'ringe' => 0,
'halskaeder' => 0,
'armbaand' => 0,
);
$myFormParams = array_replace($default, $_REQUEST);
var_dump($myFormParams);
few Suggestions... I may add more if you post your form..
1) Trim whitespace from user input (unless you need it)
$input = trim($_REQUEST['user_input']);
2) In this case, you should use empty(), because this function checks to see if the variable exists, and if it's empty, and if it's falsy. You can kill three birds with one stone.
3) Use ternary. It's shorthand for what you're doing.
$input = trim($_REQUEST['user_input']);
$input = empty($input) ? "0" : "1";
I found an answer. My problem wasnt the php code, but the checkboxed not sending any input when unchecked. The workaround is simply putting a hidden input field to send a false signal no matter what. You give it the same name attribute as your real checkbox, which will override when checked.
looks like this:
<input type="hidden" name="ringe" value="0" /><input type="checkbox" name="ringe" value="1" /><p>Ringe</p>
thanks for the help though!
I have a textarea field and want to add a text or php code when the field is empty and if the field is not empty to add another php code. For example:
if the field is empty>do something.....else>do something...
I'm not good at php, so I'll be glad for any help.
Thanks.
p.s.Sorry for my english.
Assuming you're posting this from a form then you can do the following.
I'll write this out the long way so it's easier to understand:
if (!empty($_POST['TEXTAREA_NAME']){
// If the textarea is set then do something here
} else {
// If it is NOT set it then it will do the code here
}
<?php echo isset($_POST['texarea_name']) && !empty($_POST['texarea_name']) ? 'textarea not empty' : 'texarea is empty'; ?>
To test if outside variable is empty or not, you can just compare it with empty string
if ( $_POST['text'] === '') {
echo 'there was something';
} else {
echo 'nothing to say';
}
I have script when I will input data from a textbox.
For example: In the textbox, we may input anything string excepting(admin, Admin, ADMIN, AdmiN). I am using this script, but i think my script is so long
Can you show me other ways, regex possibly, to do this?
<?php
$nama=$_POST['name'];
$pesan=$_POST['pesan'];
if(isset($_POST['submit'])){
if($nama== ("admin") || $nama==("Admin") || $nama==("AdmiN") || $nama=("ADMIN"))
{
echo "Masukkan nama yang lain";
}else{
$sql="insert into table1(nama,pesan)values('$nama','$pesan')";
$result_sql=mysql_query($sql);
header('Location:index.php');
}
}
?>
Convert the string to lower case:
if (strtolower($nama) === 'admin')
Try something like:
//make an array of your restricted words, all in lowercase
$restrictedWordsArr = array("admin"); //and more you want
//then check
if(in_array(strtolower($yourWordToCheck), $restrictedWordsArr)) {
//restricted word
}
else {
//valid
}
No need for regex, simply: strtolower($nama) == "admin"