I have argument which "do something" when i press the searchbutton, but I want to do the same on load webpage
<?php
if (isset($_POST['searchbutton'])) {
$time = time();
$time_now = (date("H:i:s",$time));
echo $time_now;
}
?>
I want to do this when page is loading and after click button searchbutton
Is there any options to use two arguments in this if like this:
if ((window.onload) OR (isset($_POST['searchbutton'])))
Related
How to go back to a certain previous page after submitting input data in a page. I have this "edit info" button to the two different pages. This "edit info" button will be redirected to only one "Edit info" page. What I want to execute is if I am in 1st page and going to edit an info and redirect to only one "Edit info" page, after editing I will be redirected to the previous page which is the 1st page, same goes when I want to edit an info in 2nd page. I am already using 'history.go(-1)', and '(isset($_SERVER['HTTP_REFERER']))' or back and it's not working.
How will I fix this, I guess it's in my parameter or the whole condition.
'''
if($user_no > 0){
$result = $this->User_Model->update($data,$user_no);
if(isset($_SERVER['HTTP_REFERER'])) {
if($_SERVER['HTTP_REFERER'] != 'index.php/home'){
$previous_page = base_url().'index.php/home';
}else{
$previous_page = base_url().'index.php/faculty/detail/'.$user_no;
}
}
}
else{
$result = $this->User_Model->create($data,$user_no);
$previous_page = base_url().'index.php/faculty';
}
$_SESSION['result'] = ($result) ? 'SUCCESS!' : 'ERROR!';
redirect($previous_page);
'''
you need to define the path inside the base_url() function like this (only if the detail function in your controller contains an parameter!!):
$previous_page=base_url('index.php/faculty/detail/'.$user_no);
or by using <a href> tag inside your button like this will make it easier:
<a href="<?php if(condition-1)
{
echo base_url('index.php/home/')?>"
}
else { echo base_url('index.php/faculty/detail/'.$user_no)}?>"
}>Submit</a>
I forgot to state my idea of using the '$_SERVER['HTTP_REFERER']'. I just want to redirect to one of the two previous pages after editing an info. I use '($_SERVER['HTTP_REFERER'] != 'index.php/home')' condition to check if this edit info page has a previous page of either a 1st page - 'index.php/home' or 2nd page - 'index.php/faculty/detail/.$user_no'.
<script>
function refreshPage()
{
document.forms[0].submit();
}
</script>
<?php
foreach($values as $value)
{
?>
<input type = "checkbox" name = "food[]" value = "<?php echo($value['dinner']);?>"
<?php if(isset($_POST['food'])) echo "checked='checked'"; ?> > // note this is still part of
input
<?php
}
?>
<button name = "pass" onClick = "refreshPage()">refresh</button>
<?php
if(isset($_POST['pass'])
{
// do a lot more stuff but I have this for temp info
echo("hello");
// I am printing all the check box values in here I do not
// have the code for it yet but I think ik how to do it
// but since i do not have code for it now it is just empty
}
?>
hi so everytime I click on the button refresh. the isset($_POST['pass']) does not work. I have to click on it a second for it to be set which would then print the hello and table of check items part.
I want it so that if you click on it once it will print the hello. Not twice. How do i fix my code? FYI I know you could do isset($_POST['food']) for it to work. But that will break other parts of my code.
I need the button to set the isset($_POST['pass']) to be True (after one click) after I press the refresh button one time.
i'm quite a beginner with PHP and i tried to make something to get xp when cliking the button. You just need to click and it gives xp, then it refresh the page to refresh the player's stat on screen.
<form method="post">
<p><input type="submit" value="Kill the mob" name="add20xp" /></p>
</form>
<?php
if (isset($_POST['add20xp']))
{
$add20xp =("UPDATE users SET exp = (exp + 20)");
$execadd20xp = mysqli_query($connection, $add20xp);
echo '<meta http-equiv="refresh" content="0.1" />';
}
?>
The problem is that i want to prevent the user from smashing the button to prevent bugs and things like that... I tried to put sleep(1) but i can just keep spamming, wait the seconds and it works so it's not very useful.
Thanks for the help !
Save the last time the update was done in session state. Then, only allow the button to be pressed after (last time + 2 seconds) (Two seconds was chosen since that was the suggested interval in your original question).
if (isset($_POST['add20xp'])) {
if (!isset($_SESSION['last_post'])) {
$_SESSION['last_post'] = 0;
}
$currtime = time();
if ($currtime > ($_SESSION['last_post'] + 2)) {
$_SESSION['last_post'] = $currtime;
// ... process the post.
$add20xp =("UPDATE users SET exp = (exp + 20)"); // fix this line
$execadd20xp = mysqli_query($connection, $add20xp);
echo '<meta http-equiv="refresh" content="0.1" />';
}
}
As #Martin noted above in his comment, you want to do the update only for the user who pressed the button, which is the meaning of the comment "fix this line."
If you want to disable the button for 3 seconds after the form is submitted you can use this:
if(sessionStogare.getItem('submitted') === true){
document.querySelector('input[type="submit"]').disabled = true;
setTimeout(function(){
document.querySelector('input[type="submit"]').disabled = false;
sessionStorage.removeItem("submitted");
}, 3000);
}
document.querySelector("body").onclick = function() {
sessionStorage.setItem("submitted", true);
};
We will note the submission in the sessionStorage and check, if the form has been submitted every time we load the page. Then, we will disable the button and enable it after 3 seconds.
Change your php page to this:
// the beginning of the page:
<?php
// start a SESSION
session_start();
// setup a $_SESSION variable
if (!isset($_SESSION["timestamp"]))
$_SESSION["timestamp"] = 0;
//
// now put the $_POST part
if (isset($_POST['add20xp'])) {
// check the time
$now = time();
if ($now - $_SESSION["timestamp"] > 2) {
// more than 2 seconds have passed since last $_POST
// update the time
$_SESSION["timestamp"] = time();
//
$add20xp =("UPDATE users SET exp = (exp + 20)");
$execadd20xp = mysqli_query($connection, $add20xp);
//
echo '<meta http-equiv="refresh" content="0.1" />';
exit;
} else {
// nothing, just let the page load like it is.
}
}
?>
Notice some important changes:
the use of $_SESSION vars -> these vars are stored and can be
retrieved at every page load -> you can use them to store the last
time an action took place
the $_POST part should be at the beginning
of the page -> otherwise after you send a form, you load the page ->
check the post -> then reload... it's not efficient
if you put the $_POST part at the beginning, you actually don't need the page reload with the meta tag -> because the data are already
updated
i redirect to a page in the same folder using onclick event to show information for different dates , but when i use onclick then it doesn't work , it only works if make a button and put everything in side a form , don't want to use buttons because it doesn't fit the style of the page. Is my code wrong or do i have to do some setting to keep the session alive.
Page A
<php
//enable sessions
session_start();
$todaydate=new datetime();
$todaydate=$todaydate->format("Y-m-d");
//check if session is not set
if(!isset($_SESSION['curdate'])){
$_SESSION['curdate']=$todaydate;
}
// get monday's date
$daynumber=date('N',strtotime($todaydate));
$daynumber=$daynumber - 1;
$mondaydate=date('Y-m-d',strtotime($todaydate . ' - '.$daynumber.' day'));
$_SESSION['mondaydate']=$mondaydate;
$i=0;
While($i != '7'){
$buttondate=date('Y-m-d',strtotime($mondaydate. ' + '.$i." day'));
echo '<span onclick="location=','B',$i,'.php'",'">',$buttondate,'</span>';
$i++;
}
?>
Page B1
<?php
session_start();
$choosedate=$_SESSION['mondaydate'];
$$_SESSION['curdate']=$choosedate;
include_once("a.php""):
?>
I have a PHP calendar application that presents a schedule in table form. Each cell is selectable and has the following Jquery popup code behind it:
document.getElementsByTagName('body')[0].appendChild(f);
if (roleID > 2)
{
window.open("","popUpForm","height=550,width=1050,toolbar=0,menubar=0,location=100,status=no,scrollbars=1,resizable=1,right=300,top=100");
$('.jobTime').submit();
}
That works fine and presents a form called newJobForm.php in the popup window. The user enters selected times...and submits. If there is a conflict with the attempted schedule the window popup is closed and the form is reopened with with conflict information:
else
{
$userSTime = new DateTime($params[0]);
$userSTime = date_format($userSTime,'m/d/Y h:i a');
$userETime = new DateTime($params[1]);
$userETime = date_format($userETime,'m/d/Y h:i a');
$_SESSION['jbNum'] = $params[2];
$_SESSION['asset'] = $params[4];
$_SESSION['userSTime'] = $userSTime;
$_SESSION['userETime'] = $userETime;
$_SESSION['userDesc'] = trim($params[3]);
$_SESSION['conJbNum'] = $msg['JobNum'];
$_SESSION['conSTime'] = date_format($msg['StartTime'], 'm/d/Y h:i a');
$_SESSION['conETime'] = date_format($msg['EndTime'], 'm/d/Y h:i a');
$_SESSION['dueDate'] = $params[5];
$_SESSION['comment'] = $params[6];
$_SESSION['destination'] = $params[7];
$_SESSION['jStat'] = $params[8];
$_SESSION['ujob'] = $params[9];
if ($_SESSION['recurring'] == 'n')
{
echo '<script>window.open("../forms/newJobForm.php","popUpForm","height=550,width=900,status=yes, scrollbars=1, toolbar=no,menubar=no,location=no");</script>';
windowClose();
exit;
}
This part works fine if the user makes the right corrections. However, if the user makes another scheduling error the form will close and redisplay information. At this point the close function quits working and the original parent page doesn't refresh. The parent page not refreshing I kind of understand, but the close function is simply that 'close'.
function windowClose()
{
echo "<script>window.close()</script>";
}
what is going wrong? I can't really post the whole code because there is about 1000 lines of code, give or take... How can I get this to work properly?
Thanks to #CBroe I was able to solve this particular problem with the following modifications to my application. In my file where I check the user input for any conflicts with existing entries I changed:
if ($_SESSION['recurring'] == 'n')
{
echo '<script>window.open("../forms/newJobForm.php","popUpForm","height=550,width=900,status=yes, scrollbars=1, toolbar=no,menubar=no,location=no");</script>';
windowClose();
exit;
}
to:
if ($_SESSION['recurring'] == 'n')
{
echo "<meta http-equiv='refresh' content='0;URL=../forms/newJobForm.php' target = '_SELF'/>";
exit;
}
Which simply refreshes the original form with the new conflict (if there is a conflict) information, or allows the user to submit or cancel the attempted action regardless of how many times they try. And to the form its self I added the following javascript make sure the user's actions refresh the parent page:
window.onunload = refreshParent;
function refreshParent()
{
window.opener.location.href = window.opener.location.href;
}
And the function that creates the initial popup was slightly modified like this:
document.getElementsByTagName('body')[0].appendChild(f);
if (roleID > 2)
{
popUpForm = window.open("","popUpForm","height=550,width=1050,toolbar=0,menubar=0,location=100,status=no,scrollbars=1,resizable=1,right=300,top=100");
$('.jobTime').submit();
}
I hope others will find this useful.