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.
Related
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;
}
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.
I have created a custom component with form to update prices of four product to be displayed on frontend.
My main controller code is here:
public function display($cachable = false, $urlparams = false) {
require_once JPATH_COMPONENT.'/helpers/calculator.php';
$view = JFactory::getApplication()->input->getCmd('view', 'pricetable');
$layout = JFactory::getApplication()->input->getCmd('layout', 'edit');
JFactory::getApplication()->input->set( 'layout', $layout );
JFactory::getApplication()->input->set('view', $view);
JFactory::getApplication()->input->set('id', 1);
parent::display($cachable, $urlparams);
return $this;
}
id is set to 1 so it loads only first row from database.
code for pricetable container is:
function __construct() {
$this->view_list = 'pricetable';
parent::__construct();
}
Now in admin backend the form is loaded as desired with the first row of data.
When I try to save the form it is redirected to administrator/index.php?option=com_calculator&view=pricetable and error is:
Error: You are not permitted to use that link to directly access that
page (#1).
my form action is:
<?php echo JRoute::_('index.php?option=com_calculator&task=pricetable.edit&id='.(int) $this->item->id); ?>
Please suggest where I am doing wrong. It is third day I'm scratching my head. :(
You can do updating actions (or calling them) inside your code whenever it is.
New instance or update new - just add one more if in code and hidden input on form. For example:
<input type="hidden" name="task" value="update" />
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.
I am trying to get a HTML/PHP form to submit properly. Some details:
base url = http://localhost/directory
page = page/add
complete address = http://localhost/directory/page/add
Using htaccess to rewrite urls so http://localhost/directory/page/add is actually http://localhost/directory/index.php?q=page/add
My HTML POST action is "page/add" so that the front controller knows which function to fire to sanitize and submit the data (it acts as a 'form id').
The page loads fine at http://localhost/directory/page/add but when I click on the submit button, the URL gets mangled to page/page/add. And every time I press "submit" I get another "page" added to the url. So 5 clicks will get "page/page/page/page/page/page/add"
I can't seem to find why I am getting that "extra" "page".
The actual PHP error (page/page/add doesn't exist in $routes since it isn't a valid route):
Notice: Undefined index: page/page/add in C:\xampp\htdocs\script\includes\common.inc on line 92
Here is the function at line 92:
function route_path($path = NULL) {
$routes = get_routes(); //Returns array: approved "urls => function callbacks"
if($path === NULL) {
$path = get_path(); //Returns $_GET['q'] with trim and strip_tags
}
$function = $routes[$path]; <<<<<----This is LINE 92
if(isset($function)) {
$form_name = str_replace('/', '_', $path); // page/add = function page_add()
}
if(function_exists($function)) {
call_user_func($function, $form_name);
}
else {
//TODO: Redirect to Login screen.
}
}
The basic HTML is:
<form action="page/add" method="post" />
//Form elements
<input type="submit" value="Submit" />
</form>
Thanks for the help.
UPDATE: What I did was add the <base> tag to my HTML templates. This allows me to keep the action as page/add (since it is also a route in my simple router/dispatcher).
By using a relative path, you're telling the form to submit at the existing path plus your action. So if you are at http://example.com/page/add, the form uses http://example.com/page/ as a base and adds the action page/add resulting in a POST to http://example.com/page/page/add.
You can still use a relative path, just change the action accordingly:
<form action="add" method="post" />