kirbyweb Posted November 17, 2009 Share Posted November 17, 2009 Hopefully this will be my last question for awhile, is it possible to do a if (the html link was pressed), ................else........update.users..you get the point, is it possible to do that? Link to comment Share on other sites More sharing options...
jeffman Posted November 17, 2009 Share Posted November 17, 2009 You can do that in JavaScript for immediate results.For it to work with PHP, the link's href will have to be the address of a PHP script, and you could attach a GET request to the URL via query string.Or the link could trigger some JavaScript that posts a form. If the actual link makes a difference (like maybe you have several to choose from) the script could update a hidden form element based on some data in the link. Then your PHP script could check for that piece of data.If you want all this to happen without a page refresh, use AJAX. Link to comment Share on other sites More sharing options...
kirbyweb Posted November 17, 2009 Author Share Posted November 17, 2009 I want the page to refresh, I want a link to be pressed, and it just refreshes the page, and the bank goes up, well I can do the bank part.I do not know javascript. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 17, 2009 Share Posted November 17, 2009 This is how you use the get method:http://www.php.net/manual/en/reserved.variables.get.phpThat's how you can pass information to PHP through the URL, the data shows up in PHP's $_GET array. In the URL for this thread you see a ? followed by "showtopic=" and a number, PHP has access to that number in $_GET['showtopic']. Link to comment Share on other sites More sharing options...
kirbyweb Posted November 17, 2009 Author Share Posted November 17, 2009 Well I have this code <?phpinclude ('__________');//calculate money needed for next level of bank upgrade (php part of file)$need_money = 200;?><form method='POST'>Bank level: <?php echo $username['bank'];?> <br>Next level: <input type="submit" value="<?php echo number_format($need_money);?> money" name="upgradebank"></form><?php$upgradebank = $_POST['upgradebank'];//now we create another php part of this fileif (isset($upgradebank)) {//we check if user has enough money to upgrade bankif ($username['money'] < $need_money) {//we use die function to stop script execution and to show message what is wrongdie("You don't have enough money to upgrade Bank!");}//everything is OK, we update our user tablemysql_query("UPDATE `castle wars users` SET money=money-'$need_money', bank=bank+'1' WHERE id='$username[id]'") or die(mysql_error());}?> I just want to make the form a link that refreshes the page, and everything else is the same except the form, and maybe something else. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 18, 2009 Share Posted November 18, 2009 First, when you do this: $upgradebank = $_POST['upgradebank'];//now we create another php part of this fileif (isset($upgradebank)) { That if statement will always be true, $upgradebank will always be set. You've set it above to the value in $_POST. If you want to check if the $_POST value was set you need to use that directly:if (isset($_POST['upgradebank']))You can use that to figure out if the form was submitted or not. If you know that the form was already submitted then you can modify whatever you want to modify. It may be easier if you use a variable to track if the form was submitted: $form_submitted = isset($_POST['upgradebank']); Then you can just use if($form_submitted) wherever you want to change something based on whether or not the form was submitted.If you want to make a link to same page, you can use the $_SERVER['PHP_SELF'] variable, e.g.:<a href="<?php echo $_SERVER['PHP_SELF']; ?>?t=<?php echo time(); ?>">Reload</a>The extra variable on the end there will make sure the page isn't cached when they click. If they click that link, the $_POST variable won't be set so you can still track whether or not they submitted the form. If you want to check if they clicked that link, you can check if $_GET['t'] is set. Link to comment Share on other sites More sharing options...
kirbyweb Posted November 18, 2009 Author Share Posted November 18, 2009 ok I get it, the only part I am having trouble with is the last phrase you can check if $_GET['t'] is set. Link to comment Share on other sites More sharing options...
kirbyweb Posted November 18, 2009 Author Share Posted November 18, 2009 OK I did it, the only thing is when I refresh the page, it still clicks the link. Link to comment Share on other sites More sharing options...
chibineku Posted November 18, 2009 Share Posted November 18, 2009 When you refresh a page, any GET parameters still apply because they are part of the query string. To get around that, you would need to do something like set a session variable on the preceeding page, to say 0, and check its value on the page in question. If it is zero, proceed as normal. Increment the session variable. Then if the page is reloaded, the session variable will be one greater and you will know it's been refreshed. The only way back in is through the page that set the session variable to 0 again. However, if you let someone into a page once, then refreshing will also be accompanied by a re-posting of form data, so it still doesn't count as navigating directly to the page. Link to comment Share on other sites More sharing options...
kirbyweb Posted November 18, 2009 Author Share Posted November 18, 2009 I do not get it, is there an easier way? Link to comment Share on other sites More sharing options...
chibineku Posted November 18, 2009 Share Posted November 18, 2009 This way is easy. Say you have page1 that has a form and you want to allow it to be submitted to page2 only once. On page1, put this at the top:<?phpsession_start();$_SESSION["count"] = 0;?>Then whatever else you want. On page2, put this:<?phpsession_start();if($_SESSION["count"] && $_SESSION["count"] > 0) {//already been here, redirect:header("Location: somewhere.html");exit;}$_SESSION["count"]++;//form submit checks?>In order to visit the page, the user must first visit page one. On subsequent visits, even refreshes, the variable $_SESSION["count"] will fail the test. If you want the user only to be able to access this page once per visit, then you could simply add:if($_SESSION["count"]) {//already seen page 2, redirect:header("Location: somewhere.html");exit;} Link to comment Share on other sites More sharing options...
kirbyweb Posted November 18, 2009 Author Share Posted November 18, 2009 I am doing a link though. Link to comment Share on other sites More sharing options...
chibineku Posted November 18, 2009 Share Posted November 18, 2009 Hm, I read a lot of stuff about POST and got a bit lost. However, the session variable thing works no matter how you end up on page 2. I may not be sure what you want to happen... Link to comment Share on other sites More sharing options...
kirbyweb Posted November 18, 2009 Author Share Posted November 18, 2009 Well I want them to click the link, and then the update happens not when they refresh the page. Link to comment Share on other sites More sharing options...
kirbyweb Posted November 18, 2009 Author Share Posted November 18, 2009 OK is there a name I can put in the if statement to make this work, not a $_GET. <a href="<?php echo $_SERVER['PHP_SELF']; ?>?t=<?php echo time(); ?>">Reload</a><p></html><?php//now we create another php part of this fileif (IS THERE A NAME I CAN PUT IN HERE NOT $_GET) Link to comment Share on other sites More sharing options...
justsomeguy Posted November 18, 2009 Share Posted November 18, 2009 You can probably use the referer header. The referer header gets set whenever you click a link, it tells the server where you came from. So this will be true if they clicked the link: if (isset($_GET['t']) && !empty($_SERVER['HTTP_REFERER'])) If they refresh the page the referer should not be set, so that if statement should not be true for a refresh. Link to comment Share on other sites More sharing options...
kirbyweb Posted November 19, 2009 Author Share Posted November 19, 2009 Ok I will try this, but I just noticed, its still updating the database even when it is at 500, I think only when I am logged out. Link to comment Share on other sites More sharing options...
kirbyweb Posted November 19, 2009 Author Share Posted November 19, 2009 This code does not work if (isset($_GET['t']) && !empty($_SERVER['HTTP_REFERER'])) { And also the number goes up over 500 when I am logged out. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 19, 2009 Share Posted November 19, 2009 What do you mean that code doesn't work? That line is just the start of an if statement that checks if $_GET['t'] is set and if $_SERVER['HTTP_REFERER'] is not empty. So whether or not it "works" depends on how you're trying to use it. And also the number goes up over 500 when I am logged out.If the maximum value is 500, then is the code where you update the number checking if the total will be over 500 before it updates the value? Link to comment Share on other sites More sharing options...
kirbyweb Posted November 20, 2009 Author Share Posted November 20, 2009 OK I fixed the link problem, the only problem now is this: <?phpsession_start();$user = $_SESSION['username'];//open my database$connect = mysql_connect('_______', '_____', '___') or die('Couldn\'t connect!');mysql_select_db('_______castlewarslogin');$query = mysql_query("SELECT * FROM `castle wars users` WHERE username = '$user'");$username = mysql_fetch_array($query);$last = $username['lastupdate']; //timestamp from db$extraTime = time() - $last;$money = floor($extraTime / 60);if (($username['money']<500&&$username['bank']==0) || ($username['money']<600&&$username['bank']==1) || ($username['money']<700&&$username['bank']==2)) {if ($money > 0) { //lets update users table mysql_query("UPDATE `castle wars users` SET money=money+$money WHERE id= '$username[id]'") or die(mysql_error());}echo 'user\'s money:'. ((int)$username['money']+(int)$money) .'money.';}elseecho 'user\'s money:'. ((int)$username['money']+(int)$money) .'money.'; //lets update users table mysql_query("UPDATE `castle wars users` SET lastupdate=lastupdate+$extraTime WHERE id= '$username[id]'") or die(mysql_error());?> The number goes over 500 if the bank is at 1 it goes over 600 ect, how do I stop this. Link to comment Share on other sites More sharing options...
thescientist Posted November 20, 2009 Share Posted November 20, 2009 conditional statements can keep your script from exceeding certain conditions. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 20, 2009 Share Posted November 20, 2009 It's best to print everything out that the code is using so that you can see when it messes up and why. So print out every variable that you're using in the if statements, and then when it messes up you can look at what the values were and step through the code to figure out why those values made it do a certain thing. You can use error logging for this, I use this code on my pages to redirect all error messages to a file called error.log:error_reporting(E_ALL);ini_set('error_log', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'error.log');ini_set('html_errors', 0);ini_set('log_errors', 1);ini_set('display_errors', 0);Then, you can use the error_log function to write whatever you want to the error log file. So you can use error_log to record the values that your variables have to help you debug the script, e.g.:error_log('money: ' . $money);Record any variable you use in an if statement to figure out why the if statements were either true or false. Link to comment Share on other sites More sharing options...
kirbyweb Posted November 20, 2009 Author Share Posted November 20, 2009 I need to know how to change it so it does not go over 500, a simple way. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 20, 2009 Share Posted November 20, 2009 Yeah, I know, you already said that. I'm trying to teach you how to figure out why it's doing that, so that you can determine how to fix it. And just to be clear, if programming were "simple" then you'd see a lot more women and children doing it. Link to comment Share on other sites More sharing options...
kirbyweb Posted November 20, 2009 Author Share Posted November 20, 2009 Well, my variables work, so now what? Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.