Solved

Looking for a PHP Button who calls a function

Posted on 2009-04-14
4
419 Views
Last Modified: 2013-12-12
Hi, i do have seperated HTML and PHP Content.

With the following php code

<?php
$fk ->jump(newpage.html)
?>

i can jump from page to page. But i would like to have a button and if it is click, jump is call.

I tried, but failed. Any Ideas?
0
Comment
Question by:easterbunny09
  • 2
4 Comments
 
LVL 2

Expert Comment

by:Shodan82
ID: 24136379

<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
    <input type="button" name="submit" value="Submit">
</form>
 
// at top of the page put this:
 
<?php
// if the button is clicked
if(isset($_POST['submit']))
{
    // run your php function
    foo();
}
?>

Open in new window

0
 
LVL 6

Accepted Solution

by:
den4b earned 500 total points
ID: 24136385
There are 2 ways of doing this:
PHP + JavaScript:
Use PHP to print out JavaScript code that will redirect browser to another page, e.g. <script>window.location = 'http://your-new-url'</script> 
PHP + HTTP Headers:
Use PHP's header() function to send HTTP headers of a redirect, e.g. header('Location: http://your-new-url'). For this to work you would need to buffer the output, because header() function cannot be called after you started outputting from your PHP page.
Tell me which way you want to do it and I can give you a full example.
0
 
LVL 6

Expert Comment

by:den4b
ID: 24156131
I actually thought that within PHP code you needed functionality to break the execution of the code and redirect to another page. Now that I read it again, it seems that you needed something else. A little mistake on my side.

If you want a PHP function that will print an HTML button that will call another page once clicked, then you can use this code for your jump() function:


<?php
 
function make_jump_button($url, $title)
{
  $url = htmlspecialchars($url, ENT_QUOTES);
  $title = htmlspecialchars($title, ENT_QUOTES);
  echo "<button style=\"cursor: pointer\" onclick=\"window.location='{$url}'\">{$title}</button>";
}
 
make_jump_button('http://www.example.com/', 'test link');
 
?>

Open in new window

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
comma true 6 37
How is this connection happening? 3 20
PHP 5.6 and 7.x 4 20
Reference key in foreach loop 4 16
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question