Jump to content

Form Only Submit When Button Clicked


kirbyweb

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

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...