views:

227

answers:

3

I want to make a button that starts my php script after I click it. So do I just make 2 separate files and have action post to the php file and then let it start? or is there a better way to do this? Possibly in one document?

Update:

Well, I basically made a script that would do a series of a loops until it's finished. So usually when I visit the page it automatically starts, so I'm making a button to start it only when I need it.

More info: Answer to one of the questions, "starting the script" as in it would only execute the script.

More info: I really don't need to pass any data from the submit form to the php script. I just want my script to run when I hit a button. I just want to know what's the best way to do this.

+1  A: 

Having 2 files like you suggested would be the easiest solution.

For instance:

2 files solution:

index.html

(.. your html ..)
<form action="script.php" method="get">
  <input type="submit" value="Run me now!">
</form>
(...)

script.php

<?php
  echo "Hello world!"; // Your code here
?>

Single file solution:

index.php

<?php
  if (!empty($_GET['act'])) {
    echo "Hello world!"; //Your code here
  } else {
?>
(.. your html ..)
<form action="index.php" method="get">
  <input type="hidden" name="act" value="run">
  <input type="submit" value="Run me now!">
</form>
<?php
  }
?>
Carlos Lima
A: 

What exactly do you mean by "starts my php script"? What kind of PHP script? One to generate an HTML response for an end-user, or one that simply performs some kind of data processing task? If you are familiar with using the tag and how it interacts with PHP, then you should only need to POST to your target PHP script using an button of type "submit". If you are not familiar with forms, take a look here.

jkndrkn
A: 

You could do it in one document if you had a conditional based on params sent over. Eg:

if (isset($_GET['secret_param'])) {
    <run script>
} else {
    <display button>
}

I think the best way though is to have two files.

Myles
err, that's not a very good way of doing it, you'd be much better off just testing $_SERVER['REQUEST_METHOD'] == 'POST';
Kris
So that anyone could initiate the script by posting to it?
Myles