kirbyweb Posted November 20, 2009 Share Posted November 20, 2009 Ok someone suggested I make this in a new thread, so here it goes.When this form submit button is pressed and the you refresh the page, it will send the code again.I want to know if anyone can fix this code, since I know nothing about doing this part.Also when the form is submited, then it will refresh the money so I do not have to refresh it twice. <?phpinclude ('______');?><html><p><form method='POST'>Number: <input size="3" maxlength="4" value="0" name="train_s"><input name="train" type="submit" value="Train"></form><form method='POST'>Number: <input size="3" maxlength="4" value="0" name="untrain_s"><input name="untrain" type="submit" value="Untrain"></form></html><?php//now for the PHP part//first if train button is pressedif ($_POST['train']) {$train_s = strip_tags(addslashes($_POST['train_s'])); //number of soldiers trained//we check if it is numeric and positiveif (!is_numeric($train_s)) { die("Wrong input!"); }if ($train_s < 0) { die("Wrong input!"); }//we check if we have enough money$train_money = 100;if ($username[money] < $train_money) { die("Not enough money to train so many units!"); }//if script got it to this point it was all ok so we can updatemysql_query("UPDATE `castle wars users` SET money=money-'$train_money', units=units+'$train_s' WHERE id='$username[id]'");}//we now make script which will run if we press Untrain buttonif ($_POST['untrain']) {$untrain_s = strip_tags(addslashes($_POST['untrain_s'])); //number of soldiers trained//we check if it is numeric and positiveif (!is_numeric($untrain_s)) { die("Wrong input!"); }if ($untrain_s < 0) { die("Wrong input!"); }//we check if user has enough units to untrainif ($username['units'] < $untrain_s) { die("You cannot untrain so many units!"); }//if script got it to this point it was all ok so we can update, but first calculate amount of gained money$gain_money = $untrain_s * 50;//we update, so we add money gained because of untraining units, and change number of unitsmysql_query("UPDATE `castle wars users` SET money=money+'$gain_money', units=units-'$train_s' WHERE id='$username[id]'");}?> Link to comment Share on other sites More sharing options...
justsomeguy Posted November 20, 2009 Share Posted November 20, 2009 When you refresh a page it will resubmit the post values, if you want to stop that you need to redirect to the same page after you process the form. If they refresh the redirected page it won't resubmit the form. Link to comment Share on other sites More sharing options...
kirbyweb Posted November 20, 2009 Author Share Posted November 20, 2009 I said I know nothing about this, you have to be more clear. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 20, 2009 Share Posted November 20, 2009 I thought redirecting was pretty clear. Redirecting means you send them to a new page. You can also send them to the same page, which is what you want to do. You can use a location header to redirect:header('Location: ' . $_SERVER['PHP_SELF']); Link to comment Share on other sites More sharing options...
kirbyweb Posted November 20, 2009 Author Share Posted November 20, 2009 Can you just write the code, or tell me where to put this, I need it only in php.And its going back to the same page. Link to comment Share on other sites More sharing options...
thescientist Posted November 21, 2009 Share Posted November 21, 2009 Can you just write the code, or tell me where to put this, I need it only in php.And its going back to the same page.really? Link to comment Share on other sites More sharing options...
kirbyweb Posted November 21, 2009 Author Share Posted November 21, 2009 I fixed it. And yes really! Link to comment Share on other sites More sharing options...
thescientist Posted November 21, 2009 Share Posted November 21, 2009 I'm glad you got it working. Link to comment Share on other sites More sharing options...
kirbyweb Posted November 21, 2009 Author Share Posted November 21, 2009 Yes thanks, but now I have a problem that I am not sure if I will be able to fix, I need to make it so in the input, if they just have 1 then they submit, it will only add one unite, and it will only take 100 dollars out since 100 is how it costs per unite.Anyway I need to make it so if they choose 2 it will add to at take 200 dollars, you get it right?How can I do this? Link to comment Share on other sites More sharing options...
thescientist Posted November 21, 2009 Share Posted November 21, 2009 sounds like simple math. what part are you stuck on? Link to comment Share on other sites More sharing options...
kirbyweb Posted November 21, 2009 Author Share Posted November 21, 2009 I cannot figure out what code makes it add what ever number they put in. Link to comment Share on other sites More sharing options...
kirbyweb Posted November 21, 2009 Author Share Posted November 21, 2009 Or where it would go. Link to comment Share on other sites More sharing options...
kirbyweb Posted November 21, 2009 Author Share Posted November 21, 2009 OK I did it, now my question is, for the untrain input, if I do 100 it will still go through with the code, it is not suppose to do this does anyone know why?Also I am trying to figure out in the train input box, if I do 3 it will only do one how can I solve this?Please help really soon. Link to comment Share on other sites More sharing options...
kirbyweb Posted November 21, 2009 Author Share Posted November 21, 2009 Where should I put this header('Location: ' . $_SERVER['PHP_SELF']); ???Please help asap. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 22, 2009 Share Posted November 22, 2009 You put that line at whichever point in the code you want to redirect the user. It's best to use exit after sending the header to stop the rest of the code from running. Link to comment Share on other sites More sharing options...
kirbyweb Posted November 23, 2009 Author Share Posted November 23, 2009 2 questions.1 - How do I make it so if the user puts in 2 and then clicks submit, it will take the 200 dollars, and add 2 to the db, I can make ti add 1, but not anything else.2 - I plugged my printer cord into my laptop, and when I took it out, it snapped the black piece in my laptops usb port off, is there anything I can do? repair, get a replacement, I am using my desktop now because the laptop sparked. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 23, 2009 Share Posted November 23, 2009 You can get the value they enter from $_POST and do whatever you want with it. If you have code that updates the database you should be able to modify that to update with the value they enter.For the laptop, it depends which piece broke off. If the USB port is detached from the motherboard then you can't fix that without replacing the motherboard, which involves stripping everything else out of the laptop to get to it. If it's just the plastic back plate then you can just order a replacement and put it on, but if the thing is sparking it doesn't sound like a plastic plate. If you can find the service manual for your laptop online you can use that as a guide about how to remove and replace everything, I've had to do that once for a friend with an HP laptop who's CPU fan stopped working. Link to comment Share on other sites More sharing options...
kirbyweb Posted November 23, 2009 Author Share Posted November 23, 2009 it only sparked when I stuck something in it, and its a plastic black piece that seperates the needles from touching.And how do I add the values to the db? I am not getting this?? Link to comment Share on other sites More sharing options...
justsomeguy Posted November 23, 2009 Share Posted November 23, 2009 You just send a query to the database, using mysql_query. You can use an INSERT query to add a new record, or an UPDATE query to update an existing record.http://www.w3schools.com/sql/sql_insert.asphttp://www.w3schools.com/sql/sql_update.asp Link to comment Share on other sites More sharing options...
kirbyweb Posted November 23, 2009 Author Share Posted November 23, 2009 Yeah I get that but the units cost 100. and if they type 2 I want it to take 200 away from money I can only get it to do one? How would I do it this way? Link to comment Share on other sites More sharing options...
alexnofue Posted November 23, 2009 Share Posted November 23, 2009 Hi, i don't know if your code has changed a lot but you never touch the $train_money value in the first post. I would believe you need to multiply the units with the money, no? Link to comment Share on other sites More sharing options...
kirbyweb Posted November 23, 2009 Author Share Posted November 23, 2009 Yeah I figured that out just like 2 minutes ago actualy, thanks anyway. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.