Jump to content

How do I use Buttons?


Pete C

Recommended Posts

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>";

  
?>

 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 ;)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.