1

so I am trying to do something in php based on what button a user presses in the html form.

<?php
if (isset($_POST["submit"])) {
echo "do this";
}else{
echo "do this instead";
}
?>



<form action ="Deliveryslot.php" method="post">

    <span class="em">
    <button type="submit" name = "em" value="1" > <img src="em.png" /></button>
    </span>
    <span class="em">
    <button type="submit" name = "ea" value="2" > <img src="ea.png" /></button>
    </span>
    <span class="em">
    <button type="submit" name = "la" value="3" > <img src="la.png" /></button>
    </span>
    <span class="em">
    <button type="submit" name = "e" value="4" > <img src="e.png"  /></button>
    </span>
    <span class="em">
    <button type="submit" name = "ln" value="5" > <img src="ln.png" /></button>
    </span>
</form>

How can I get the if statement at the top to respond to the values 1,2,3, etc..? So far I can't figure out why the submit function is not working in this code! I am showing it this way because I know this syntax is supposed to be correct and I am asking how I can change it to validate on the values.

6
  • you buttons have no name="i_have_no_name" Commented Sep 12, 2014 at 1:01
  • $_POST is for all the elements with the name attribute. So for example: <button type="submit" name="btn1" value="5" > in PHP $_POST["btn"]. Commented Sep 12, 2014 at 1:01
  • use $delivery = $_POST['submit']; echo $delivery; Commented Sep 12, 2014 at 1:05
  • wow thanks you so much!! my first html php code! thank you guys! Commented Sep 12, 2014 at 1:06
  • You don't have a form element named "submit" and your conditional statement if (isset($_POST["submit"])) relies on it. Commented Sep 12, 2014 at 1:06

2 Answers 2

2

To make it easier, you really don't need to capture each name. Put them in the same name instead. Like this:

<form action ="" method="post">
    <span class="em">
    <button type="submit" name = "submit[em]" value="1" > <img src="em.png" /></button>
    </span>
    <span class="em">
    <button type="submit" name = "submit[ea]" value="2" > <img src="ea.png" /></button>
    </span>
    <span class="em">
    <button type="submit" name = "submit[la]" value="3" > <img src="la.png" /></button>
    </span>
    <span class="em">
    <button type="submit" name = "submit[e]" value="4" > <img src="e.png"  /></button>
    </span>
    <span class="em">
    <button type="submit" name = "submit[ln]" value="5" > <img src="ln.png" /></button>
    </span>
</form>

Then on your PHP:

<?php
if (isset($_POST["submit"])) {
    $submit = $_POST['submit'];
    $key = key($submit); // these get the em, ea, la, e, ln
    $value = $submit[$key]; // and then use that key to get the value
    echo "$key => $value"; // em => 1, ea => 2, la => 3, e => 4, ln => 5
}
?>
Sign up to request clarification or add additional context in comments.

Comments

1

In PHP you get elements by name and you access them by $_POST["elementNameHere"]. So in your code to access the buttons values you do this:

if (isset($_POST["em"])) { // Check that a element with name "em" exist
    $btn1 = $_POST["em"]; // $btn1 = 1
    $btn2 = $_POST["ea"]; // $btn1 = 2
    ...
}

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.