SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Zealot samohtwerdna's Avatar
    Join Date
    Jul 2007
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    get $_POST[Submit] from <a> instead of <button>?

    hello all,

    not sure if this is the best place to ask this - but since my page is written in php I thought it would be fine.

    I want to replace some buttons with a styled list. In most cases the buttons are just links anyway - but I do have a "save" button that has a name="Submit" and an id="Submit" and a value="Save" then I post that to this code:
    PHP Code:
    # FOR SAVE BUTTON
    if ($_POST[Submit] && $_POST[Submit] == 'Save') {
        
    # Check for required fields.
        
    $Required = array("Code","Price Code","Name","Brand Cat Id","Descr Short");
        foreach (
    $Required as $Field) {... 
    How can I use an anchor tag in its place (especially since <a> does not support value=)??

    Thanks for the help

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Use some javascript to submit the form onClick:

    Code:
    <a href="javascript:document.formName.submit();">Submit</a>
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Zealot samohtwerdna's Avatar
    Join Date
    Jul 2007
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks spike,

    I am not sure why but this is not going through my save routine. Perhaps because of my condition
    PHP Code:
    if ($_POST[Submit] && $_POST[Submit] == 'Save') { 
    or am I supposed to pass somthing through the submit() ??
    HTML Code:
    <li><a href="javascript:document.form1.submit();">Save</a></li>

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Yes it is beause of the if().
    In your form put a hidden field called formsubmit. then use that to check for your submission

    Code:
    <input type="hidden" name="formsubmit" value="1" />
    PHP Code:
    if(isset($_POST['formsubmit'])) {
    //


    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    SitePoint Zealot samohtwerdna's Avatar
    Join Date
    Jul 2007
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot spikeZ!

    problem solved.

  6. #6
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Submitting forms like this is a bit nasty. Especially putting "javascript:" in the href attribute of the link. You might be better off using an image. Then it works without javascript.
    HTML Code:
    <input type="image" name="submit" alt="Submit" src="submit.png">

  7. #7
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    I agree Raffles it is a bit of a hack but for text link based submission it is a valid way.
    A 'nicer' alternative would be:
    Code:
    <li><a href="#" onclick="document.form1.submit();">Save</a></li>
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  8. #8
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Barbados
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As Raffles said, I think it'd be best to have all the javascript in its own exclusive .js file in order to improve maintainability.

    Using the 'onclick' attribute of the <a> tag is basically the same thing as your previous suggestion.

    However, either one works so it's really up to the programmer.

  9. #9
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I think using the onclick attribute is slightly better than having "javascript:" in the href attribute, because "javascript" is not a real HTTP protocol. Of course the best way is to put all the JS into a separate file, but for some reason that seems to be an enormous leap for some people simply because they're used to doing it the inline way. I find it's much much nicer to have it externally, even if it's a trivial thing.

  10. #10
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Barbados
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think anyone realises it when they realise they're retyping (or copy-and-pasting) the same javascript over and over again on each page they want to use instead of just including it as an external file. It's just easier to maintain in the long run.

  11. #11
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    True, but I bet there are still loads of people who still think putting inline event handlers in HTML is the only way to connect the HTML page to the external JS file. Just like there are people who still think tables are the only real way to lay out a web page.

  12. #12
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,322
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    what about changing the php code from POST to using GET and use something on the end of the link in the HTML?
    e.g. <a href=".....php?save=1">Save</a> or something.

    the idea of relying on javascript to be turned on in order to make use of page is bad news imo

  13. #13
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    it depends on what is being sent through the form, if it a 1000 word textarea using GET may not work also you would still need to submit the form from the link.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  14. #14
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,322
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    oh yes, i wasn't fully thinking straight there. thanks.

  15. #15
    SitePoint Zealot samohtwerdna's Avatar
    Join Date
    Jul 2007
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure about the benefits of the external js script in relation to the <a> - though it sounds interesting.

    I did throw in a <noscript><button.. for those without js - won't that cover my bases??

    Also, is there a security issue with using js in an <a>? - or just preference?

  16. #16
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using Javascript either breaks the behaviour for some, or provides duplicate interfaces with the noscript tag.
    It also messes with the semantics of the anchor tag.

    I'd style the button to look like a link.
    A quick example:
    Code:
    <input type="submit" name="saveButton" value="Save" 
    style="border: 0; margin: 0; padding: 0; background: transparent;  text-decoration: underline; color: #C50; cursor: pointer; ">
    move the styling to your style sheet in practice.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •