2

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

Like: 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

<?php
if(isset($_GET['mars']))
{
  $mars = "0,60";
  echo "Bitte Zahlen Sie noch <input type=\"button\" value=\"$mars\"> Euro<br>";
  echo "<input type=\"submit\" value=\"0,05\" name=\"fcent\">";
  if(isset($_GET['fcent']))
  {
   echo "test";
  }
}

?>
2
  • Do you have a form you are submitting with? Commented Oct 16, 2016 at 18:56
  • is there a form tag som place in your code? Please show the code that makes the form Commented Oct 16, 2016 at 18:57

1 Answer 1

1

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

<?php

    $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>";
    if(isset($_GET['mars'])){
        echo $payForm;
    }
    if(isset($_GET['fcent'])){
        echo $payForm;
        echo "test";
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.