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>";
`
Related
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';
}
}
}
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';
}
I've tried solutions from below links. But none of them give luck.
php-output-text-before-sleep
php-output-data-before-and-after-sleep
php-time-delay-using-ob-flush-with-loading-message
Actually below is my script.
<?php
include 'ini/INI.class.php';
$CompIP = $_SERVER['REMOTE_ADDR'];
$inidata = (parse_ini_file("guard.ini",true));
$atm = time()-$inidata["guard"][$CompIP];
if ($atm>60) { $atm = 1; }
echo "<p>You will be redirected to report page in <span id='counter'>" . $atm . "</span> second(s).</p>";
sleep($atm);
//my
//100
//line
//user report from mysql
$ini = new INI('guard.ini');
$ini->data['guard'][$CompIP] = time();
$ini->write();
?>
Still I get the whole content include 'You will be redirected to .......' after $atm (pause seconds) seconds.
My Workaround
<?php
include 'ini/INI.class.php';
$CompIP = $_SERVER['REMOTE_ADDR'];
$inidata = (parse_ini_file("guard.ini",true));
$atm = (time()-(isset($inidata["guard"][$CompIP]) ? $inidata["guard"][$CompIP] : 0));
if ($atm<60)
{
echo "<p>You will be redirected to report page in <span id='counter'>" . (60-$atm) . "</span> second(s).</p> <script type='text/javascript'> function countdown() { var j = document.getElementById('counter'); j.innerHTML = parseInt(j.innerHTML)-1; if (parseInt(j.innerHTML)<=0) { j.innerHTML = 0; location.href = 'tr.php'; } } for (i=1;i<=" . (60-$atm) . ";i++) { setTimeout(function(){ countdown(); },i*1000); } </script>";
} else { mysqlreport; $ini = new INI('guard.ini');
$ini->data['guard'][$CompIP] = time();
$ini->write();
}
?>
You can use JavaScript for this purpose and pass variables from PHP to JavaScript simply by writing the JavaScript code inside "echo". I think something like this will do the trick.
For redirection, the below example will give you an idea:
<?php
$url = "http://google.com";
$step = "1000";
$start = 12;
echo 'Redirection After <h1 id="counter">'.$start.'</h1> ';
echo '
<script>
var x = '.$start.';
setInterval(function(){
if(x==1){
window.location = "'.$url.'";
}
document.getElementById("counter").innerHTML = x;
x--;
}, "'.$step.'");
</script>';
?>
As for your content that you want to output, just place a tag and with JavaScript. Also, you can update it every 10 seconds; the technique is this, how you do it is up to you.
I have two form
<form name='theForm' id='theForm' method="post">
and
<form id="frmDate" action="?page=absensi" method="post">
I submit first form with this
<input type="radio" name="keterangan" value="Hadir" onChange="theForm.submit();" />
Second form with this
<script type="text/javascript">
jQuery(function(){
$('#tgl').daterangepicker({
arrows:true,
onChange: function(){
$('#frmDate').submit();
}//removed comma here
});
});
</script>
I already can get variable from one of forms with this
<?php
$keterangan = "";
$date1 = "";
$date2 = "";
if(isset($_POST["keterangan"])){
$keterangan = $_POST["keterangan"];
}
elseif(isset($_POST["tgl"])){
$dates = explode("-",$_POST[tgl] );
$date1= date("Y-m-d", strtotime($dates[0]));
if ($dates[0] == $_POST[tgl]){
$date2 = $date1;
}
else{
$date2= date("Y-m-d", strtotime($dates[1]));}
} ?>
But I don't know how to get variable from forms when one of them already submit.
I have try to manipulate code with this:
<?php
if(isset($_POST["keterangan"])){
$keterangan = $_POST["keterangan"];
if(isset($_POST["tgl"]) and $keterangan<>''){
$dates = explode("-",$_POST[tgl] );
$date1= date("Y-m-d", strtotime($dates[0]));
if ($dates[0] == $_POST[tgl]){
$date2 = $date1;
}
else{
$date2= date("Y-m-d", strtotime($dates[1]));}
}
}
elseif(isset($_POST["tgl"])){
$dates = explode("-",$_POST[tgl] );
$date1= date("Y-m-d", strtotime($dates[0]));
if ($dates[0] == $_POST[tgl]){
$date2 = $date1;
}
else{
$date2= date("Y-m-d", strtotime($dates[1]));}
if(isset($_POST["keterangan"]) and $date1<>'' and $date2<>''){
$keterangan = $_POST["keterangan"];}
} ?>
please help me out from my problem. thanks
Use one form instead of two separated and handle the cases on the backend. You can't read the state of one form if another was submitted.
HTTP is stateless. If you submit a form, you lost the data of the second form.
You must use only one form.
This is a js script checkbox to select all dates. It works at Firefox but not at Chrome.
<script type="text/javascript" charset="utf-8">
function toggle(source) {
checkboxes = document.getElementsByName('check_time[]');
for each(var checkbox in checkboxes)
checkbox.checked = source.checked;
}
</script>
<?php
session_start();
include ("include/dbconnect.php");
$timestamp = mysql_real_escape_string(strtotime($_POST['date']));
$doc_username = $_SESSION['username'];
$date = # strip_tags (date("Y-m-d",$timestamp));
$timezone ="Asia/Kuala_Lumpur";
if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
$todaydate = date("Y-m-d");
if($date == '')
{
echo "<i>Please select a date</i>";
}else if ($date < $todaydate)
{
echo "<i>* You are not allowed to choose date before $todaydate</i>";
}else
{
$inc = 1800;
$start = (strtotime('0830')); // 9 AM
$end = (strtotime('1630')); // 5 PM
echo "<b>*Pick if the time is not available</b><br> <br>";
echo "<form action='doc_confirmtime.php' method='POST'>";
echo "<input type='checkbox' onClick='toggle(this)' />Select All<br/>";
for( $i = $start; $i <= $end; $i += $inc )
{
$timeslot = date( 'G:i:s ', $i + $inc );// to the standart format
$query = mysql_query("SELECT * from appointment Limit 1");
while($row = mysql_fetch_array($query))
{
$date2 = $row['date'];
$time = $row['time'];
$query2 = mysql_query("SELECT doc_username, date, time, status from appointment WHERE (doc_username = '$doc_username' AND date = '$date' AND time = '$timeslot') AND (status = '0' OR status = '1' OR status = '2' OR status is NULL)");
$result = mysql_fetch_array($query2,0);
if ($result > 0)
{
echo "<font size='+1' face='Tahoma, Geneva, sans-serif'><input type='checkbox' name='check_time[]' value='$timeslot' checked = 'checked' DISABLED><b>$timeslot</b></font><br>";
}else
{
echo "<font face='Arial, Helvetica, sans-serif'><input type='checkbox' name='check_time[]' value='$timeslot'>$timeslot</font><br>";
}
}
}
echo "<i>*Highlighted Time has chosen by you or your patient</i>";
echo "<br><br>";
echo "<input type='submit' name='submit' value='Submit' style='width:60px; height=30px' />";
echo "<input type='hidden' name='Date' value='$date' >";
echo "</form>";
}
mysql_close($connect);
?>
This is actually a php that run by a JQuery AJAX html page. When submit button is enter, a list of dates(Checkbox) and a "select all" checkbox is shown. Firefox definitely can work but not at Chrome.
I'm not really sure if for each() syntax work in other browsers rather than Firefox?
You should use https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach to iterate over the elements.
I would also recommend using jQuery library as it eases work with Javascript when you don't know much about language it self.
In jQuery it would look like this:
function toggle(source) {
$checkboxes = $('input[name=check_time]');
$checkboxes.each(function(){
$(this).attr("checked", source.checked);
});
}
Your for each loop is still not supported by Chrome. Instead use a for:
for(var i=0; i<checkboxes.length; i++)
{
checkboxes[i].checked = source.checked;
}
Chrome currently only supports Javascript 1.5 and the syntax you used is a feature of Javascript 1.6 (which now looks to be deprecated). The reason for this is because the Chrome developers aim to keep Chrome inline with Safari (the other major webkit user).
Ok, finally I got answer from this page. Here I share the source
<script language="JavaScript">
function toggle(source) {
checkboxes = document.getElementsByName('check_time[]');
for(var i in checkboxes)
checkboxes[i].checked = source.checked;
}
</script>