PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Sorry if this is a stupid question, I am new to PHP.
I am trying to use a form to query a database using a function. When I load the PHP page it seems to call my function (even though I do not want it to). Then when I press the form button it seems to call the function but the results from the query are those of when I initially loaded the page. Can anyone tell me why this is happening or perhaps give me an alternative? Here is my code:
This is because PHP runs on the server before the page gets to the browser, so all of your code will get executed irregardless of any javascript stuff.
Think of PHP as a program than runs and then spits out HTML, this HTML is then sent to your browser. Load that page up that you created and look at the source. Notice that its just HTML and all of your PHP is gone.
Thanks Begby, so is there another way to do this to dynamically get query results based on when someone presses a button? I would really prefer not to have to reload the page but rather dynamically update just the query results (say in a grid). Is DHTML a good way to do this?
citytours wrote:Thanks Begby, so is there another way to do this to dynamically get query results based on when someone presses a button? I would really prefer not to have to reload the page but rather dynamically update just the query results (say in a grid). Is DHTML a good way to do this?
You are going to need to use ajax to do this. I suggest using a javascript library such as prototype to do it. Your script will have two parts, the HTML page with the javascript, and a PHP page that does the query and returns the results. The ajax part will be a function that remotely calls the PHP page when you click a button, gets the results, then displays them on your page in a div tag or something.
Before you delve into this, I strongly suggest becoming a little more versed in PHP/HTML/Scripting so that you understand how client/server scripting works and also understand a bit more about php syntax.