PHP Checkbox Assign To Variable - php

This is what I am after here. If the checkbox is checked, then I want to assign these values
$begindate = '20160101';
$enddate = '20161231'
If the checkbox is NOT checked then I want to force the user to select from the date selectors, and if either date is null then throw an error. I have this syntax, and the issue I am having with it is that if the check box is checked AND the user inputs dates then the input dates over-write the checkbox dates. NOT what I want to happen. There may be other syntax flaws in here that my new self has not realized yet, but what needs to be changed in this syntax in order to make it flow properly and execute as I describe above?
<body>
<form>
Display 2016 Data?   <input type = 'Checkbox' Name='twentysix' value="twentysix"><br>
<input type="submit" name="submit" value="Gather">
</form>
</body>
<?php
if (isset($_POST['submit']))
{
if ( isset($_POST['twentysix']) ) {
$begindate = '20160101';
$enddate = '20161231'
} else {
$begindate = $begindate;
$enddate = enddate;
}
$begindateerror = false;
$enddateerror = false;
if (empty($_POST['begindate'])) {
$begindateerror = true;
}
if (empty($_POST['enddate'])) {
$enddateerror = true;
}
if ($begindateerror) {
echo "<strong>Please select a start date.</strong><br>";
} else if ($enddateerror) {
echo "<strong>Please select a end date.</strong><br>";
} else {
$begindate = $_POST['begindate'];
$enddate = $_POST['enddate'];
}
}

Move your if statements for the checkbox to the end
$begindateerror = false;
$enddateerror = false;
if (empty($_POST['begindate'])) {
$begindateerror = true;
}
if (empty($_POST['enddate'])) {
$enddateerror = true;
}
if ($begindateerror) {
echo "<strong>Please select a start date.</strong><br>";
} else if ($enddateerror) {
echo "<strong>Please select a end date.</strong><br>";
} else {
$begindate = $_POST['begindate'];
$enddate = $_POST['enddate'];
}
if ( isset($_POST['twentysix']) ) {
$begindate = '20160101';
$enddate = '20161231'
} else {
$begindate = $begindate;
$enddate = enddate;
}
Note: This is not the best way to do it. I would recommend moving the begin/end date code into the else statment of the checkbox

Related

PHP Conditional logic troubles row 2 won't work without row 1

I'm working on a project that I grew frustrated with because I think I may have been a little to happy with conditional logics. The website is eqcalendar.com and it's for a private entity.
My objective is to make this user friendly so anyone in this group can access a .txt file, edit the variables, and they show up appropriately.
I was trying to make it that if a variable was left blank that no characters would show up but that if the variables are filled, they will show up.
I want to repeat this for 1 day a week spanning out to three months. As I finished day 1, I went onto day 2 but day 2 will not work unless all variables are defined in day 1.
I was also making it possible to turn the day off if the date was not filled, it would add a css inline display: none;. It was working but now it won't. I found the more if statements I add, the less seems to be cooperative.
I've noticed the same thing with the jQuery toggle. The more I add, then no other month will work.
This is supposed to be a simple web app for mobile devices. I have the following elements that I've included:
Above the DOCTYPE, I have the PHP calling in the .txt file
<?php
$title = "EQ Calendar";
include 'planner.txt'; ?>
The planner.txt file looks like the following:
/*** MONTH 1 ***/
$month1 = "February";
/** WEEK 1 **/
$m1w1_Day = "07";
$m1w1_Chapter = "4";
$m1w1_Chapter_Header = "Test";
$m1w1_Chapter_Link = "Testing";
$m1w1_Conf_Header = "Tester";
$m1w1_Conf_Speaker = "Dieter";
$m1w1_Conf_Link = "http://www.google.com";
$m1w1_Teacher = "Steven Scott";
$m1w1_Other = "Presidency Message";
/** WEEK 2 **/
$m1w2_Day = "14";
$m1w2_Chapter = "";
$m1w2_Chapter_Header = "";
$m1w2_Chapter_Link = "";
$m1w2_Teacher = "";
$m1w2_Other = "Stake Conference";
/** WEEK 3 **/
$m1w3_Day = "21";
$m1w3_Chapter = "2";
$m1w3_Chapter_Header = "My Peace I Give unto You";
$m1w3_Chapter_Link = "";
$m1w3_Teacher = "Joshua Hayes";
$m1w3_Other = "";
/** WEEK 4 **/
$m1w4_Day = "28";
$m1w4_Chapter = "";
$m1w4_Chapter_Header = "";
$m1w4_Chapter_Link = "";
$m1w4_Conf_Header = "Be Not Afraid, Only Believe by Dieter F. Uchtdorf";
$m1w4_Conf_Speaker = "Dieter F. Uchtdorf";
$m1w4_Conf_Link = "";
$m1w4_Teacher = "Jory Wahlen";
$m1w4_Other = "";
/** WEEK 5 **/
$m1w5_Day = "";
$m1w5_Chapter = "";
$m1w5_Chapter_Header = "";
$m1w5_Chapter_Link = "";
$m1w5_Conf_Header = "";
$m1w5_Conf_Speaker = "";
$m1w5_Conf_Link = "";
$m1w5_Teacher = "";
$m1w5_Other = "";
The PHP code within the index.php is as follows:
<div class="top-header">
<h1><?php echo $month1 ?> <span><i class="fa fa-close month1"></i></span></h1>
</div>
<div class="col-md-2 col-sm-2"></div>
<div class="col-md-8 col-sm-8">
<p <?php if (empty($m1w1_Day)) { echo "style='display:none;'"; } ?>><strong><?php echo $m1w1_Day ?></strong> <i class="fa fa-ellipsis-v"></i>
<?php
if ($m1w1_Chapter !== "") {
if (empty($m1w1_Chapter)) {
return null;
} else {
echo "Ch. $m1w1_Chapter –";
}
if (empty($m1w1_Chapter_Header)) {
return null;
} elseif ($m1w1_Chapter_Link == "") {
echo " $m1w1_Chapter_Header";
if (empty($m1w1_Teacher)) { return null; } else { echo " – $m1w1_Teacher"; }
} else {
echo " <a href='$m1w1_Chapter_Link'>$m1w1_Chapter_Header</a>";
if (empty($m1w1_Teacher)) { return null; } else { echo " – $m1w1_Teacher"; }
}
} elseif ($m1w1_Conf_Header !== "") {
if (empty($m1w1_Conf_Header)) {
return null;
} elseif ($m1w1_Conf_Link == "") {
echo " $m1w1_Conf_Header";
} else {
echo " <a href='$m1w1_Conf_Link'>$m1w1_Conf_Header</a>";
}
if (empty($m1w1_Conf_Speaker)) {
return null;
} else {
echo " by $m1w1_Conf_Speaker";
if (empty($m1w1_Teacher)) { return null; } else { echo " – $m1w1_Teacher"; }
}
} else {
echo "$m1w1_Other";
if (empty($m1w1_Teacher)) { return null; } else { echo " – $m1w1_Teacher"; }
}
?></p>
</div>
My goal is to be able to repeat this code weekly and monthly. Am I missing any elements that is preventing me from isolating the if conditional to a single variable that is interfering with the other ifs?

How to validate date using PHP?

I have small problem with date validation. It works if it only validates a day, not the whole date.
If, today is 21.07.2015, validation enables dates before the 21st of each month/each year. It should accept only dates starting from today.
Code is here:
if (empty ($_POST['data'])) {
$data_error = "You need to type a date";
++$error_counter;
} elseif ($_POST['data'] < date("d.m.Y")) {
$data_error = "You have chosen incorrect date";
++$error_counter;
} else {
$data = $_POST['data'];
}
You can compare by strtotime()
if (empty ($_POST['data'])) {
$data_error = "You need to type a date";
++$error_counter;
} elseif (strtotime($_POST['data']) < strtotime(date("d.m.Y"))) {
$data_error = "You have chosen incorrect date";
++$error_counter;
} else {
$data = $_POST['data'];
}

php validation from array form

I have a code like this
First looping count how many post the array:
for($i = 0; $i < $jumlah_qty ;$i++) {
if(!empty($qty[$i]) && !empty($id_cat[$i])) {
Insert booking:
$insert_booking_hd = $user_class->select($az);
$id_cates = $id_cat[$i];
for($b = 0;$b<$qty[$i];$b++) {
First validation if $_POST[$id_cates) is set run this code:
if(isset($_POST[$id_cates."".$b])){
$id_seat = $_POST[$id_cates."".$b];
Find the seat number in $select_seat and find if seat number is exist in $seat_number:
$select_seat = $user_class->select($query);
$seat_number = $user_class->select($querys);
$row_seat = $user_class->numrows($select_seat);
$row_seat2 = $user_class->numrows($seat_number);
if($row_seat>0) {
$update_seat = $user_class->update($update_false);
$bol[$b] = FALSE;
} else {
if( $row_seat2>0 ) {
$insert_booking_dt = $user_class->insert($insert);
$update_seat = $user_class->update($update_true);
$bol[$b] = TRUE;
} else {
$bol[$b] = FALSE;
}
}
} else {
$insert_booking_dt = $user_class->insert($insert_without_seat);
$bol[$b] = TRUE;
}
if($bol[$b]) {
echo "FALSE";
header("location:../../../print.php?id=$id_booking");
}
else {
echo "WRONG";
header("location:../../../event.php?msg=Same seat number");
}
}
}
}
Anything wrong with my php validation?
Because if I input array of $id_seat it will always redirect to print.php although validation is FALSE
for example if I input 3 array and then I echo FALSE WRONG FALSE FALSE
still redirect to print.php not to event.php
How can I read if one of array is get WRONG and then redirect to event.php?
How can I read if one of array is get WRONG and then redirect to event.php?
You may break out of for-loops.
Instead of:
else {
echo "WRONG";
header("location:../../../event.php?msg=Same seat number");
}
You could try:
else {
echo "WRONG";
header("location:../../../event.php?msg=Same seat number");
break 2;
}

PHP verification data error

I have an problem with my PHP verification data.
This my code so far
$cek_saldo=mysql_query
("SELECT * FROM t_balance");
while ($data_cek = mysql_fetch_array($cek_saldo));
{
$b_id = $data_cek['badge_id'];
$mon = $data_cek['month'];
$bal = $data_cek['balance_type'];
}
if ($b_id == '$badge_id' AND $mon == '$date_month' AND $b_type == '$jns_saldo')
{
echo "<div class='emp_err warn'>Balance for this month has been added before.</div>";
}
else
{
if($_POST)
{
$query = "INSERT INTO t_balance(badge_id, balance_amount, month, balance_type, date_transaction)
VALUES ('$badge_id', '$saldo', '$bulan', '$jns_saldo', '$date_transaction')
";
$hasil = mysql_query($query);
if($hasil)
{
echo "<div class='emp_err success'>Balance transaction successfully added.</div>";
}
else
{
echo "<div class='emp'>Gagal menambahkan saldo.</div>";
}
}
}
The rule is :
Tabungan Wajib can be submit for 1 time per month. So if twice, it will give error : "Balance for this month has been added before."
Tabungan Tambahan can be submit more than 1 time per month. So if submit more than 1 time, it will saved.
Anyone have a suggestions ?
if ($b_id == "$badge_id" AND $mon == "$date_month" AND $b_type == "$jns_saldo")
{
echo "<div class='emp_err warn'>Balance for this month has been added before.</div>";
}
Your code failed due to the single quotes in the comparison using variables try to learn the difference between single and double quoted strings in php
You don't need to quote.I suppose $badge_id $date_month $jns_saldo come from your form with $_POST
if ($b_id == $badge_id AND $mon == $date_month AND $b_type == $jns_saldo)
This validation will no work
reason being you are selecting multiple rows from database and assigning only the last on e to $b_id .. $mon etc
and you are using single quotes for comparison
RESOLUTION
select data based on some id if you can
$cek_saldo=mysql_query("SELECT * FROM t_balance where id = 'someid'");
while ($data_cek = mysql_fetch_array($cek_saldo));
{
$b_id = $data_cek['badge_id'];
$mon = $data_cek['month'];
$bal = $data_cek['balance_type'];
}
set the error flag
$error = false;
while ($data_cek = mysql_fetch_array($cek_saldo));
{
$b_id = $data_cek['badge_id'];
$mon = $data_cek['month'];
$bal = $data_cek['balance_type'];
if($b_id == $badge_id AND $mon == $date_month AND $b_type == $jns_saldo)
{
$error = true;
break
}
}
if($error)
{
echo "<div class='emp_err warn'>Balance for this month has been added before.</div>";
}
Hope it helps
Use this.
$cek_saldo= mysql_query("SELECT * FROM t_balance");
while ($data_cek = mysql_fetch_array($cek_saldo));
{
$b_id = $data_cek['badge_id'];
$mon = $data_cek['month'];
$bal = $data_cek['balance_type'];
if ($b_id == "$badge_id" && $mon == "$date_month" && $b_type == "$jns_saldo")
{
echo "<div class='emp_err warn'>Balance for this month has been added before.</div>";
}
else
{
if($_POST)
{
$query = "INSERT INTO t_balance(badge_id, balance_amount, month, balance_type, date_transaction)
VALUES ('".$badge_id."', '".$saldo."', '".$bulan."', '".$jns_saldo."', '".$date_transaction."')";
$hasil = mysql_query($query);
if($hasil)
{
echo "<div class='emp_err success'>Balance transaction successfully added.</div>";
}
else
{
echo "<div class='emp'>Gagal menambahkan saldo.</div>";
}
}
}
}

PHP Syntax Error?

I have coded a nice script but i am constantly getting
Error on line 29: Parse error, unexpected T_IF(if)
I have tried debugging code, wasted plenty of time. But nothing, came out.
Here is my code.
<?php
include("geoip.inc");
$ip=$_SERVER['REMOTE_ADDR'];
$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
$country_code = geoip_country_code_by_addr($gi, "$ip");
$referrer=$_SERVER['HTTP_REFERER'];
// Country name is not used so commented
// Get Country Name based on source IP
//$country = geoip_country_name_by_addr($gi, "$ip");
$real=0;
geoip_close($gi);
if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "googlebot")) {
$real = 1;
}
else {
if ($_COOKIE['iwashere'] != "yes") {
setcookie("iwashere", "yes", time()+315360000);
if ($country_code="IN") {
if(preg_match('/google/i', $referrer)) {
$key = "g17x9erm28n7cgifddssfqhgorjf3e"; // Account API Key
$ip = $_SERVER['REMOTE_ADDR']; // IP to Lookup
$result = file_get_contents('http://www.ipqualityscore.com/api/ip_lookup.php?KEY='.$key.'&IP='.$ip);
$real=$result
//$result will be equal to 1 for detected proxies & vpns or equal to 0 for clean IP's
{if($real==0)
{setcookie("testcookie", "testvalue");
if( isset( $_COOKIE['testcookie'] ) ) {
if (isset($_POST['jstest'])) {
$nojs = FALSE;
} else {
// create a hidden form and submit it with javascript
echo '<form name="jsform" id="jsform" method="post" style="display:none">';
echo '<input name="jstest" type="text" value="true" />';
echo '<script language="javascript">';
echo 'document.jsform.submit();';
echo '</script>';
echo '</form>';
// the variable below would be set only if the form wasn't submitted, hence JS is disabled
$nojs = TRUE;
}
if ($nojs){
$real=1;
}
}
else
$real=1;
}
else
$real=1;
} else
$real = 1;
}
else {
$real = 1;
}
} }
if ($real==1) {
include_once('Biggenius1.htm');
}
?>
It is if inside. Please give me advice, on how can i avoid these error. And also is there any alternative to code such complex script with multiple nested if statements?
Please post entire code:
try this
$real = 0;
geoip_close($gi);
if (strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "googlebot")) {
$real = 1;
} else {
if ($_COOKIE['iwashere'] != "yes") {
setcookie("iwashere", "yes", time() + 315360000);
if ($country_code = "IN") {
if (preg_match('/google/i', $referrer)) {
$key = "g17x9erm28n7cgifddssfqhgorjf3e"; // Account API Key
$ip = $_SERVER['REMOTE_ADDR']; // IP to Lookup
$result = file_get_contents('http://www.ipqualityscore.com/api/ip_lookup.php?KEY=' . $key . '&IP=' . $ip);
$real = $result;
//$result will be equal to 1 for detected proxies & vpns or equal to 0 for clean IP's {
if ($real == 0) {
setcookie("testcookie", "testvalue");
if (isset($_COOKIE['testcookie'])) {
if (isset($_POST['jstest'])) {
$nojs = FALSE;
} else {
}
// create a hidden form and submit it with javascript
echo '<form name="jsform" id="jsform" method="post" style="display:none">';
echo '<input name="jstest" type="text" value="true" />';
echo '<script language="javascript">';
echo 'document.jsform.submit();';
echo '</script>';
echo '</form>';
// the variable below would be set only if the form wasn't submitted, hence JS is disabled
$nojs = TRUE;
}
if ($nojs) {
$real = 1;
}
}
else
$real = 1;
}
else
$real = 1;
} else
$real = 1;
}
else {
$real = 1;
}
}
if ($real == 1) {
include_once('Biggenius1.htm');
}
On line 29, $real=$result should end in a semi-colon and on the following line {if($real==0) should be if($real==0){.
The error message is your friend, it suggested you look to line 29.
You placed a curely braces before the if condition
//$result will be equal to 1 for detected proxies & vpns or equal to 0 for clean IP's
{if($real==0)
remove it then your error wil be removed
From reading over your code, it seems like the only errors I can find are these:
{if($real==0)
And:
$real=$result
Which should be changed into:
if($real==0){
And:
$real=$result;
Here are the few errors I found:
if ($country_code="IN") : This is an assignment not comparision, will always return true
$real=$result : Missing Termination ; on the end

Categories