PHP post - converting time in form - php

i want to build a site, where a user can choose a timeslot. This timeslot should be printed on the following site. But i have a problem with the correct presentation or displaying the chosen time.
Here my code so far:
Site1, where the user choose a timeslot:
<form name="Uhrzeit" action="confirmation.php" method="POST">
<?php
$start = 10;
$end = 18;
for ($time = $start; $time <= $end; $time++) {
echo "<input type='radio' name='Zeit'>";
echo date("H:00", mktime($time+0,25)).'<br>';
}
?>
<input type="submit" value="Wählen">
</form>
Site2, where the chosen time should be printed:
<?php
$new_date = date('H:00', strtotime($_POST['Zeit']));
echo $new_date;
?>
But with this i get everytime the output of "01:00".

Please try below code
<form name="Uhrzeit" action="confirmation.php" method="POST">
<?php
$start = 10;
$end = 18;
for ($time = $start; $time <= $end; $time++) {
echo "<input type='radio' name='Zeit' value='".date("H:00", mktime($time+0,25))."'>";
echo date("H:00", mktime($time+0,25)).'<br>';
}
?>
<input type="submit" value="Wählen">
</form>

Related

Creating a popup after a user has been on the site after a specific period of time

I am creating a popup on my site to try get users to sign up for our mailing list.
By including this php file in all my pages the pop up will appear when they have been navigating through the site for over a minute.
However, if they stop on one page and don't click through I still want it to pop up without having to change the page or refresh the current page.
<?php
session_start();
if(isset($_POST['mailing_list_input']) && $_POST['mailing_list_input']!=""){
$email = $_POST['mailing_list_input'];
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$params = [$email];
$sql = "INSERT INTO mailing_list(email, date_time) VALUES (?,now())";
$stmt = DB::run($sql,$params);
$date = new DateTime();
unset($_SESSION['timer']);
$_SESSION['already_asked'] = $date;
}else{
$email_warning = "<div id='warning'>Email address is not valid</div>";
}
}
if(isset($_POST['mailing_list_hide']) && $_POST['mailing_list_hide']=="hide"){
$date = new DateTime();
unset($_SESSION['timer']);
$_SESSION['already_asked'] = $date;
}
if(!isset($_SESSION['timer']) && !isset($_SESSION['already_asked'])){
$date = new DateTime();
$date->modify("+1 minutes");
$_SESSION['timer'] = $date;
}
if(isset($_SESSION['timer']) && !isset($_SESSION['already_asked']) && !isset($_COOKIE["customer_login"])){
$date = new DateTime();
if($_SESSION['timer'] <= $date){
echo "<div id='popup'>";
echo "<form class='mailing_list_form' method='POST' action='".$_SERVER['PHP_SELF']."'>";
echo "<input type='hidden' id='mailing_list_hide' name='mailing_list_hide' value='hide'>";
echo "<button id='mailing_list_close' name='mailing_list_close' onclick='this.form.submit();'>X</button>";
echo "</form>";
echo "<h2>SUBSCRIBE FOR LATEST DISCOUNTS & COMPETITIONS</h2>";
echo "Stay up to date with our latest products";
echo "<br /><br />";
echo "<form class='mailing_list_form' method='POST' action='".$_SERVER['PHP_SELF']."'>";
echo "<input type='text' id='mailing_list_input' name='mailing_list_input' placeholder='Enter your email address here'>";
echo "<button id='mailing_list_button' name='mailing_list_button' onclick='this.form.submit();'>";
echo "<img src='media/icons/subscribe_icon.png' alt='subscribe icon' width='15px' height='15px'>";
echo "</button>";
echo "</form>";
if(isset($email_warning)){echo $email_warning;}
echo "<p>By continuing you agree to the <u><a href='page/Privacy-and-Cookie-Policy'>Privacy and Cookie Policy</a></u>.</p>";
echo "</div>";
}
}
?>
PHP is a server-side language, (in)activity happens on the client-side. You have to implement you solution on the latter, with JS for example:
var popupTimer,
TIME_OUT = 60;
function displayPopup() {
// display the popup
}
popupTimer = setTimeout(displayPopup, TIME_OUT);
$(document).on('click change keypress', function() {
clearTimeout(popupTimer);
// Reset
popupTimer = setTimeout(displayPopup, TIME_OUT);
});
So this is what I came up with in the end. If they are navigating through the site it will get the session timer and if the stall on the page without clicking through it will just use a JS timer
<script>
function displayPopup(){
$("#popup").fadeIn("slow");
}
function hidePopup(){
$("#popup").css("display", "none");
}
</script>
<?php
if(isset($_POST['mailing_list_input']) && $_POST['mailing_list_input']!=""){
$email = $_POST['mailing_list_input'];
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$params = [$email];
$sql = "INSERT INTO mailing_list(email, date_time) VALUES (?,now())";
$stmt = DB::run($sql,$params);
$date = new DateTime();
unset($_SESSION['timer']);
$_SESSION['already_asked'] = $date;
}else{
$email_warning = "<div id='warning'>Email address is not valid</div>";
}
}
if(isset($_POST['mailing_list_hide']) && $_POST['mailing_list_hide']=="hide"){
$date = new DateTime();
unset($_SESSION['timer']);
$_SESSION['already_asked'] = $date;
}
if(!isset($_SESSION['already_asked'])){
echo "<script>var popup_timer = setTimeout(displayPopup, 10000);</script>";
}
?>
<div id='popup'>
<form class='mailing_list_form' method='POST' action='<?php echo $_SERVER['REQUEST_URI']; ?>'>
<input type='hidden' id='mailing_list_hide' name='mailing_list_hide' value='hide'>
<button id='mailing_list_close' name='mailing_list_close' onclick='this.form.submit();'>X</button>
</form>
<h2>SUBSCRIBE FOR LATEST DISCOUNTS & COMPETITIONS</h2>
<p>Stay up to date with our latest products</p>
<form class='mailing_list_form' method='POST' action='<?php echo $_SERVER['REQUEST_URI']; ?>'>
<input type='text' id='mailing_list_input' name='mailing_list_input' placeholder='Enter your email address here'>
<button id='mailing_list_button' name='mailing_list_button' onclick='this.form.submit();'>
<img src='media/icons/subscribe_icon.png' alt='subscribe icon' width='15px' height='15px'>
</button>
</form>
<?php if(isset($email_warning)){echo $email_warning;} ?>
<p>By continuing you agree to the <u><a href='page/Privacy-and-Cookie-Policy'>Privacy and Cookie Policy</a></u>.</p>
</div>
<?php
if(!isset($_SESSION['timer']) && !isset($_SESSION['already_asked'])){
$date = new DateTime();
$date->modify("+20 seconds");
$_SESSION['timer'] = $date;
echo '<script>hidePopup();</script>';
}
if(isset($_SESSION['timer']) && !isset($_SESSION['already_asked']) && !isset($_COOKIE["customer_login"])){
$date = new DateTime();
if($_SESSION['timer'] <= $date){
echo '<script>displayPopup();</script>';
}else{
echo '<script>hidePopup();</script>';
}
}
if(isset($_SESSION['already_asked'])){
echo '<script>hidePopup();</script>';
}
?>

PHP button click - value / id to pass over

Hey I'm currently having a problem with trying to make my input button have a value of the id / server that I need and it showing a different value this is how my code currently looks I know the HTML and forms are invalid I will be refactoring it however I'm trying to get a server identifier and a id identifier to cross to another page
<?php
$propertyType = $xmlDom1->getElementsByTagName('PropertyType');
$rent = $xmlDom1->getElementsByTagName('rates');
$rooms = $xmlDom1->getElementsByTagName('rooms');
//$server = $xmlDom1->getElementsByTagName('server');
$propertyServer = $xmlDom1->getElementsByTagName('Property');
$propertyID = $xmlDom1->getElementsByTagName('Property');
$imageURL = $xmlDom1->getElementsByTagName('url');
$imageAlt = $xmlDom1->getElementsByTagName('altText');
$server = $propertyID->item($i)->getAttribute('server');
echo '<form action="level5Details.php" method="get" enctype="application/x-www-form-urlencoded">';
echo '<table><th>Type:</th><th>Rent:</th><th>Rooms:</th><th>Server</th>';
$records = $xmlDom1->documentElement->childNodes;
for ($i = 0; $i < $records->length; $i++) {
echo "<tr><td>".$propertyType->item($i)->nodeValue."</td>";
echo "<td>".$rent->item($i)->nodeValue."</td>";
echo "<td>".$rooms->item($i)->nodeValue."</td>";
echo "<td>".$propertyServer->item($i)->getAttribute('Server')."</td>";
echo '<td><img src="data:image/jpeg;base64,'.$imageURL->item($i)->nodeValue.'" alt="'.$imageAlt->item($i)->nodeValue.'"></img></td>';
?>
<td>
<button class="submit" type="submit" value="<?php echo $propertyID->item($i)->getAttribute('pid'); ?>" name="submit22">something </button>
</td>
</tr>
<?php
}
?>
</form>
If you are trying to generate lots of form each with just a button then you need to put the form in a table cell like this
It might also be useful to place the data items you want to pass in hidden fields rather than try and put 2 data items in the button value
<?php
$propertyType = $xmlDom1->getElementsByTagName('PropertyType');
$rent = $xmlDom1->getElementsByTagName('rates');
$rooms = $xmlDom1->getElementsByTagName('rooms');
//$server = $xmlDom1->getElementsByTagName('server');
$propertyServer = $xmlDom1->getElementsByTagName('Property');
$propertyID = $xmlDom1->getElementsByTagName('Property');
$imageURL = $xmlDom1->getElementsByTagName('url');
$imageAlt = $xmlDom1->getElementsByTagName('altText');
$server = $propertyID->item($i)->getAttribute('server');
echo '<table><th>Type:</th><th>Rent:</th><th>Rooms:</th><th>Server</th>';
$records = $xmlDom1->documentElement->childNodes;
for ($i = 0; $i < $records->length; $i++) {
echo "<tr><td>".$propertyType->item($i)->nodeValue."</td>";
echo "<td>".$rent->item($i)->nodeValue."</td>";
echo "<td>".$rooms->item($i)->nodeValue."</td>";
echo "<td>".$propertyServer->item($i)->getAttribute('Server')."</td>";
echo '<td><img src="data:image/jpeg;base64,'.$imageURL->item($i)->nodeValue.'" alt="'.$imageAlt->item($i)->nodeValue.'"></img></td>';
$pid = $propertyID->item($i)->getAttribute('pid');
?>
<td>
<form action="level5Details.php" method="get" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="pid" value="<?php echo $pid;?>">
<input type="hidden" name="server" value="<?php echo $server;?>">
<button class="submit" type="submit" value="submit" name="submit22">something</button>
</form>
</td>
</tr>
<?php
}
?>
If you just want the button to carry all the data and have only one form you will have to package the 2 data items into one string and sent it as the value of the button
<?php
$propertyType = $xmlDom1->getElementsByTagName('PropertyType');
$rent = $xmlDom1->getElementsByTagName('rates');
$rooms = $xmlDom1->getElementsByTagName('rooms');
//$server = $xmlDom1->getElementsByTagName('server');
$propertyServer = $xmlDom1->getElementsByTagName('Property');
$propertyID = $xmlDom1->getElementsByTagName('Property');
$imageURL = $xmlDom1->getElementsByTagName('url');
$imageAlt = $xmlDom1->getElementsByTagName('altText');
$server = $propertyID->item($i)->getAttribute('server');
echo '<form action="level5Details.php" method="get" enctype="application/x-www-form-urlencoded">';
echo '<table><th>Type:</th><th>Rent:</th><th>Rooms:</th><th>Server</th>';
$records = $xmlDom1->documentElement->childNodes;
for ($i = 0; $i < $records->length; $i++) {
echo "<tr><td>".$propertyType->item($i)->nodeValue."</td>";
echo "<td>".$rent->item($i)->nodeValue."</td>";
echo "<td>".$rooms->item($i)->nodeValue."</td>";
echo "<td>".$propertyServer->item($i)->getAttribute('Server')."</td>";
echo '<td><img src="data:image/jpeg;base64,'.$imageURL->item($i)->nodeValue.'" alt="'.$imageAlt->item($i)->nodeValue.'"></img></td>';
$dataPackage = $propertyID->item($i)->getAttribute('pid') . ':' . $server;
?>
<td>
<button class="submit" type="submit" value="<?php echo $dataPackage;?>" name="submit22">something</button>
</td>
</tr>
<?php
}
</table>
</form>
?>
Now in the receiving form unpack the data for use
<?php
// usual checks for things existing
list($pid, $server) = explode(':', $_GET['submit22'];

PHP countdown timer using 'd-m-Y H:i:s' timestamp

Im trying to create a timer that for 1 hour from a timestamp it shows a count down then after that shows a form button but I can't get it to work, I have tried multiple methods I found online but they either display the wrong time, or never show the form. Here is what I have so far:
while($row2 = $result2->fetch_assoc()) {
$timestamp = $row2['timestamp'];
$work_id = $row2['work_id'];
$current_time = date("Y-m-d H:i:s");
$ready_time = date("Y-m-d H:i:s", strtotime($timestamp)+3600);
if($current_time > $ready_time){?>
<form action="" method="POST" onsubmit="return confirm('Are you sure?');">
<input type="hidden" value="<?echo $work_id;?>" name="work_id">
<input type="submit" name="submit" value="Complete Workorder">
</form>
?
}else{
$time1 = new DateTime($current_time);
$time2 = new DateTime($timestamp);
$interval = $time1->diff($time2);
echo $interval->d ." Days ". $interval->h . " Hours, " . $interval->i." Mintues, ".$interval->s." seconds";
}
}
You can calculate the time left in your sql:
SELECT work_id, TIMESTAMPDIFF(SECOND,CURRENT_TIMESTAMP(),`timestamp`) AS timeleft
FROM tablename
Then check that value in your php:
$timeleft = $row2['timeleft'];
$work_id = $row2['work_id'];
if($timeleft >=0): ?>
<form action="" method="POST" onsubmit="return confirm('Are you sure?');">
<input type="hidden" value="<?echo $work_id;?>" name="work_id">
<input type="submit" name="submit" value="Complete Workorder">
</form>
<?php else: ?>
<p>You need to wait <?php echo $timeleft;?> seconds</p>
<?php endif;
If you want to chow a countdown that actually updates without a refresh, you will need to use javascript, in place of <p>You need to wait <?php echo $timeleft;?> seconds</p>

PHP(simple): Trouble with loops

I'm very new to programming, I am trying to take create a converter from Celsius to Fahrenheit to Kelvin. The user inputs the values (in celsius) they want converted in the 2 input boxes and it creates a table using loops. The first set of data where it outputs the amounts in celsius looks great however the second data stream (fahrenheit) will only output one value of celsius which is the converted value of the final nuber in the celsius loop.
<form name="calculator" action="" method="post">
From: <input class="inputbox" type="number" name="one" value="" /><br />
<p>to</p><br>
To: <input class="inputbox" type="number" name="two" value="" /><br />
<input type="submit" class="submit" name="submit" value="Get Conversions!" />
</form>
<br>
<table border='1' cellpadding='5px'>
<tr>
<th>Degrees Celsius</th>
<?php
if ($_POST['submit']) {
$one = $_POST['one'];
$two = $_POST['two'];
}
if ($two < $one) {
echo "<p>Please put the lowest number in the first input box.</p>";
} else if ($one < (-273) OR $two < (-273)) {
echo "<p> Tempature cant go below -273 Celsius (0 kelvin), please enter higher values.</p>";
} else {
$c = $one - 1;
do {
$c++;
echo "<td>" . $c . "</td>";
} while ($c < $two);
}
?>
</tr>
<tr>
<th>Degrees Fahrenheit</th>
<?php
$f = (1.8 * $c) + 32;
do {
$c++;
echo "<td>" . $f . "</td>";
} while ($c < $two);
$k = $x - 273;
?>
</tr>
</table>
Your have 2 problems in your code.
One problem is that you are only assigning c once, when you are running the first while, it's counting C up and then when you get to the second do{}while() it's allready at the maximum.
you should "reset" C after The first while loop like here:
<th>Degrees Fahrenheit</th>
<?php
$c = $one - 1;
Secondly your only counting your f variable once, you should either make a function for it (might be overkill in this case) or move your calculation of f down inside the while loop, the last part of your code would be something like this
Degrees Fahrenheit
<?php
$c = $one - 1;
do {
$f = (1.8 * $c) + 32;
$c++;
echo "<td>" . $f . "</td>";
} while ($c < $two);
$k = $x - 273;
?>
</tr>

Php.Advance weekly calendar one week [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Get next/previous ISO week and year in PHP
I am trying to write a script that will display the days of a week in a table and that will advance a week if a button is clicked. I have managed to get it working up until the point where it reaches the end of the year and then the dates all go wrong. He is what I have so far...
<?
if(isset($_POST['add_week'])){
$week = date('d-m-Y', strtotime($_POST['last_week']));
$new_week = strtotime ( '+1 week' , strtotime ( $week ) ) ;
$new_week = date('d-m-Y', $new_week);
$week_number = date("W", strtotime( $new_week));
$year = date("Y", strtotime( $new_week));
}else{
$week_number = date("W");
$year = date("Y");
}
if($week_number < 10){
$week_number = "0".$week_number;
}
$week_start = date('d-m-Y', strtotime($year."W".$week_number,0));
echo $week.' '.$new_week.' '.$week_number;
?>
<table name="week">
<tr>
<?
for($day=1; $day<=7; $day++)
{
echo '<td>';
echo date('d-m-Y', strtotime($year."W".$week_number.$day))." | \n";
echo '</td>';
}
?>
</tr>
<tr>
<form name="move_weeks" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="last_week" value="<? echo $week_start; ?>" />
<td colspan="7"><input type="submit" name="back_week" value="back_week" />
<input ype="submit" name="add_week" value="add_week" />
</td>
</form>
</tr>
</table>
Some of the values have been echo'd so I can check the values that are being passed are correct and I know I have probably taken extra steps I didn't need to but I am fairly new to this and wanted to make the code easier to folllow whilst I get it working. As I said, the add button works a treat until it hits new year.
Thanks
Ok, made some advancement, works fine until it gets to 2012 then it just runs through 2012 again rather than starting 2013
<?
if(isset($_POST['add_week'])){
$week = date('d-m-Y', strtotime($_POST['last_week']));
$new_week = strtotime ( '+1 week' , strtotime ( $week ) ) ;
$new_week = date('d-m-Y', $new_week);
$week_number = date("W", strtotime( $new_week));
$year = date("Y", strtotime( $new_week));
}else if(isset($_POST['back_week'])){
$week = date('d-m-Y', strtotime($_POST['last_week']));
$new_week = strtotime ( '-1 week' , strtotime ( $week ) ) ;
$new_week = date('d-m-Y', $new_week);
$week_number = date("W", strtotime( $new_week));
$year = date("Y", strtotime( $new_week));
}else{
$week_number = date("W");
$year = date("Y");
}
/*if($week_number < 10){
$week_number = "0".$week_number;
}*/
$week_start = date('d-m-Y', strtotime($year."W".$week_number,0));
echo $week.' '.$new_week.' '.$week_number;
?>
<table name="week">
<tr>
<?
for($day=1; $day<=7; $day++)
{
echo '<td>';
echo date('d-m-Y', strtotime($year."W".$week_number.$day))." | \n";
echo '</td>';
}
?>
</tr>
<tr>
<form name="move_weeks" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="last_week" value="<? echo $week_start; ?>" />
<td colspan="7"><input type="submit" name="back_week" value="back_week" /><input type="submit" name="add_week" value="add_week" />
</td>
</form>
</tr>
</table>
In my opinion you are going to be way better served to make all your calculation based on a unix timestamp value and then convert to string only as needed for output. That way you don't have to deal with week number problems (i.e. week 0), you are not limited to having Monday be the first day of each week (as as is the basis of calculation in date("W")), and you won't have to make a bunch of hacks to look for edge conditions.
So assuming that $_POST['last_week'] is in your d-m-Y format something like this:
if(isset($_POST['add_week'])){
$last_week_ts = strtotime($_POST['last_week']);
$display_week_ts = $last_week_ts + (3600 * 24 * 7);
} else if (isset($_POST['back_week'])) {
$last_week_ts = strtotime($_POST['last_week']);
$display_week_ts = $last_week_ts - (3600 * 24 * 7);
} else {
$display_week_ts = floor(time() / (3600 * 24)) * 3600 * 24;
}
$week_start = date('d-m-Y', $display_week_ts);
For the part where you are looping through the week to display you can use something like this:
for ($i = 0; $i < 7; $i++) {
$current_day_ts = $display_week_ts + ($i * 3600 *24);
echo date('d-m-Y', $current_day_ts);
}

Categories