Trying to get name of Submit button

Discussion in 'PHP' started by lonewolff, Mar 30, 2010.

  1. #1
    Hi there!

    I am currently having troubles sending the name of a submit button to another page.

    The code for the button is as follows;

    <form method='post' action='edit.php'><input type='submit' name='ValueIamTryingToGet' style='width:48px' value='Edit'/></form>

    And the request from the edit.php field is

    $value=$_REQUEST['name'];
    echo"Value: $value<br/><br/>";


    But, the value is always blank on the paged 'edit.php'.

    Could anyone please advise on how I may be going wrong?

    Thanks in advance! :)
     
    lonewolff, Mar 30, 2010 IP
  2. Nyu

    Nyu Peon

    Messages:
    79
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The name field is used to identify the field. The value is stores in the value attribute. The right code would be:
    <form method='post' action='edit.php'><input type='submit' name='somename' style='width:48px' value='Edit'/></form>
    HTML:
    
    $value=$_REQUEST['somename'];
    echo"Value: $value<br/><br/>";
    
    PHP:
    This should output the value 'Edit' from your input field identified by the name 'somename'
     
    Nyu, Mar 30, 2010 IP
  3. stOK

    stOK Active Member

    Messages:
    114
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #3
    
    $value=$_REQUEST['[COLOR="Red"]ValueIamTryingToGet[/COLOR]'];
    echo"Value: $value<br/><br/>";
    
    Code (markup):
     
    stOK, Mar 30, 2010 IP
  4. lonewolff

    lonewolff Member

    Messages:
    338
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    30
    #4
    Thanks for the replies. But this is sort of the reverse of what I want to achieve.

    I was planning to have an ID number that could be of any value attached to a submit button. So, the 'name' variable would be $id - which is derived from a MySQL database. $id could be anything depending on the query and there would be a table of submit buttons.

    So, I cant really do...

    $value=$_REQUEST['ValueIamTryingToGet'];
    echo"Value: $value<br/><br/>";


    as ValueIamAmTryingToGet could be anything.

    Hopefully what I am trying to do makes sense.

    Thanks again :)
     
    lonewolff, Mar 30, 2010 IP
  5. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #5
    You do it like this

    
    $value = array_search('Edit',$_POST);
    
    PHP:
     
    stephan2307, Mar 30, 2010 IP
  6. lonewolff

    lonewolff Member

    Messages:
    338
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    30
    #6
    I dont see how 'array search' would work in this case as I dont have an array to search from.

    I am trying to get a virtually random numer that is assigned to a submit button.
     
    lonewolff, Mar 30, 2010 IP
  7. lonewolff

    lonewolff Member

    Messages:
    338
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    30
    #7
    What if I want the contents and the contents contain a random value. How would I go about getting the value of the button.

    I'll give you some more background as to what I am trying to do.

    I have a MySQL database that is full of 'listings'. Each listing has an auto-increment ID column.

    I want to be able to view all current listings and have an edit or delete button next to each listing and then send the value of the ID through a submit button over to another page and act accordingly to the button that was pressed.

    Something like

    DELETE FROM Listings WHERE id=$submit_button_value

    Thanks again!
     
    lonewolff, Mar 30, 2010 IP
  8. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #8
    btw - dear god, sanitize your inputs
     
    krsix, Mar 30, 2010 IP
  9. Narrator

    Narrator Active Member

    Messages:
    392
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    80
    #9
    $_POST is the array you search, if you print_r($_POST) you get:
    
    Array (
     [ValueIamTryingToGet] => Edit
    ) 
    
    Code (markup):
    so by using
    $value = array_search('Edit',$_POST);
    PHP:
    you get the name of the field
     
    Narrator, Mar 30, 2010 IP
  10. lonewolff

    lonewolff Member

    Messages:
    338
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    30
    #10
    How do you mean?

    If I am going about it the wrong way, I am more than open to suggestions.
     
    lonewolff, Mar 30, 2010 IP
  11. lonewolff

    lonewolff Member

    Messages:
    338
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    30
    #11
    @Narrator

    Actually, I have tried what you explained and it has worked exactly as I wanted.

    Thanks heaps for everyones help! :)
     
    lonewolff, Mar 30, 2010 IP
  12. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #12
    Well thank me that is :p

    In the future I would try the suggestions from people before saying it doesn't work. Because you never know you could learn something new one day.
     
    stephan2307, Mar 30, 2010 IP
  13. lonewolff

    lonewolff Member

    Messages:
    338
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    30
    #13
    I dont believe I ever said that something 'didn't work'.

    I will say that I didn't fully understand the answer though, until I read deeper and put it into practice.

    Thanks again though. :cool:
     
    lonewolff, Mar 31, 2010 IP
  14. shivam0000

    shivam0000 Member

    Messages:
    178
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    33
    #14
    is this worked ??
     
    shivam0000, Mar 31, 2010 IP
  15. lonewolff

    lonewolff Member

    Messages:
    338
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    30
    #15
    Yes, it did thanks :)
     
    lonewolff, Mar 31, 2010 IP