Solved

Looking for a PHP Button who calls a function

Posted on 2009-04-14
4
410 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now