Jump to content

Form Only Submit When Button Clicked


kirbyweb
 Share

Recommended Posts

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

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

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

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...