Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A button to start php script, how?

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.

like image 444
Strawberry Avatar asked Nov 08 '09 18:11

Strawberry


3 Answers

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
  }
?>
like image 153
Carlos Lima Avatar answered Oct 09 '22 09:10

Carlos Lima


I know this question is 5 years old, but for anybody wondering how to do this without re-rendering the main page. This solution uses the dart editor/scripting language.

You could have an <object> tag that contains a data attribute. Make the <object> 1px by 1px and then use something like dart to dynamically change the <object>'s data attribute which re-renders the data in the 1px by 1px object.

HTML Script:

<object id="external_source" type="text/html" data="" width="1px" height="1px">
</object>

<button id="button1" type="button">Start Script</button>

<script async type="application/dart" src="dartScript.dart"></script>
<script async src="packages/browser/dart.js"></script>

someScript.php:

<?php
echo 'hello world';
?>

dartScript.dart:

import 'dart:html';

InputElement button1;
ObjectElement externalSource;

void main() {
    button1 = querySelector('#button1')
        ..onClick.listen(runExternalSource);

    externalSource = querySelector('#external_source');
}

void runExternalSource(Event e) {
    externalSource.setAttribute('data', 'someScript.php');
}

So long as you aren't posting any information and you are just looking to run a script, this should work just fine.

Just build the dart script using "pub Build(generate JS)" and then upload the package onto your server.

like image 31
RattleyCooper Avatar answered Oct 09 '22 07:10

RattleyCooper


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.

like image 23
Myles Avatar answered Oct 09 '22 09:10

Myles