window.location.href doesn't submit form [duplicate] - javascript

This question already has answers here:
JavaScript post request like a form submit
(32 answers)
How can I submit a form using JavaScript?
(10 answers)
Closed 4 years ago.
I used the Javascript below.
If the time is finished it must redirect to the next page "result.php" by submitting a form. But unfortunately the page displayed is empty.
below my form which must send to the next page "result.php".
I want to have how to send this form and redirect to the next page.
how to send the form with this Javascript to make my page work?
var hoursleft = 3;
var minutesleft = 0;
var secondsleft = 0;
var end1;
var progressBar = document.getElementById('progressBar');
if (localStorage.getItem("end1")) {
end1 = new Date(localStorage.getItem("end1"));
} else {
end1 = new Date();
end1.setMinutes(end1.getMinutes() + minutesleft);
end1.setSeconds(end1.getSeconds() + secondsleft);
end1.setHours(end1.getHours() + hoursleft);
}
progressBar.max = end1 - new Date();
var counter = function() {
var now = new Date();
var diff = end1 - now;
diff = new Date(diff);
var sec = parseInt((diff / 1000) % 60)
var mins = parseInt((diff / (1000 * 60)) % 60)
var hours = parseInt((diff / (1000 * 60 * 60)) % 24);
if (hours < 10) {
hours = "0" + hours;
}
if (mins < 10) {
mins = "0" + mins;
}
if (sec < 10) {
sec = "0" + sec;
}
if (now >= end1) {
clearTimeout(interval);
localStorage.setItem("end", null);
localStorage.removeItem("end1");
localStorage.clear();
if (confirm("TIME UP!"))
window.location.href = "result.php";
//alert("Time finished!");
} else {
progressBar.value = progressBar.max - (end1 - now);
localStorage.setItem("end1", end1);
document.getElementById('hours').innerHTML = "<b>" + hours + "</b> " + ":";
document.getElementById('mins').innerHTML = "<b>" + mins + "</b> " + ":";
document.getElementById('sec').innerHTML = "<b>" + sec + "</b>";
}
}
var interval = setInterval(counter, 1000);
<form class="form-horizontal" role="form" id='question' name="question" method="post" action="result.php">
<?php
$number_question=1;
$limit=10;
$row = mysqli_query( $conn, "select id,question_name,image from testquestion where email = '".$email."' ");
$total = mysqli_num_rows( $row );
$remainder = $total/$number_question;
$i = 0;
$j = 1; $k = 1;
?>
<?php while ( $result = mysqli_fetch_assoc($row) ) {
if ( $i != 0)
echo "<div id='question_splitter_$j'>";
if ( $i == 0)
echo "<div class='cont' id='question_splitter_$j'>";?>
<div id='question<?php echo $k;?>' >
<p class='questions' id="qname<?php echo $j;?>"> <?php echo $k?>.<?php echo $result['question_name'];?>
<input type="checkbox" id="review" name="review" />
<label style="color:#0080ff">Review</label></p>
</br>
<?php echo '<img src="data:image/png;base64,'.($result['image']).'" />'; ?></br><br/><br/>
<input type="text" id="answer<?php echo $result['id'];?>" name="<?php echo $result['id'];?>" placeholder="Enter your answer" />
<br/>
<br/>
</div>
<?php
$i++;
if ( ( $remainder < 1 ) || ( $i == $number_question && $remainder == 1 ) ) {
echo "<button id='".$j."' class='next btn btn-primary' type='submit'>Finish</button>";
echo "</div>";
} elseif ( $total > $number_question ) {
if ( $j == 1 && $i == $number_question ) {
echo "<button id='".$j."' class='next btn btn-primary' type='button'>Next</button>";
echo "</div>";
$i = 0;
$j++;
} elseif ( $k == $total ) {
echo " <button id='".$j."' class='previous btn btn-primary' type='button'>Previous</button>
<a href='review.php'><button id='review.php' class='previous btn btn-primary' type='button'>Review</button></a>
<button id='".$j."' class='next btn btn-primary' type='submit'>Finish</button>";
echo "</div>";
$i = 0;
$j++;
} elseif ( $j > 1 && $i == $number_question ) {
echo "<button id='".$j."' class='previous btn btn-primary' type='button'>Previous</button>
<button id='".$j."' class='next btn btn-primary' type='button' >Next</button>";
echo "</div>";
$i = 0;
$j++;
}
}
$k++;
}
?>
<br/>
<br/>
<br/>
</form>

You have to use document.getElementById('question').submit(); with window.location.href = "result.php"; the POST data isnt posted

Related

How to display count down in loop javascript?

i have this in php file
<body>
<span id="time"></span>
</body>
<?php $var0 = 1; ?>
and this in javascript
function startTimer(duration, display) {
var timer = duration, minutes, seconds;
setInterval(function () {
minutes = parseInt(timer / 60, 10)
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
if(minutes <= 0){
display.textContent = seconds + "s";
}else{
display.textContent = minutes + "m " + seconds + "s";
}
if (--timer < 0) {
timer = duration;
}
}, 1000);
}
window.onload = function () {
var fiveMinutes = 60 * <?php echo $var0; ?>,
display = document.querySelector('#time');
startTimer(fiveMinutes, display);
};
it works perfectly but i need to display this in a loop for example i have 2 variables and i want to display two times with new line .. or when i have n variables i got n new line with the time of the variables that will hold the information from database.. thank you
Here's 5 spans updated on the fly...
<html>
<head>
<title>Multiple Spans</title>
<script type="text/javascript">
var items = 5;
function addSpans() {
var element = document.getElementById("container");
for (var i = 1; i < items; i++)
{
var div = document.createElement('div');
var para = document.createElement('span');
para.id = 'span' + i;
div.appendChild(para);
element.appendChild(div);
}
}
function startTimer(duration, display) {
var timer = duration, minutes, seconds;
setInterval(function () {
minutes = parseInt(timer / 60, 10)
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
var displayElement = document.getElementById("span" + display);
if(minutes <= 0){
displayElement.textContent = seconds + "s";
}
else {
displayElement.textContent = minutes + "m " + seconds + "s";
}
if (--timer < 0) {
timer = duration;
}
}, 1000);
}
window.onload = function () {
addSpans();
var fiveMinutes = 60;
for (var i = 1; i < items; i++) {
startTimer(fiveMinutes, i);
}
</script>
</head>
<body>
<div id="container">
</div>
</body>
</html>
finally I solved the problem and I used this code I integrated with the table with here I can develop further
<table width="100%" cellspacing="0">
<tr>
<th class="topLine">Item(poza)</th>
<th class="topLine">Owner</th>
<th class="topLine">Ultimu Licitator</th>
<th class="topLine">Pret</th>
<th class="topLine">Timp</th>
<th class="topLine"> </th>
</tr>
<tr>
<th> </th>
<th> </th>
<th> </th>
<th> </th>
<th> </th>
<th> </th>
</tr>
<?php
$i =0;
foreach($auctions_inf as $auctions_information){
$test = $auctions_information['date_expired'];
if($i%2==0)
{$class= "thell-a";}
else{$class="tdunkel-a";}
?>
<tr class="thedata ">
<td class="<?php echo $class; ?>">Poza mea</td>
<td class="<?php echo $class; ?>">Zorke</td>
<td class="<?php echo $class; ?>">Tot el</td>
<td class="<?php echo $class; ?>">1200 yang</td>
<td class="<?php echo $class; ?>">
<span id="countdown<?php echo $i;?>" class="timer"></span><br>
<script>
var countDownDate<?php echo $i;?> = new Date("<?php echo $test;?>").getTime();
var x<?php echo $i;?> = setInterval(function() {
var now = new Date().getTime();
var distance<?php echo $i;?> = countDownDate<?php echo $i;?> - now;
var hours = Math.floor((distance<?php echo $i;?> % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance<?php echo $i;?> % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance<?php echo $i;?> % (1000 * 60)) / 1000);
hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
document.getElementById('countdown<?php echo $i;?>').innerHTML = hours +"h "+ minutes + "m " + seconds + "s ";
if (distance<?php echo $i;?> < 0){
clearInterval(x<?php echo $i;?>);
document.getElementById('countdown<?php echo $i;?>').innerHTML = "Expirat";
}
}, 1000);
</script>
</td>
<td class="<?php echo $class; ?>"> <button> Licitează </button></td>
</tr>
<?php
$i++;
}
?>
</table>

Calendar PHP - error at first day

Javascript - functions enable switch months
function goLastMonth(month, year)
{
if (month == 1)
{
--year;
month = 13;
}
--month
var monthstring= ""+month+"";
var monthlength = monthstring.length;
if(monthlength <=1)
{
monthstring = "0" + monthstring;
}
document.location.href ="<?php $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year;
}
function goNextMonth(month, year)
{
if(month == 12)
{
++year;
month = 0;
}
++month
var monthstring= ""+month+"";
var monthlength = monthstring.length;
if(monthlength <=1)
{
monthstring = "0" + monthstring;
}
document.location.href ="<?php $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year;
}
</script>
PHP - creating calendar
if (isset($_GET['day']))
{
$day = $_GET['day'];
}
else
{
$day = date("j");
}
if(isset($_GET['month']))
{
$month = $_GET['month'];
}
else
{
$month = date("n");
}
if(isset($_GET['year']))
{
$year = $_GET['year'];
}
else
{
$year = date("Y");
}
$currentTimeStamp = strtotime( "$day-$month-$year");
$monthName = date("F", $currentTimeStamp);
$numDays = date("t", $currentTimeStamp);
$counter = 0;
?>
<table border='1'>
<tr>
<td><input style='width:50px;' type='button' value='<'name='previousbutton' onclick ="goLastMonth(<?php echo $month.",".$year?>)"></td>
<td colspan='5'><?php echo $monthName.", ".$year; ?></td>
<td><input style='width:50px;' type='button' value='>'name='nextbutton' onclick ="goNextMonth(<?php echo $month.",".$year?>)"></td>
</tr>
<tr>
<td width='50px'>Poniedziałek</td>
<td width='50px'>Wtorek</td>
<td width='50px'>Środa</td>
<td width='50px'>Czwartek</td>
<td width='50px'>Piątek</td>
<td width='50px'>Sobota</td>
<td width='50px'>Niedziela</td>
</tr>
<?php
echo "<tr>";
for($i = 1; $i < $numDays+1; $i++, $counter++)
{
$timeStamp = strtotime("$year-$month-$i");
if($i == 1)
{
$firstDay = date("w", $timeStamp);
for($j = 0; $j < $firstDay-1; $j++, $counter++)
{
echo "<td> </td>";
}
}
if($counter % 7 == 0)
{
echo"</tr><tr>";
}
$monthstring = $month;
$monthlength = strlen($monthstring);
$daystring = $i;
$daylength = strlen($daystring);
echo "<td align='center' ";
echo "><a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&day=".$daystring."&year=".$year."'>".$i."</a></td>";
}
echo "</tr>";
?>
</table>
The calender works well, but when the first day in the month is Sunday, then it starts to count the days from Monday (for example May 2016). How do I fix this?
Try the following code
if($i == 1)
{
$firstDay = date("w", $timeStamp) - 1;
if ($firstDay < 0)
$firstDay = 6;
// Now monday is 0, and sunday is 6
for($j = 0; $j < $firstDay; $j++, $counter++) // remove the -1 in $firstDay-1
{
echo "<td> </td>";
}
}
The first column of your calendar is Monday, and the last column is Sunday. $firstDay = date("w", $timeStamp) returns a number from 0-6 where 0 is Sunday, and 6 is Saturday. So I change it so that 0 is Monday, and 6 is Sunday.
So now the loop for($j = 0; $j < $firstDay; $j++, $counter++)
If Monday, then since it's the first column you don't want the loop to run. On Monday, $firstDay is 0 so the loop doesn't run. If Tuesday, then $firstDay is 1 and the loop runs once to make the empty space for Monday.

got multiples number in php

Make a program that prints the numbers from 1 to 100,
which, if the numbers are multiples of three will print the word
"Max" (without the quotes two), and if the number is a multiple of five
will be scored "Good", and if the number is a multiple of three and five
both, then it will print "MaxGood".
here my script
<?php
for ($x = 1; $x <= 100; $x++) {
echo "$x,";
}
$check_number = 100;
for ($i = 1; $i < $check_number; $i += 3)
{
echo "<br>$i = Max";
}
for ($i = 1; $i < $check_number; $i += 5)
{
echo "<br>$i = Good";
}
for ($i = 1; $i < $check_number; $i += 3 and $i += 5)
{
echo "<br>$i = MaxGood";
}
?>
the last function is wrong please help me
for ($i = 1; $i <= 100; i++) {
echo $i;
if($i % 3 == 0) echo "Max";
if($i % 5 == 0) echo "Good";
echo "<br/>";
}
php modulo is very practicable in this case.
http://php.net/manual/en/internals2.opcodes.mod.php
greetings
<?php
for ($x = 1; $x <= 100; $x++) {
echo "$x, ";
if($x % 3 == 0)echo 'Max';
if($x % 5 == 0)echo 'Good';
echo '<br>';
}
?>
<?php
for ($num = 1; $num <= 100; $num++) {
if ($num % 3 == 0 && $num % 5 == 0)
echo "MaxGood" . '</br>';
elseif ($num % 3 == 0)
echo "Max" . '</br>';
elseif ($num % 5 == 0)
echo 'Good' . '</br>';
else
echo $num . '</br>';
}

creating custom time algorith like timeago in javascript

I want a custom time algorithm like javascript. I was able to achieve this using php but am not able to do the same with javascript!
WHat i had in php was like.....
PHP FIDDLE(i want something like this in js output)
function getTime($mytime){
// $mytime is a datetime sql database format time
$thetime = strtotime($mytime);
$q = new DateTime($mytime);
$current_time = date('Y-m-d H:i:s',time());
$w = new DateTime($current_time);
$result = $w ->diff($q);
$string = "";
$year = $result->y;
$month = $result->m;
$day = $result->d;
$hour = $result->h;
$min = $result->i;
$sec = $result->s;
if($year==0){
if($month==0){
if($day==0){
if($hour==0){
if($min==0){
if($sec <30){
$string .= "Just now";}else if($sec<60){ $string .= "A few seconds ago";}
}else{
if($min<5){$string .="A few minutes ago";}else if($min>=5){$string .=$min . ' minutes ago'; }
}}else{
if($hour<6){$string .=$hour . ' hours ago';}else if($hour>=6){
$chour = (date('H',$thetime));
if( ($chour>=5)&&($chour<=17) ){$string .= 'Today at ' .date('h.m a',$thetime);}else{$string .= 'Tonight at '. date('h.m a',$thetime);}}
}}else{
if($day==1){$string .= "Yesterday at " . date('h:m a',$thetime);}else if(($day>1) && ($day<=7)){$string .= $day . " days ago";}else if($day>7){$string .= date('M, d',$thetime) . ' at '. date('h:i a',$thetime);}
}}else{
if($month<5){$string .=$month . ' months ago'; }else if($month>=5){$string .= date('M,d',$thetime) .' at ' . date('h.i a',$thetime) ;}
}}else{
$string .=date('dS M , Y',$thetime) .' at '. date('h.i a',$thetime);
}
return $string;
}
What i thought was to dynamically update time using javascript! so i did this......
$(function(){
var $second = 1000;
var $minute = $second * 60;
var $hour = $minute * 60;
var $day = $hour * 24;
setInterval(function(){
$('.comment').each(function(){
var $this = $(this);
var $time = new Date($this.attr('datetime'));
var $now = new Date();
var $distance = $now - $time;
var $days = Math.floor($distance / $day);
var $hours = Math.floor(($distance % $day) / $hour);
var $minutes = Math.floor(($distance % $hour) / $minute);
var $seconds = Math.floor(($distance % $minute) / $second);
var $time_string = '';
if($days > 0) {
$time_string = $time_string + $days + ' Day(s) ';
}
if($hours > 0) {
$time_string = $time_string + $hours + ' Hour(s) ';
}
if($minutes > 0) {
$time_string = $time_string + $minutes + ' Minute(s) ';
}
if($seconds > 0) {
$time_string = $time_string + $seconds + ' Second(s)';
}
$this.text($time_string);
});
}, 1000);
});
but his outputs something like this fiddle here.
What i wanted is like my prev. php output i.e X min ago or X seconds ago or 12 May 2014 etc how do i implement my above php code with this js ?

Javascript countdown just working on Chrome

Well, I'm making a game, and in my game I have a countdown script in javascript that receives the date when the building upgrade is over, and makes a countdown, and when the countdown ends, executes a script that upgrades the building to the next level.
But just works in google chrome, and in the other browsers appears like that:
Firefox:
Google Chrome
Just the Javascript:
date_default_timezone_set('europe/lisbon');
$datephp = date('Y-m-d H:i:s');
echo'
<script type="text/javascript">
function cdtd() {
var xmas = new Date("' . $factory_date . '");
var now = new Date();
var timeDiff = xmas.getTime() - now.getTime();
if (timeDiff <= 0) {
clearTimeout(timer);
$("#factory_upgrade").load("/include/factory_upgraded.php");
$("#quantidade_fabricas").load("/include/factory_stats.php");
}
var seconds = Math.floor(timeDiff / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
hours %= 24;
minutes %= 60;
seconds %= 60;' .
"var tempo=('0' + hours).slice(-2)+':'+('0' + minutes).slice(-2)+':'+('0' + seconds).slice(-2);"
.
'
document.getElementById("secsBox").innerHTML = tempo;
var timer = setTimeout("cdtd()",1000);
}
</script>
';
Complete function:
function factory_update($get){
$userid = $_SESSION['userid'];
$query00 = "SELECT * FROM factory_upgrading WHERE userid = '$userid'";
$result00 = mysql_query($query00) or die(mysql_error());
while($row00 = mysql_fetch_array($result00)){
$factory_upgrade = $row00['userid'];
}
if(!isset($factory_upgrade)){
echo "Sem melhoramentos.";
return 0;
}
$query01 = "SELECT * FROM factory_upgrading WHERE userid = '$userid'";
$result01 = mysql_query($query01) or die(mysql_error());
while($row01 = mysql_fetch_array($result01)){
$factory_level = $row01['new_level'];
$factory_date = $row01['upgraded'];
}
if ($get == "load")
{
echo '<div class="message_upgrades" ">';
echo '<div class="loading"><img src="/images/loading.gif"></img></div>';
echo '</div>';}
else
{
date_default_timezone_set('europe/lisbon');
$datephp = date('Y-m-d H:i:s');
echo'
<script type="text/javascript">
function cdtd() {
var xmas = new Date("' . $factory_date . '");
var now = new Date();
var timeDiff = xmas.getTime() - now.getTime();
if (timeDiff <= 0) {
clearTimeout(timer);
$("#factory_upgrade").load("/include/factory_upgraded.php");
$("#quantidade_fabricas").load("/include/factory_stats.php");
}
var seconds = Math.floor(timeDiff / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
hours %= 24;
minutes %= 60;
seconds %= 60;' .
"var tempo=('0' + hours).slice(-2)+':'+('0' + minutes).slice(-2)+':'+('0' + seconds).slice(-2);"
.
'
document.getElementById("secsBox").innerHTML = tempo;
var timer = setTimeout("cdtd()",1000);
}
</script>
';
echo '<div class="success_upgrades">';
echo '<div class="upgrade_text"><b>Nivel: </b>' . $factory_level . '</div><div class="div_separator"></div><div class="upgrade_text"><b>Duração:</b>
<div class="secsBox" id="secsBox"></div>
<script type="text/javascript">cdtd();</script></div><div class="div_separator"></div>
';
echo '<div id="close" class="stop_upgrade" ></img></div>';
echo '</div>';
echo '<script>
$(".stop_upgrade").click(function (e) {
e.preventDefault();
$(factory_upgrade2).empty();
setTimeout(function(){
$("#factory_upgrade2").load("/include/upgrade_cancel.php");
$("#factory_upgrade").load("/include/factory_update.php");
$("#load").load("/include/cabecalho_content.php");
$("#industrial").fadeIn();
$(loader1).delay(1000).hide(0);
}, 100);
});
</script>
';
}
}
The format of $factory_date is invalid according to standard JavaScript and Chrome happens to be able to parse it.
For better results, stick to these Date constructors:
new Date();
new Date(value);
new Date(dateString);
new Date(year, month [, day, hour, minute, second, millisecond]);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Categories