view raw
benjiin benjiin - 7 months ago 65
PHP Question

PHP submit button in echo

I'm try to build a snack machine where you can choose your snack, get the price and then click the button to pay.

The price is 0,60 €
0.05(button) 0.10(button) ....
if you press 0.05-button the price will reduce to 0,55€

Why don't I get a "test" echo after I click the button?

Here is my code

$mars = "0,60";
echo "Bitte Zahlen Sie noch <input type=\"button\" value=\"$mars\"> Euro<br>";
echo "<input type=\"submit\" value=\"0,05\" name=\"fcent\">";
echo "test";



First, there appears to be no form-tag in your code. Without a form tag, it would be a miracle that pressing that button actually submits it via PHP. In other words, you need to wrap your form elements in a <form></form> Tag.

Secondly, the nested if: if(isset($_GET['fcent'])) is unreachable because when you press the fcent button; the $_GET['mars'] is no more in scope and since your code explicitly demands to be run when $_GET['mars'] is SET, nothing would happen. The Snippet below takes this 2 Points into account and you can fine-tune it even further to meet your needs...

NOTE: You have to be sure that your URL reads something similar to this: http://localhost/index.php=mars=some-value


    $mars       = "0,60";
    $payForm    = "<form name='whatever' method='get' action=''><br>";
    $payForm   .= "Bitte Zahlen Sie noch <input type=\"button\" value=\"$mars\"> Euro<br>";
    $payForm   .= "<input type=\"submit\" value=\"0,05\" name=\"fcent\">";
    $payForm   .="</form>";
        echo $payForm;
        echo $payForm;
        echo "test";