Submit form with multiple conditions not working - php

Situation
I have a submit form. With this form, users are able to reserve a table in a restaurant. With each submit there is some data send to the database with the wp_insert_post function. For example the user_ip and the datum_nieuwe_reservering(explained later). I want to use this to check if the user is allowed to submit a new reservation(prevent spamming). I have tried a couple of thing, but it's not working. See code below.
Question
How can I get this to work? What am I doing wrong?
CODE
reserving.php
<!-- CODE WHEN POST = SUBMIT, MAKE A NEW RESERVATION.
=========================================== -->
<?php
global $wpdb;
$user_ip_db = getUserIP();
$date = date("d-m-Y H:i:s");
//echo $date;
$check_user_ip = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = 'user_ip_reservering' AND meta_value = '$user_ip_db'");
$check_time_new = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = 'datum_nieuwe_reservering'");
print_r($check_time_new);
if ( isset($_POST['submit_reservering']) ) {
if ( (empty($check_user_ip)) && $date > $check_time_new ) {
create_reservering();
echo "Your reservation has been send";
} else {
echo 'You have already reserved';
}
}
?><!-- end CODE FOR MAKING RESERVATION-->
Plugin file
//Using this function to send the current day + 1 day to the database
//with a hidden text field named `datum_nieuwe_reservering`(show below)
//and in the end check if current date >= `datum_nieuwe_reservering`
<?php
function getDateTime(){
date_default_timezone_set('Europe/Amsterdam');
$next_day = time() + (1 * 24 * 60 * 60);
$date_now = date('d-m-Y H:i:s');
$date_tomorrow = date('d-m-Y H:i:s', $next_day);
echo $date_tomorrow;
}
?>
Hidden field for datum_nieuwe_reservering
<input type='hidden' value='<?php echo getDateTime(); ?>' name='datum_nieuwe_reservering'>
NOTE: The submit form is working correctly with the if (empty($check_user_ip)) { }; code only. So I'm struggle to add the date in the if statement too.
I hope someone can help me in the right direction!
Thanks in advance.

Have you tried to use brackets in condition?
if ( isset($_POST['submit_reservering']) ) {
if ( (empty($check_user_ip)) && ($date > $check_time_new) ) {
create_reservering();
echo "Your reservation has been send";
} else {
echo 'You have already reserved';
}
}

if (isset($_POST['submit_reservering']) === true) {
$reserved = false;
if (empty($check_user_ip) === true) {
$test = end($check_time_new);
if(date > $test->meta_value) {
create_reservering();
echo "Your reservation has been send";
} else {
echo 'You have already reserved';
}
}
}

Related

How to make specific button disappear

I am newbie at website programming. This is for my school 'end of the year' project. I need to know how to make upvote/downvote system. I managed to make +1 and -1 buttons for each of my posts, but when I want them to disappear when clicked all of them disappear (instead of only the specific one). The +1 and -1 works for individual posts but my solution does not work. example: When I click button +1 on post id=2 all of +1 buttons disappear and so on. I would like to know the solution for my problem. Thanks in advance
while ($forum = $vysledek->fetch_assoc())
{
if (isset($_SESSION['upvote'])) {
$type = 'hidden';
}
else
{
$type = 'button';
}
if (isset($_SESSION['downvote'])) {
$type1 = 'hidden';
}
else
{
$type1 = 'button';
}
?>
<a href='votes.php?pris=<?php echo $forum['id_prispevek'] ?> & ad_id=1'>
<input type="<?php echo $type ?>" value="+1"></a>
<a href='votes.php?pris=<?php echo $forum['id_prispevek'] ?> & ad_id=0'>
<input type="<?php echo $type1 ?>" value="-1"></a>
}
and on votes.php
<?php
include "pripojeni1.php";
$var_value = $_GET['ad_id'];
$prispevek = $_GET['pris'];
if ($var_value == 1) {
$query = mysqli_query($link,"
UPDATE forum
SET votes = votes + 1
WHERE id_prispevek = '".$prispevek."'
");
header("location:vypisForum.php?var=$prispevek");
$_SESSION['upvote'] = 1;
unset($_SESSION['downvote']);
}
if ($var_value == 0) {
$query = mysqli_query($link,"
UPDATE forum
SET votes = votes - 1
WHERE id_prispevek = '".$prispevek."'
");
header("location:vypisForum.php?var=$prispevek");
$_SESSION['downvote'] = 1;
unset($_SESSION['upvote']);
}
?>

How to get the value which user has selected in php?

I have a little problem here. I have a list, which is:
<form action="display.php" method="post">
<select name="holiday">
<option value="month">Kiekvieno mėnesio skaičiavimas</option>
<option value="day">Kiekvienos dienos skaičiavimas</option>
</select>
<br> <br>
<input type="submit" name="select" value="Pasirinkti" />
</form>
What I need to do is that when the user selects value ="month", php code would do one action, and when the user selects value ="day", php code would do another action?
My php code looks like this:
<?php
if($_POST['select']){
// Storing selected value in a variable
$selectedValue= $_POST['holiday'];
}
else if ($selectedValue == $_POST['month']) {
$todaysDate = new DateTime();
while ($employee = $select->fetch()){
$employmentDateValue = new DateTime($employee['employment_date']);
// Comparing employment date with today's date
$differenceBetweenDates = date_diff($employmentDateValue, $todaysDate);
$workMonths = $differenceBetweenDates->y * 12 + $differenceBetweenDates->m;
$holidayDays = $workMonths *4;
echo "<tr>";
echo "<td>".$employee['name']."</td>";
echo "<td>".$employee['surname']."</td>";
echo "<td>".$employee['employment_date']."</td>";
echo "<td>".$workMonths."</td>";
echo "<td>".$holidayDays."</td>";
echo "</tr>";
}
}
else {
echo "Lalalala";
}
?>
I've tried to do so with $_POST['select'], but it doesn't work. Thanks for any help guys
<?php
if($_POST['select']){
// Storing selected value in a variable
$selectedValue= $_POST['holiday'];
if ($selectedValue == 'month') {
}
else if ($selectedValue == 'day') {
}
else{
echo "Lalalala";
}
}
?>
You need to do $_POST['holiday'], so change:
if($_POST['select']){
// Storing selected value in a variable
$selectedValue= $_POST['holiday'];
}
to
if($_POST['holiday']){
// Storing selected value in a variable
$selectedValue = $_POST['holiday'];
}
You also need to change the line:
else if ($selectedValue == $_POST['month']) {
So it's not part of the original if statement:
if ($selectedValue == 'month') {
// your code
}
else {
echo "Lalalala";
}

How to check past date should not be past from current date in PHP? in this second condition not working

My code is here,I am not able to perfect. when press submit button it check old date correct.. and shows "date must be in future" it correct. and whenever i put future date.. that time not show second alert instead it showing first.
if(isset($_POST['update']))
{
$pdata["card"] = $_POST['smcard'];
$pdata["expiry_date"] = date("Y-m-d" strtotime($_POST['smexdate']));
$dateone = $pdata["expiry_date"];
$nowdate = new DateTime();
if ($dateone < $nowdate)
{
echo "<script>";
echo "alert('date must be in future');";
echo "</script>";
}
elseif ($dateone > $nowdate)
{
echo "<script>";
echo "alert('Okay Good, Your date is in future');";
echo "</script>";
}
update_data('smartcard_data', $pdata, 'promotion_id='.$_REQUEST['promotion_id']);
exit;
}
Try below example to fine date passed or not
$dtA = new DateTime('05/14/2010 3:00PM');
$dtB = new DateTime('05/14/2010 4:00PM');
if ( $dtA > $dtB ) {
echo 'dtA > dtB';
}
else {
echo 'dtA <= dtB';
}

after form submit,if user already login then form will submit else login first then form should be submit automatically

I am trying to implement a form, if user already login then from will submit, else login first then form should be submit automatically with the help of cookie an error is sowing in my code on last move_uploaded_file.
My code is given as follows
<?php
if(isset($_POST['submit'])
{
$ad_title=$mysqli->real_escape_string($_POST['ad_title']);
$category=$mysqli->real_escape_string($_POST['category']);
$sub_category=$mysqli->real_escape_string($_POST['sub_category']);
$description=$mysqli->real_escape_string($_POST['description']);
$rent_amount=$mysqli->real_escape_string($_POST['rent_amount']);
$rent_security=$mysqli->real_escape_string($_POST['rent_security']);
$contact=$mysqli->real_escape_string($_POST['contact']);
$email=$mysqli->real_escape_string($_POST['email']);
$city=$mysqli->real_escape_string($_POST['city']);
$state=$mysqli->real_escape_string($_POST['state']);
$area=$mysqli->real_escape_string($_POST['area']);
$buy=(isset($_POST['buy'])?1:0);
$sell=(isset($_POST['sell'])?1:0);
$rent=(isset($_POST['rent'])?1:0);
$manufacture=$mysqli->real_escape_string($_POST['company_name']);
$conditions=$mysqli->real_escape_string($_POST['condition']);
$rent_option=$mysqli->real_escape_string($_POST['rent_option']);
$a=$_FILES['file']['name'];
$path="image/product/$a";
$b=$_FILES['file2']['name'];
$path2="image/product/$b";
$c=$_FILES['file3']['name'];
$path3="image/product/$c";
$d=$_FILES['file4']['name'];
$path4="image/product/$c";
$e=$_FILES['file5']['name'];
$path5="image/product/$c";
if(isset($_SESSION['user_id'])){
$query=$mysqli->query("insert into ads(product_name,category,sub_category,description,image_1,image_2,image_3,image_4,image_5,city,state,rent_amount,rent_option,security_amount,contact_no,email,area,buy,sell,rent,user_id,manufacture,conditions)values('$ad_title','$category','$sub_category','$description','$a','$b','$c','$d','$e','$city','$state','$rent_amount','$rent_option','$rent_security','$contact','$email','$area','$buy','$sell','$rent','$user_id','$manufacture','$conditions')");
move_uploaded_file($_FILES['file']['tmp_name'],$path) & move_uploaded_file($_FILES['file2']['tmp_name'],$path2) & move_uploaded_file($_FILES['file3']['tmp_name'],$path3) & move_uploaded_file($_FILES['file4']['tmp_name'],$path4) & move_uploaded_file($_FILES['file5']['tmp_name'],$path5);
if($query)
{
echo "success";
}
}else{
$time = time() + 60;
setcookie('email',$email,$time);
setcookie('ad_title',$ad_title,$time);
setcookie('category',$category,$time);
setcookie('sub_category',$sub_category,$time);
setcookie('description',$description,$time);
setcookie('rent_amount',$rent_amount,$time);
setcookie('rent_security',$rent_security,$time);
setcookie('contact',$contact,$time);
setcookie('city',$city,$time);
setcookie('state',$state,$time);
setcookie('area',$area,$time);
setcookie('buy',$buy,$time);
setcookie('sell',$sell,$time);
setcookie('rent',$rent,$time);
setcookie('manufacture',$manufacture,$time);
setcookie('condition',$conditions,$time);
setcookie('rent_option',$rent_option,$time);
setcookie('file',$a,$time);
setcookie('file2',$b,$time);
setcookie('file3',$c,$time);
setcookie('file4',$d,$time);
setcookie('file5',$e,$time);
header("Location:product/login.php"); }
}
if(isset($_COOKIE['email'])){
$email =$_COOKIE['email'];
$cookie2 = $_COOKIE['ad_title'];
$cookie3 = $_COOKIE['category'];
$cookie4 = $_COOKIE['sub_category'];
$cookie5 = $_COOKIE['description'];
$cookie6 = $_COOKIE['rent_amount'];
$cookie7 = $_COOKIE['rent_security'];
$cookie8 = $_COOKIE['contact'];
$cookie9 = $_COOKIE['city'];
$cookie10 = $_COOKIE['state'];
$cookie11 = $_COOKIE['area'];
$cookie12 = $_COOKIE['buy'];
$cookie13 = $_COOKIE['sell'];
$cookie14 = $_COOKIE['rent'];
$cookie15 = $_COOKIE['manufacture'];
$cookie16 = $_COOKIE['condition'];
$cookie17 = $_COOKIE['file'];
$cookie18 = $_COOKIE['file2'];
$cookie19 = $_COOKIE['file3'];
$cookie20 = $_COOKIE['file4'];
$cookie21 = $_COOKIE['file5'];
$cookie22 = $_COOKIE['rent_option'];
$pat="image/product/$cookie17";
$pat2="image/product/$cookie18";
$pat3="image/product/$cookie19";
$pat4="image/product/$cookie20";
$pat5="image/product/$cookie21";
$query1=$mysqli->query("insert into ads(product_name,category,sub_category,description,image_1,image_2,image_3,image_4,image_5,city,state,rent_amount,rent_option,security_amount,contact_no,email,area,buy,sell,rent,user_id,manufacture,conditions)
values
('$cookie2','$cookie3','$cookie4','$cookie5','$cookie17','$cookie18','$cookie19','$cookie20','$cookie21',
'$cookie9','$cookie10','$cookie6','$cookie22','$cookie7','$cookie8',
'$email','$cookie11','$cookie12','$cookie13','$cookie14','$user_id','$cookie15','$cookie16')");
move_uploaded_file($cookie17,$pat)&
move_uploaded_file($cookie18,$pat2)&
move_uploaded_file($cookie19,$pat3)&
move_uploaded_file($cookie20,$pat4)&
move_uploaded_file($cookie21,$pat5);
if($query1){
echo "Succes";
}
else{
echo "Something went wrong.";
}
}
?>
Not a solution to your question: You have no name assigned to your select menu.
That aside, you need to check for the existence of a session variable - assuming you set some sort of session variable when the user logged in? ie: $_SESSION['username']='fred' etc? You can generate javascript that will submit the form at page load.
<form name="appointment" method="POST" action="appointment.php" role="form">
<!-- form contents -->
</form>
<?php
if( isset( $_SESSION['username'] ) ){
echo "
<script type='text/javascript'>
document.querySelectorAll( form['name=\"appointment\"]' )[0].submit();
</script>";
}
?>

Can't get .trigger('click'); working

I'm working on date-based trigger in php, and want to auto click a button with this code:
<?php
$oldDate = metadata('item', array('Dublin Core', 'Date'));
$latestDate = explode("/", $oldDate);
$year = $latestDate[2];
$month = $latestDate[1];
$day = $latestDate[0];
$newDate = $month.'/'.$day.'/'.$year;
$newestDate = new DateTime($newDate);
echo $newestDate->format('jS M Y');
$t = time();
$nt = strtotime($newDate);
if($nt <= $t){
} else {
echo "<script> $(document).ready(function(){
$('#gatemodal').trigger('click');
});</script>";
}
?>
but the trigger click event doesn't fire up. Don't have a clue where the fault is. I've tried changing the trigger line into this:
$('#gatemodal')[0].click();
and
jQuery(document).ready(function(){
jQuery('#gatemodal').trigger('click');
still not working.
On the echo part, set a variable and assign it (e.g. $fail='1'), then on your php file check whether $fail has been set and it is equal to one then have your script.
`<?php if (isset($fail)&& $fail=='1'){ ?>
<script type="text/javascript">
jQuery(document).ready(function(){
$('#gatemodal').trigger('click');
});
</script>";
`

Categories