Print output result on same page - php

<?php
class myDemo
{
function __construct()
{
return strip_tags(file_get_contents("http://example.com/bitcoin/checker.php?method=gLink&secret=xxxxxx&user_id=".$_SESSION['user_id']));
}
}
$o = new myDemo;
$data = $o->__construct();
$data = json_decode($data);
echo $data->random; ?>
this script show to user a bitcoin address for submit payment . i want the code that make a button and when user click on the button he get bitcoin address on same page below the button . i mean user get the output of this script on same page after click on the button .

You may implement an AJAX request to do that.
HTML
<input type="submit" id="getBitCoinAddr" value="Get Address">
<div id="BitCoinAddr"></div>
jQuery
$("getBitCoinAddr").click(function(){
$("#BitCoinAddr").load("BitCoinAddr.php?getResult=1");
});
PHP
if($_GET['getResult==1'])
{
return $data->random;
}
However, it's better to separate side effects and declarations (PSR-1).

Related

How do I use PHP to detect a button click and open an new page?

I was wondering if someone can help me. I am trying to write some PHP code so that when I click on a button in one page, upload.php, the button click is detected and I am redirected to another page, processing.php. I am following along another tutorial and I have triple checked, I don't see what I have done wrong, but the button click is not being detected as it is in the video.
This is the code for my upload.php file:
<?php include_once('includes/header.php');?>
<?php include_once('includes/classes/VideoDetailsFormProvider.php');?>
<div class="column">
<!-- //calling PHP function to create upload form -->
<?php
//create variable and assign value
$formProvier = new VideoDetailsFormProvider($con);
//call function
echo $formProvier->createUploadForm();
?>
</div>
<?php include_once('includes/footer.php');?>
This is the relevant code from my additional class VideoDetailsFormProvider.php:
class VideoDetailsFormProvider{
private $con;
//create constructor and pass $con variable to it
public function __construct($con){
$this->con = $con;
}
//creating a function to create the upload form
public function createUploadForm(){
$fileInput = $this->createFileInput();
$titleInput = $this->createTitleInput();
$descriptionInput = $this->createDescriptionInput();
$privacyInput = $this->createPrivacyInput();
$categoryInput = $this->createCategoryInput();
$uploadButton = $this->createUploadButton();
return "
<form action='processing.php' method='POST'>
$fileInput
$titleInput
$descriptionInput
$privacyInput
$categoryInput
$uploadButton
</form>
";
}
private function createUploadButton(){
$html = "<button name='uploadButton' class='btn btn-primary'>Upload Video</button>";
return $html;
}
And this is what I have in my processing.php file:
<?php include_once('includes/header.php');
//check for submission of the form or button is pressed
if(!isset($_POST['uploadButton'])){
echo "No form data has been set";
}else{
}
?>
When I click the button object, nothing happens. In the video I am transferred to processing.php and the echo message is not displayed. Or at least I should be, but that doesn't happen. I did try to check here to see if I could find some answers, a few things I tried didn't work out. Does anyone have any ideas about something I might be missing? Thanks in advance
The problem is with your upload button, when you use an html form it has to be submitted to the php processing page, try:-
private function createUploadButton(){
$html = "<button type='submit' name='uploadButton' class='btn btn-primary'>Upload Video</button>";
return $html;
}

Why isnt my PHP function firing from another file?

I have this input element in index.php
<input type="submit" name="OUs" value="Get all OU's">
This is where I fire the function in a PHP codeblock in index.php:
require_once('./OU.php');
$ou = new \Google\OU\OU();
if(isset($_POST['OUs'])) {
echo $ou->getOUs($response['access_token']);
}
I try to run a function in ou.php
namespace Google\OU;
define("APIURL_DIRECTORY","https://www.googleapis.com/admin/directory/v1/customer/"); // For Google
Directory actions
class OU
{
// Get OU's
function getOUs($token){
$url = "https://www.googleapis.com/admin/directory/v1/customer/my_customer/orgunits?orgUnitPath=/&type=all";
$method = "GET";
echo exeCurl($url,$method);
}
What is the reason that nothing happens when I click on that submit button? It should execute the Curl Call, shouldnt it?
I cant really figure it out. Thanks in advance.

How do I trigger/capture an event from my PHP generate HTML Code?

My class...
<?php
class SelectionBoxbuilder
{
public function RenderToHTML()
{
$SelectBox = '<select>
<option>"One"</option>
<option>"Two"</option>
</select>';
return $SelectBox;
}
}
My PHP test file that generates the selection box...
<?php
foreach (glob("classes/*.php") as $filename)
{
include $filename;
}
$sb = new SelectionBoxbuilder();
echo $sb->RenderToHTML();
This Works, but I need to know how to capture the event from the Selection box I have generated, not sure if I am in fact approaching this from the wrong angle perhaps I need to do this in an entirely different manner?
I basically want the event to trigger some other PHP code I have not written yet and pass the value of the selection box as a parameter.
Hope this I clear enough if not please let me know any additional information I could add.
So...
When the user changes the item selected on the selection box I would like this to trigger an event, which I will then 'point' to some other PHP code.
I am assuming that you are talking about change events on the select box (i.e. when the user selects a different value).
The main thing you need to understand is that the event is fired on the client side in the browser, while your PHP code is running on the server. It has no way of knowing what's happening on the client.
If you need to do something in PHP when these events are fired, you can add JavaScript code to handle the client-side event and to fire an AJAX request to your server-side PHP script.
In basicly PHP is a server side language.
So after the server side send output to browser you can't use PHP anymore.
To get the selection from user you need to make a new request to server.
You can do it by two methods:
Send the form data to server by regular http request (GET/POST).
Send data with ajax request for UX reasons.
First you need to change your "SelectionBoxbuilder" class to form element.
Like this:
<?php
class SelectionBoxbuilder{
public function RenderToHTML(){
$SelectBox = '
<form method="post">
<select name="selectbox">
<option value="1">"One"</option>
<option value="2">"Two"</option>
</select>
<br/>
<input type="submit" value="Send" />
</form>
';
return $SelectBox;
}
}
Now you have a form in you HTML output.
To get the response we need to add handler for the post request to the test file:
<?php
foreach (glob("classes/*.php") as $filename){
include $filename;
}
$sb = new SelectionBoxbuilder();
// If form submited
if( isset( $_POST['selectbox'] ) ){
echo 'Your selection is: ' . $_POST['selectbox'];
}
echo $sb->RenderToHTML();
You can add new method to your class to take care of the response:
<?php
class SelectionBoxbuilder{
public function RenderToHTML(){
$SelectBox = '
<form method="post">
<select name="selectbox">
<option value="1">"One"</option>
<option value="2">"Two"</option>
</select>
<br/>
<input type="submit" value="Send" />
</form>
';
return $SelectBox;
}
//Check response and return the value if form already submit
public function checkForResponse(){
if( isset( $_POST['selectbox'] ) ){
return 'Your selection is: ' . $_POST['selectbox'];
}
// return false if form not send
return false;
}
}
And now in test file you can do somthing like this:
<?php
foreach (glob("classes/*.php") as $filename){
include $filename;
}
$sb = new SelectionBoxbuilder();
// If form submited
if( $userAnswer = $sb -> checkForResponse() ){
echo 'Your selection is: ' . $userAnswer;
} else {
echo $sb->RenderToHTML();
}
I hope I was helpful to you.

submit pressed then loaded function

login.php
<?php
class airsystem{
public function login(){
echo "check login";
}
}
$airsystem = new airsystem;
?>
index.php
<?php
require_once ("login.php");
if(isset($_POST['submit'])){
$airsystem->login();
}
?>
<input type="submit" name="submit">
my intention is to make the login system and the function only runs when i clicked submit, it mean "check login" only show if i click submit button. but the echo always there even i am not pressing the submit.
so mean it auto load the function , how i going to achieve that ??
If I recall correctly isset on a $_POST will cause you problems as it can be set but empty. Try using this which will ensure submit is set and populated:
<?php
if( !empty($_POST['submit']) ){
require_once ("login.php");
$airsystem->login();
}
?>
Additionally it is worth only including the login script if it is required (the user has pressed the button). (As suggested by #rineez)

Can't submit Typo3 forms when not logged in

I have made a simple plugin with a form but it won't post when I'm not logged in.
Here is the file class.tx_gctest_pi1.php, created with Kickstarter.
require_once(PATH_tslib.'class.tslib_pibase.php');
class tx_gctest_pi1 extends tslib_pibase {
var $prefixId = 'tx_gctest_pi1'; // Same as class name
var $scriptRelPath = 'pi1/class.tx_gctest_pi1.php'; // Path to this script relative to the extension dir.
var $extKey = 'gc_test'; // The extension key.
var $pi_checkCHash = true;
function main($content, $conf) {
$this->conf = $conf;
$this->pi_setPiVarDefaults();
$this->pi_loadLL();
if($_POST) {
echo 'test';
}
$content='
<strong>This is a few paragraphs:</strong><br />
<p>This is line 1</p>
<p>This is line 2</p>
<h3>This is a form:</h3>
<form action="'.$this->pi_getPageLink($GLOBALS['TSFE']->id).'" method="POST">
<input type="text" name="'.$this->prefixId.'[input_field]" value="'.htmlspecialchars($this->piVars['input_field']).'">
<input type="submit" name="'.$this->prefixId.'[submit_button]" value="'.htmlspecialchars($this->pi_getLL('submit_button_label')).'">
</form>
<br />
<p>You can click here to '.$this->pi_linkToPage('get to this page again',$GLOBALS['TSFE']->id).'</p>
';
return $this->pi_wrapInBaseClass($content);
}
}
if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/gc_test/pi1/class.tx_gctest_pi1.php']) {
include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/gc_test/pi1/class.tx_gctest_pi1.php']);
}
?>
This will output test when logged in and nothing when not logged in.
The page is reloaded but no post is sent
I think this has not much to do with logged in / logged out. TYPO3 caches content unless you tell it to not cache.
echo var_dump print_r debug are methods that directly display things through php. TYPO3 doesn't catch them. If you want to have something displayed, add it to e.g. $content and return $content at the end of main(). The return value of main() gets cached.
You can try this by clearing your cache in backend and refresh the page. echo etc is displayed. after a new reload, it is gone.
So, how to solve this? There are a few possibilities
make the page that contains your plugin uncached
make the plugin itself uncached
I would suggest you find out what you really want to do and then write your code that is is using caching.

Categories