0

I am using a button to submit a form with ajax..all works fine..But I need to check if the button is clicked in server side page..How to do it??Any help appreciated..Thanks..

<form>

some values
</form>
<input type="button" name="delete" id="delete" value="Delete"/><br/>
<input type="button" name="edit" id="edit" value="Edit"/><br/>

Script

$("#edit").click(function(event) {
    event.preventDefault();
     $("#form1").submit()
});

$("#form1").validate({
         debug: false,
   rules: {

    plid:"required",
       },
   messages: {

    plid: "Please select a pack name id..",
    },

    submitHandler: function(form) {

    $.ajax
   ({

type: "POST",
url: "aanew.php",
data: $('#form1').serialize(),
cache: false,

success: function(response) {
    $('#result1').html(response); 

        }
        });
        }

  });

I want to carry any attribute to check if my button is set... Thanks again..

The form value passes successfully, but I need to check the button status in another page..

user2234992
  • 543
  • 4
  • 8
  • 20
  • Your button input-tags should be within the form-tags – RST May 12 '13 at 09:05
  • 1
    create an hidden field with some value in the form and then check it server side – konnection May 12 '13 at 09:05
  • @RST I did that the values that passed is only 'plid' form value...the button are not seen.. – user2234992 May 12 '13 at 09:07
  • @konnection I appreciate your response..Thanks.. I have two buttons..How can I use it to know if one button is clicked? – user2234992 May 12 '13 at 09:09
  • check my response its all there ;) – konnection May 12 '13 at 09:11
  • Do you want to recreate the button on the other page or just check its value and act upon it, or do you want to know whether or not the information was submitted through the form or by someone hacking your system? Or are you looking for something completely different? – RST May 12 '13 at 09:13
  • @konnection ok so I have two buttons..how would a hidden field help..I am seralize the entire from.. so all values will pass..If I understood you right..thanks again – user2234992 May 12 '13 at 09:13
  • sorry the hidden field is just a precaution to check if user came from the form page, if you type directly in the url the page aanew.php it wont work because your hidden field was not set – konnection May 12 '13 at 09:22
  • @konnection The answer you gave helped me..thanks..But the alert(response) is displaying the entire page..What is the problem?? I have no idea of it..I am using only buttons here.. Any idea?? Thanks.. – user2234992 May 12 '13 at 09:31

3 Answers3

1

First of all your buttons in page should be in form tag like this

<form>

some values
<input type="button" name="delete" id="delete" value="Delete"/><br/>
<input type="button" name="edit" id="edit" value="Edit"/><br/>
</form>

then Simply just use isset function

if (isset($_POST['delete'])) 

or

if (isset($_POST['edit']))

whatever you click

chandresh_cool
  • 11,753
  • 3
  • 30
  • 45
0

Put this field on the form, and the other inputs too like others have already pointed

<input type="hidden" name="check_click" value="1">

then in server side

if ((isset($_POST['check_click'])) && ($_POST['check_click'] == 1))
{
//The form was clicked
}

UPDATE:

if you want for each button

    if ((isset($_POST['check_click'])) && ($_POST['check_click'] == 1))
    {
    //The form was clicked

if (isset($_POST['delete']))
{
//delete has some value
}

if (isset($_POST['edit']))
{
//edit has some value
}

    }
konnection
  • 433
  • 4
  • 13
  • Thanks you for your answer..The problem is that in the server side I need to check the isset of both button, separately.. Any idea? If I use this I cannot identity which button was clicked..Thanks you :) – user2234992 May 12 '13 at 09:18
  • Thank you..It worked.. But I like to ask one thing..I am alerting the response here..It seems to alert the whole page in html..Any soultions?? – user2234992 May 12 '13 at 09:27
0

You can insert into the form tag two submit buttons with different values. And on server side check - which one has come. This approach is also make adding Ajax functionality easier - because you can now add the same callback on this two buttons/

Paul Seleznev
  • 672
  • 1
  • 11
  • 24
  • Thank you.. When I used submit button,I didn't get the desired result..When I alert I get the whole page.. – user2234992 May 12 '13 at 09:42
  • Maybe this will give you a clue about what you can do - http://stackoverflow.com/questions/4007942/jquery-serializearray-doesnt-include-the-submit-button-that-was-clicked – Paul Seleznev May 12 '13 at 10:15