Page 1 of 1

How identify the button click without knowing the name

Posted: Tue Aug 12, 2008 12:43 pm
by ravx
Hi, basically I have code which query the database and recieve the IDs and than using the ID in a loop to go though the data, the submit button are assigned that ID.

Code: Select all

<input type='submit' value='Remove listing' name='$a1[car_listing_ID]' />
I know how to check if a button is clicked as the name is knowed but as the name is the ID i cannot know the name of the button right, so how do I check if the button has been clicked and using name of the button to perform function to the row with the ID as according to the submit button.

Thanks in advance

Re: How identify the button click without knowing the name

Posted: Tue Aug 12, 2008 1:13 pm
by EducatedFool
maybe by using JavaScript.

Code: Select all

 
<input type='hidden' value='' name='id'>
<input type='submit' value='Remove listing' name='$a1[car_listing_ID]' onClick='thisfunction($a1[car_listing_ID]);'/>
 
Write a javascript function 'thisfunction' where you put the value of the button in the hidden field. If you submit your form you can get the value from the hidden field 'id'.

Re: How identify the button click without knowing the name

Posted: Tue Aug 12, 2008 1:23 pm
by ravx
EducatedFool wrote:maybe by using JavaScript.

Code: Select all

 
<input type='hidden' value='' name='id'>
<input type='submit' value='Remove listing' name='$a1[car_listing_ID]' onClick='thisfunction($a1[car_listing_ID]);'/>
 
Write a javascript function 'thisfunction' where you put the value of the button in the hidden field. If you submit your form you can get the value from the hidden field 'id'.
Hi I got it working I realised The if statement to get the id of the button was outside the while loop thanks

Re: How identify the button click without knowing the name

Posted: Tue Aug 12, 2008 1:24 pm
by onion2k
Don't use Javascript for something as important as that. Not all users have it switched on.

Perform the same loop as you do to output the button HTML, and check if the button was pressed for each record..

Code: Select all

//whatever you do to start the loop 
{
  if ($_POST[$a1[car_listing_ID]] == "Remove listing") {
    //Remove listing
  }
}
 

Re: How identify the button click without knowing the name

Posted: Tue Aug 12, 2008 1:33 pm
by ravx
onion2k wrote:Don't use Javascript for something as important as that. Not all users have it switched on.

Perform the same loop as you do to output the button HTML, and check if the button was pressed for each record..

Code: Select all

//whatever you do to start the loop 
{
  if ($_POST[$a1[car_listing_ID]] == "Remove listing") {
    //Remove listing
  }
}
 
Hi, Thats exactly more or less what i did lol

Thanks alot

Re: How identify the button click without knowing the name

Posted: Tue Aug 12, 2008 2:53 pm
by ravx
Hi, Ok i ran into a problem bascially when some one clicked remove, I want a message saying are you sure you want to delete and a yes and no option.

Code: Select all

if (isset($_POST[$adid]))
            {
             $clickedremove = $_POST[$adid];
             $_POST[error] = "Are you sure you want to delete <br/> ";
    $deletebutton = "<input type=\"submit\" value=\"Yes\" name=\"yesdel\" /> 
    <input type=\"submit\" value=\"No\" name=\"nodel\" />";
                                 
    }    
              
              if (isset($_POST['yesdel']))
    {
      $reallydelete ="true";
       $deletebutton = "";           
             } elseif (isset($_POST['nodel'])) {
                   $reallydelete =  "false";
                   $deletebutton = "";   
            }
so when they delete on yes or no it will perform according but the problem is when they click on yes or no $adid has no value as it wasn't clicked so $clickedremove doesnt get the value of $adid so you can do a delete according to that id on sql. So how can I get around this.
The solution can be: is it possible to have the value $clickedremove to be static like it will retain it value.
Thanks

Re: How identify the button click without knowing the name

Posted: Tue Aug 12, 2008 3:27 pm
by desmi
Not completely related, but by seeing your other posts/topics, you really should learn some more basics.

I can see far too many error that will confuse you while testing you script.. eg:
<input type='submit' value='Remove listing' name='$a1[car_listing_ID]' />
you need to echo this line with "" 's or echo that name variable.

Code: Select all

 
 
#  if (isset($_POST[$adid])) //Here again you're missing ' '
#             {
#              $clickedremove = $_POST[$adid];  //And here...
#              $_POST[error] = "Are you sure you want to delete <br/> "; //And here...
#     $deletebutton = "<input type=\"submit\" value=\"Yes\" name=\"yesdel\" />
#     <input type=\"submit\" value=\"No\" name=\"nodel\" />";
 
 
I wonder how you even test your scripts.. No offence..

Re: How identify the button click without knowing the name

Posted: Tue Aug 12, 2008 3:35 pm
by ravx
desmi wrote:Not completely related, but by seeing your other posts/topics, you really should learn some more basics.

I can see far too many error that will confuse you while testing you script.. eg:
<input type='submit' value='Remove listing' name='$a1[car_listing_ID]' />
you need to echo this line with "" 's or echo that name variable.

Code: Select all

 
 
#  if (isset($_POST[$adid])) //Here again you're missing ' '
#             {
#              $clickedremove = $_POST[$adid];  //And here...
#              $_POST[error] = "Are you sure you want to delete <br/> "; //And here...
#     $deletebutton = "<input type=\"submit\" value=\"Yes\" name=\"yesdel\" />
#     <input type=\"submit\" value=\"No\" name=\"nodel\" />";
 
 
I wonder how you even test your scripts.. No offence..
Hi maybe I should have mentioned it but

<input type='submit' value='Remove listing' name='$a1[car_listing_ID]' />
I have tested it with the " " s.
was echoed. this was part of a table which was being echo, so it being printed. and regarding the $adid, i tried to put the $a1[car_listing_ID] into that variable and i must have copied the wrong code in the forum my mistake.

Re: How identify the button click without knowing the name

Posted: Tue Aug 12, 2008 5:26 pm
by jayshields
ravx wrote:I want a message saying are you sure you want to delete and a yes and no option.

Code: Select all

<form onsubmit="return confirm('Are you sure you want to do this?');">