4 Replies - 101942 Views - Last Post: 13 February 2008 - 11:00 AM Rate Topic: -----

#1 dontKnowJava  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 221
  • Joined: 29-September 07

How to call php functions from html code

Posted 13 February 2008 - 08:35 AM

hey,
i got a quick php question. say i have a php page that has some functions on it. how can i call any of them after the page loaded and its entirely html. i know how to do it in a form, but say i want it to run a php function by clicking a link. how would i do that?
Is This A Good Question/Topic? 0
  • +

Replies To: How to call php functions from html code

#2 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5322
  • View blog
  • Posts: 27,232
  • Joined: 10-May 07

Re: How to call php functions from html code

Posted 13 February 2008 - 08:41 AM

You can only execute php functions from a php page, inside of php code.

The reason for this is because php executes on the server side. The HTML is generated by the server & interepted by the web browser on the clients side. So by the time a visitor is viewing the HTML page, they have lost the ability to execute php code. However, since you asked about them clicking on ... something, you could post that into a <form> & point the action a php page that can execute, gather the results & perform whatever predefined functionality that you have in mind. That's primariliy the purpose of forms, is to send a client side request to a server side executable.
Was This Post Helpful? 0
  • +
  • -

#3 dontKnowJava  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 221
  • Joined: 29-September 07

Re: How to call php functions from html code

Posted 13 February 2008 - 08:55 AM

yeah that makes sense, but can a form be sent by clicking a link rather than a button? heres something more specific. say were talking about a shopping cart a user searches for a product and finds several different matching his search. next to each product theres "add to cart" link not a button. can that link send the form? and what would be my method post, get or nothing since theres really no text fields?
Was This Post Helpful? 0
  • +
  • -

#4 blux  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 07-August 07

Re: How to call php functions from html code

Posted 13 February 2008 - 10:35 AM

I don't think that you can change submit button, because the html tag is input type="submit" and that generates a button. The link that you talk is another thing. In the url link, You should see the character ? and that means something is passed to the php code located in a file somewhere.

For your method, it doesn't matter if you have text fields or not. Everything input tag in the form is sent. The method is good to know, because in the php you'll get your data through $post if the method is post and you'll get your data through $get if the method is get.

I hope that helps
Was This Post Helpful? 1

#5 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5322
  • View blog
  • Posts: 27,232
  • Joined: 10-May 07

Re: How to call php functions from html code

Posted 13 February 2008 - 11:00 AM

You actually can use just a simple <a href="page.php?action=add&ID=whatever"> & then pull those values apart using $_REQUEST. It works like $_GET, except you don't have to use a form.

http://www.w3schools...php/php_get.asp

Granted, if you are using this within a shopping cart, I don't know how dynamic you can make your HTML to know which user ID & item IDs, ect ect that you are pushing to the PHP page.
Was This Post Helpful? 1

Page 1 of 1