Pete C Posted May 2, 2009 Share Posted May 2, 2009 Have patience - I'm totally new to PHP. As part of my self-learning I want to know how to use buttons to fire events (click event in this case). My question: I have created a PHP script using MS Expression and the script successfully opens an MSSQL database and displays records from a table in that database. I would now like to place the code that achieves the above in a function (I think I'm ok with doing that) and I also want to put a button on my PHP page which calls that function. I have tried to place an Expression button on the php page but get an error when I run it (I guess I'm mixing html and php). Any help greatly appreciated. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 2, 2009 Share Posted May 2, 2009 You can't put a HTML button to call a PHP function. I guess you can have that button submit a form and have a PHP script call the function. Quote Link to comment Share on other sites More sharing options...
Pete C Posted May 2, 2009 Author Share Posted May 2, 2009 Thanks Ken, some explanation of how to submit a form from a button and have a PHP script call the function would be most appreciated, or perhaps a link which offers such an explanation. Thanks Pete C Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 2, 2009 Share Posted May 2, 2009 html <form method="post" action="do.php"> <input type="submit" name="submit" value="submit" /> </form> do.php <?php if (isset($_POST['submit'])) { // do something } Quote Link to comment Share on other sites More sharing options...
Pete C Posted May 2, 2009 Author Share Posted May 2, 2009 Thanks Ken. I have implemented as below but upon running default.html I get a 'downloading' dialog briefly and then a blank page .. This is the php page: <?php function firstfunction() { $myServer = "ACE-MACHINE"; $myUser = "pete"; $myPass = "pete"; $myDB = "assignment_tracking"; $result = ""; $dbhandle = ""; $selected = ""; $query = ""; $numRows = ""; $row = ""; //connection to the database $dbhandle = mssql_connect($myServer, $myUser, $myPass) or die("Couldn't connect to SQL Server on $myServer"); //select a database to work with $selected = mssql_select_db($myDB, $dbhandle) or die("Couldn't open database $myDB"); //declare the SQL statement that will query the database $query = "SELECT tutorid, tutorname, salary "; $query .= "FROM dbo.tutor "; //execute the SQL query and return records $result = mssql_query($query); $numRows = mssql_num_rows($result); echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>"; //display the results while($row = mssql_fetch_array($result)) echo "<li>" . $row["tutorid"] . " " . $row["tutorname"] . " " .$row["salary"] . "</li>"; //close the connection mssql_close($dbhandle); } if (isset($_POST['submit'])) firstfunction(); ?> and this is the default.html page <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>button</title> </head> <body> <form method="post" action="php2.php"> <input type="submit" name="submit" value="submit" /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 2, 2009 Share Posted May 2, 2009 Please learn to use tags. Maybe something's wrong with the function? I'm not familiar with mssql. Can you tell if the query failed or not? Can you put an or die after the query? Quote Link to comment Share on other sites More sharing options...
Pete C Posted May 3, 2009 Author Share Posted May 3, 2009 Thanks for the reply. The code within the function is fine. If the code within the function is pasted into a php page and run then it gets the results and displays them just as it should. However, as per previous posting if I put the code into a function and then use a html page to submit a form - it don't work. I just get a 'download' dialog box after I click the button. Code tags? OK I'll learn but what are they? Thanks for your help. Pete C Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 3, 2009 Share Posted May 3, 2009 The code tags are used for putting your codes in. When you post codes, wrap them in the code tags. Example: <?php if ($something) { // do something } Got it? When you put it in a function, did you call that function? Say the function is named "do ()", then you'll have to call do() in your php file. Functions don't execute themselves. Quote Link to comment Share on other sites More sharing options...
Pete C Posted May 3, 2009 Author Share Posted May 3, 2009 Thanks, mastered the code tag thing and code is shown below. Second lot of code is in a html page called default.html This is php2.php <?php function firstfunction() { $myServer = "ACE-MACHINE"; $myUser = "pete"; $myPass = "pete"; $myDB = "assignment_tracking"; $result = ""; $dbhandle = ""; $selected = ""; $query = ""; $numRows = ""; $row = ""; //connection to the database $dbhandle = mssql_connect($myServer, $myUser, $myPass) or die("Couldn't connect to SQL Server on $myServer"); //select a database to work with $selected = mssql_select_db($myDB, $dbhandle) or die("Couldn't open database $myDB"); //declare the SQL statement that will query the database $query = "SELECT tutorid, tutorname, salary "; $query .= "FROM dbo.tutor "; //execute the SQL query and return records $result = mssql_query($query); $numRows = mssql_num_rows($result); echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>"; //display the results while($row = mssql_fetch_array($result)) echo "<li>" . $row["tutorid"] . " " . $row["tutorname"] . " " .$row["salary"] . "</li>"; //close the connection mssql_close($dbhandle); } if (isset($_POST['submit'])) firstfunction(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>button</title> </head> <body> <form method="post" action="php2.php"> <input type="submit" name="submit" value="submit" /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
the182guy Posted May 3, 2009 Share Posted May 3, 2009 There is no need to use a form if you only want a button which doesn't need to send any data. Simply use: <input type="button" value="Go" onclick="window.location = 'somepage.php'" /> Can you post a screenshot of this download dialog you see? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 3, 2009 Share Posted May 3, 2009 So you want to put them in one file? If so, change default.html to default.php. PHP only runs on PHP extensions. Then put the PHP code on the top of all the HTML. Then you have one file. Is that what you want? Quote Link to comment Share on other sites More sharing options...
Pete C Posted May 3, 2009 Author Share Posted May 3, 2009 Thanks for that 182. I will try to catch and screenshot the dialog but it is visible for less than a second but enough time to see the standard thermometer type progress bar. I've now created a much more simple function which I will try to call with your code suggestion and post the results here. Ken, I tried the one file approach and MS Expression did not like it. However I now have more information and I will go away and try these suggestions. Thanks both. Pete C Quote Link to comment Share on other sites More sharing options...
Axeia Posted May 3, 2009 Share Posted May 3, 2009 Baaad practice relying on javascript to do something. If you want a link, use a link, if you want the link to look like a button, use css. W3schools got a nice forms tutorial Quote Link to comment Share on other sites More sharing options...
the182guy Posted May 3, 2009 Share Posted May 3, 2009 Axeia, it is not bad practice to rely on javascript. Look at ASP.NET, the entire idea is built around javascript. Yes, if you want to just link to a page then use a link, but sometimes a button just looks better. You can't make an element look the same as an <input type="button /> with CSS because buttons are styled by the operating system unless you override the styling. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 3, 2009 Share Posted May 3, 2009 Axeia, it is not bad practice to rely on javascript. Look at ASP.NET, the entire idea is built around javascript. Yes, if you want to just link to a page then use a link, but sometimes a button just looks better. You can't make an element look the same as an <input type="button /> with CSS because buttons are styled by the operating system unless you override the styling. It is if the user has JavaScript disabled. Quote Link to comment Share on other sites More sharing options...
Pete C Posted May 3, 2009 Author Share Posted May 3, 2009 OK, well, I can't get a screen shot of the download dialogue - it happens in a flash. I've made up 2 new files in MS Expression. The first shown below is called first.html, the second shown below iscalled second.php and the result is the same. When I run first I get 'the gold bar' at the top of the browser asking if I want to run the active X control blah, blah, and I say yes, click the go button and get a balnk screen with the MS Expression tab blinking on the task bar. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>Untitled 1</title> </head> <body> <input type="button" value="Go" onclick="window.location = 'second.php'" /> </body> </html> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <?php echo "<table border='1' bordercolor= '#FF0000' style= 'border: thick outset #008080'> <tr> <th>Firstname</th> <th>Lastname</th> </tr> <tr> <td>Ace</td> <td>Rimmer</td> </tr>"; ?> Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 3, 2009 Share Posted May 3, 2009 Do you have this up on a live site? Quote Link to comment Share on other sites More sharing options...
Pete C Posted May 3, 2009 Author Share Posted May 3, 2009 No Ken, sorry. Quote Link to comment Share on other sites More sharing options...
Pete C Posted May 3, 2009 Author Share Posted May 3, 2009 The daft thing is that if (in MS Expression) I select second.php and then click the run button from the MS Exprssion toolbar then second.php runs just fine and shows me the table. Thanks for the help all. Quote Link to comment Share on other sites More sharing options...
the182guy Posted May 3, 2009 Share Posted May 3, 2009 Ken, avoiding javascript simply because there is a very tiny perecentage of users that have it disabled or non-functional in my opinion, is like avoidng walking down the street incase you get hit by a car. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 3, 2009 Share Posted May 3, 2009 Ken, avoiding javascript simply because there is a very tiny perecentage of users that have it disabled or non-functional in my opinion, is like avoidng walking down the street incase you get hit by a car. The problem is not so much as the JavaScript not going to run, but the functionality will be removed! You should at least have the functionality working whether JavaScript is enabled or not. Just because a tiny percentage of users have JavaScript turned off (like me), that doesn't mean you should ignore them. Quote Link to comment Share on other sites More sharing options...
Axeia Posted May 3, 2009 Share Posted May 3, 2009 Providing a functional site without javascript is like using proper alt text for images [*]Your search engine rating will go up [*]Users that have them disabled get something useful And not everyone turns off javascript by choice! Theres people (due to disability) using browsers that don't support it, and terminal geeks with lynx Quote Link to comment Share on other sites More sharing options...
the182guy Posted May 3, 2009 Share Posted May 3, 2009 The thread title is how to use buttons so I have given an alternative to your form for every button approach. Buttons on the user interface that do not send data are commonly found in secured administrator areas of a websites which usually only a handfull of users would have access to. With regards to functionality, yes the functionality would be removed, you can spend time catering for those users. However, if I were to turn off images on my web browser, or CSS, I would not expect web pages appear in the same way as before. Alexia, I don't understand your comparison of javascript and image ALT tags? Are you suggesting a website without javascript is like an image without an ALT tag. And as for your comment on SEO... good javascript has no bearing on SEO, infact Google encourages javascript with its array of javascript reliant products. Lastly your search engine ratings will not necessarily increase with the addition of image ALT tags, although they should always be used. Anyway lets not going to discuss this any further in this thread which is about buttons. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 3, 2009 Share Posted May 3, 2009 You're right the182guy. You have offered an alternative suggestion. I'm sorry for being rash about it. I was just pointing it out. Images without an alt attribute is *NOT* xHTML-compliant. Quote Link to comment Share on other sites More sharing options...
Pete C Posted May 3, 2009 Author Share Posted May 3, 2009 Perhaps I can simplify my button problem by asking this: Can I create a php page/script such that when it is run it displays a button, when the button is pressed a function executes and displays 'hello World'. That would give me a starting point from which I could use the additional info you have all kindly posted. Pete C Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.