0

I have a form which has 3 buttons:

<input type='submit' name='mconfirm' value='  Confirm   '>

<input type='submit' name='modify' value='  Modify   '>

<input type='button' name='cancel' value='  Cancel  ' onClick='goVachSub();'></td></tr>

The form also does something along the lines of:

<form name='achievement_modify' method='post' onSubmit='return check_num();' action='".$_SERVER['PHP_SELF']."'>

When either submit buttons are pressed, the check_num() function is run which some checks against some requirements happens. However, I do not need these checks to run when the modify button is pressed, however I still have the modify button as a submit button because of the action='".$_SERVER['PHP_SELF'] which retains all my forms data.

I am looking for a way to either modify check_num such that if it can figure out that modify was pressed on submit, it will not execute the check_num code. Change it such that it is something like: if modifyPressed, do nothing. else check_num code. Or change modify such that it isn't a submit button still able to retain form data.

What actually happens in my form is it has a bunch of text and drop downs, then the user would submit it. The inputted data would be displayed again and then the three mentioned buttons would appear (Confirm, Modify, Cancel) and Modify should take it back to the text and drop downs again with the data intact.

  • 3
    submit buttons which have a name attribute will submit their `value` like any input element if they're the one that was clicked, so `if (isset($_POST['modify'])) { ... }` will capture that sort of thing. – Marc B Oct 08 '13 at 16:17
  • You can check, which button was pressed in your check_num() function. If you don't want to submit the form on certain buttons, you'll just have to prevent the default action (=submit form). – djot Oct 08 '13 at 16:24
  • @MarcB, the OP wants to capture it on the client, not the server. – epascarello Oct 08 '13 at 16:29
  • @djot How do I go about checking which button was pressed in my function? – user2859406 Oct 08 '13 at 16:33
  • possible duplicate of [How can I get the button that caused the submit from the form submit event?](http://stackoverflow.com/questions/2066162/how-can-i-get-the-button-that-caused-the-submit-from-the-form-submit-event) – epascarello Oct 08 '13 at 16:38
  • Answers also here: http://stackoverflow.com/questions/3577469/form-onsubmit-determine-which-submit-button-was-pressed – epascarello Oct 08 '13 at 16:38
  • @user2859406 : in your case, you may set a variable to a certain value when a button is pressed and check it in your function. – briosheje Oct 08 '13 at 16:40
  • I think you may suffer from poorly-designed-page-itis... It's pretty poor practice to have all of your javascript, php, and html in the same page. – Charles D Pantoga Oct 08 '13 at 18:20

2 Answers2

0
<?php
echo (isset($_POST['mconfirm']))?("mconfirm was pressed"):("mconfirm wasn't pressed");
?>

using isset on $_POST['nameattr'] will tell you if the button has been submitted or not.

Assuming you pressed the "mconfirm" submit button, the above code will eco "mconfirm was pressed".

Approach without ternary operator:

    <?php
if (isset($_POST['mconfirm'])) {
            // mconfirm was pressed
        }
        elseif (isset($_POST['modify'])) {
            // modify was pressed
        }
        else {
            // noone of them was pressed
        }
?>

It can also be done with javascript, of course, but if you DON'T need to check it exactly when the user click the button then it's fine to do that with php.

Hope this helps.

Edit after the comment: fine, then use javascript, when I firstly checked the post that wasn't written.

check which button is pressed with jQuery (jQuery is in the tags, if I'm not going wrong):

$('input[name=mconfirm]').click(function(){
// mconfirm was pressed, check what you need
});

fiddle: http://jsfiddle.net/hEQ4Q/

To get the value:

$('input[name=ciccio]').click(function(){
    alert($(this).val()); 
});
briosheje
  • 7,356
  • 2
  • 32
  • 54
  • The OP wants to run the JS function only if a certain button was clicked! Not check what was clicked on the serverside. – epascarello Oct 08 '13 at 16:28
  • I'm sorry sir, it was not written when I firstly checked the post. I've updated the post above showing a comfort way to check which button was pressed with jQuery too. – briosheje Oct 08 '13 at 16:36
0

Basically the best solution as your need is to add check on button click not on form submit.

<input type='submit' name='mconfirm' value='  Confirm   ' onclick="return check_num();">

<input type='submit' name='modify' value='  Modify   '>

<input type='button' name='cancel' value='  Cancel  ' onClick='goVachSub(); return check_num();'>

Or in jquery

$('your form selector').find('input[type="submit"]').click(function(e){
      var name=this.name;
      if(name!="modify"){
         //do your check
      }
});

Unfortunately there is no solution for this as event object does not contain data about the submit button through which the form submit has been triggered, as you can also call a form submit through javascript without clicking any submit button.

For mozilla only if you form submit is triggered by submit button.

<form name='achievement_modify' method='post' onSubmit='return check_num(event);' action='".$_SERVER['PHP_SELF']."'>

//on your check_num function

function check_num(e){
    var targetBtn=e.explicitOriginalTarget;
    if(targetBtn.name!="modify"){
      //do stuff
    }
}

Best way is to handle it is through button only, but if you even want which button is clicked, set click event on all button which will set variable or attribute in form which will hwlp to identify which one is clicked. For ex: jquery example

var form=$('your form selector');
form.find('input[type="submit"]').click(function(e){
      var name=this.name;
      form.attr("data-triggeredBy",name);
});

//than in your check num function
function check_num(){
   var btnName=$('your form selector').attr("data-triggeredBy");
   if(btnName!="modify"){
     //do stuff
   }
}
Sudhanshu Yadav
  • 2,323
  • 18
  • 18
  • Actually, I think the first idea you mentioned totally slipped my mind and might actually work...I just need to take away the onSubmit and put the function onClick. When the form loads again from the modify button being pressed, it still has the $_POST array data because of the action of submitting. – user2859406 Oct 08 '13 at 17:37
  • @user2859406 Yes first approach is better for you. Just for explanation i wrote others. – Sudhanshu Yadav Oct 08 '13 at 17:41