1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

adding php to button onclick

Discussion in 'HTML & Website Design' started by DevDream, Nov 5, 2016.

  1. #1
    How do I add php to button onclick? I dont wish to use jquery as i have been using javascript and i dont wanna complicate things and jqeury is all i could generally find.

    does it basically work the same way as in javascript, you make a function using php then call the function using inclick ?

    e.g. <?php function Myfunction()
    {
    }
    ?>

    <input type = "button" name="click here" onClick="return Myfunction()";>

    sorry for my n00b question haha
     
    DevDream, Nov 5, 2016 IP
  2. hdewantara

    hdewantara Well-Known Member

    Messages:
    536
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #2
    I could think of a pattern:
    
    <input type="button" name="click here"
      onclick="location.href=myfunctions.php?run=Myfunction";>
    HTML:
    with your myfunctions.php containing:
    
    if($_GET['run'] === 'Myfunction'){
      Myfunction();
    }
    PHP:
    But what does Myfunction() do anyway?
     
    hdewantara, Nov 5, 2016 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #3
    PHP is run server-side, not client-side. You can't call PHP directly from a event. Doesn't work that way.

    @hdewantara's example would load the result as a new page, but at that point why not use an ANCHOR? I don't think that's even close to what @DevDream is actually asking.

    To use the result of an onclick for SOMETHING scripted (damned if we know what) that's PHP/server-side, you'd have to use some form of AJAX type scripting to call it.

    Though @hdewantara asked a damned important question, what is that function supposed to do, and why would you be calling a server-side function from client-side code?
     
    deathshadow, Nov 5, 2016 IP
  4. DevDream

    DevDream Member

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #4
    just echo "thanks for submitting the form, then use the post method to post the value of the button whose name is firstname.
    like echo "Thanks $_post("firstname");
    ???
     
    DevDream, Nov 5, 2016 IP
  5. DevDream

    DevDream Member

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #5
    would you mind posting the code that could do this for a button called submit where there is a input type text which name is firstName. thanks!
     
    DevDream, Nov 5, 2016 IP
  6. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #6
    There must be a million tutorials on how to submit a form using ajax/JavaScript.

    And yes, you can do it with plain JavaScript
     
    sarahk, Nov 5, 2016 IP
  7. DevDream

    DevDream Member

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #7
    No, I NEED to do it using php specifically. I was told to by my employee
     
    DevDream, Nov 5, 2016 IP
  8. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #8
    Php runs on the server, you need JavaScript to send the info to the server. Work through some tutorials and you'll get a better understanding.
     
    sarahk, Nov 5, 2016 IP
  9. DevDream

    DevDream Member

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #9
    I got my answer elsewhere, so let's leave it at that.
     
    DevDream, Nov 5, 2016 IP
  10. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #10
    Lol, so "employee" = "lecturer"
     
    sarahk, Nov 5, 2016 IP
  11. DevDream

    DevDream Member

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #11
    You get me

    :)
     
    Last edited by a moderator: Nov 6, 2016
    DevDream, Nov 5, 2016 IP
  12. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #12
    Actually, you do NOT need JavaScript to send it to the server, unless you WANT to piss off users. What you're looking for is NOT a <button> tag with some scripttardery, it's <input type="submit"> with a proper full page load.

    Which even IF you were to enhance that with JavaScript to avoid the page-load, it's generally a good idea to implement WITHOUT the scripted asshattery FIRST for the people who actively block or have scripting unavailable!

    Sounds like you're just building a <form>, so USE THE FORM PROPERLY FIRST!

    THEN if you want to enhance it with scripting do so. I have this mantra about JavaScript:
    If you can't make a fully functional page without JavaScript FIRST, you likely have no business adding scripting to it!

    Something like a login? Scripting just adds another layer of complexity to be hacked -- see my other mantra:
    The less code you use, the less there is to break.
     
    deathshadow, Nov 5, 2016 IP