2

I have a Bootstrap form with Double Buttons (2) as shown below.

  • The submit button fires and returns processing to the page (i.e., ['PHP_SELF'])
  • However, the cancel button does nothing

How can I get both buttons to fire and return processing to the page?

<form class="form-horizontal" method="post">
<fieldset>

<legend>My Form<</legend>

<div class="form-group">
  <label class="col-md-4 control-label" for="submit"></label>
  <div class="col-md-8">
    <button id="submit" name="submit" class="btn btn-primary" value="1" type="submit">Update Profile</button>
    <button id="cancel" name="cancel" class="btn btn-default" value="0" type="submit">Cancel Update</button>
  </div>
</div>

</fieldset>
</form>

Appended (per threaded comment from @Fred): added type="submit" to both buttons but that did not make it work. This is the answer -- it works when you add type="submit"

Appended: here is my server-side code which has been my proof that cancel button does nothing -- didn't think I needed to prove why the cancel button did not work -- was just thinking than an experienced Bootstrap developer would have know right off the bat what was missing:

<?php
//This is Server-Side
  if( $_POST['cancel'] ) {
    echo 'cancel button pressed';
  }
  elseif( $_POST['submit'] ) {
    echo 'submit button pressed';
  }
?>
H. Ferrence
  • 7,906
  • 31
  • 98
  • 161
  • *"However, the cancel button does nothing"* - hard to say why that is. You tagged as PHP, so you may not have the POST array set for it. You also don't have types for your buttons. – Funk Forty Niner Nov 01 '15 at 13:05
  • "Ajax is an option" true @Fred but I am not ready to go down that path with this form. Plus I am just looking for a fundamental explanation as to why both buttons do not work as I begin to learn and code in Bootstrap. – H. Ferrence Nov 01 '15 at 13:07
  • look at my other/2nd comment. – Funk Forty Niner Nov 01 '15 at 13:07
  • There's no functionality in bootstrap. LOL. It is either jquery or javascript. – aldrin27 Nov 01 '15 at 13:09
  • added ` type="submit"` but that did not make it work – H. Ferrence Nov 01 '15 at 13:10
  • if your PHP doesn't have `$var=$_POST['cancel'];` then that's why it's failing. Check for errors, check your console if you're using JS that you're not showing, including the PHP I asked for. I can't help you any further. – Funk Forty Niner Nov 01 '15 at 13:11
  • Sorry for the misunderstanding at @aldrin27 but is your LOLL directed at me or Bootstrap? Again, I am beginning to learn the framework so the subtle remark goes over my head at this juncture. – H. Ferrence Nov 01 '15 at 13:11
  • you probably got that downvote because of missing information. You've given the community an HTML form and nothing else. and please don't shout,that doesn't help you here. I for one tried to help you but you're not helping us here. – Funk Forty Niner Nov 01 '15 at 13:15
  • The HUGEST problem with this service is downvotes do not require explanations. So unproductive and discouraging to want to come here to post and learn. – H. Ferrence Nov 01 '15 at 13:16
  • again; post your PHP or check for errors with error reporting. Your question is unclear. again... if you're using JS, check your console and/or post it in your question. Again... I for one can't help you if I/we don't know what the PHP is and if the problem is in there or not. Why are you adament on not posting the PHP and probable JS? – Funk Forty Niner Nov 01 '15 at 13:17
  • added the PHP as an addendum – H. Ferrence Nov 01 '15 at 13:22
  • The answer is the ` – H. Ferrence Nov 01 '15 at 13:38
  • I posted something 15 minutes ago but no response. I made a few edits since then, therefore you will need to reload my answer. If that does nothing to solve this, do let me know. – Funk Forty Niner Nov 01 '15 at 13:38

2 Answers2

2

The problem here is that you're using everything on the same page, HTML form and PHP.

The reason why it's failing is because you need to use isset() for both of your POST arrays.

<?php
//This is Server-Side
  if(isset($_POST['cancel'] )) {
    echo 'cancel button pressed';
  }
  if(isset($_POST['submit'] )) {
    echo 'submit button pressed';
  }
?>

Error reporting would have helped you here.

  • Use a type="submit" for your buttons.

This answer is based on what you posted with the HTML form and PHP. If Javascript is used here, then that is the type of "firing" mechanism needed.

If you want to return to the processing page, you can use a header to redirect or a meta refresh or a JS solution.

Consult the following on Stack on doing a redirect:


Another option would be to give both your buttons the same name attribute, and doing:

HTML

<input type="submit" name="action" value="Update Profile" />
<input type="submit" name="action" value="Cancel Update" />

PHP

if ($_POST['action'] == 'Update Profile') {
    //action for update here
} else if ($_POST['action'] == 'Cancel Update') {
    //action for delete
} else {
    //invalid action!
}

The above was borrowed from this answer on Stack https://stackoverflow.com/a/547848/

You can replace the input types as buttons and using <button type="submit" ...>

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • You are correct on the "another option" @Fred. That will work as well as adding `type="submit"` to the ` – H. Ferrence Nov 01 '15 at 13:42
  • @H.Ferrence Yeah, it's always nice to have more options and knowing about them. Sometimes that makes a difference and especially if Javascript is used. Many times, a button type is what is needed in JS. *Cheers* – Funk Forty Niner Nov 01 '15 at 13:47
0

You have basic problem with HTML and PHP knowledge, and not with bootstrap.

Search SO before you post: Two submit buttons in one form

Should be marked as duplicate.

Community
  • 1
  • 1
Izzy
  • 402
  • 6
  • 16