How to create custom popup based on success insert or failure mySQL? - mysql

I would like to have a customer popup window appear on a page after the insert to mySQL is completed.
I have the "header" going to a particular page, but I would also like to have a custom popup window appear after the page loads.
Here is my current php script. Everything works, but I need to add a popup window based on a success or failure.
<?
.........
if ($result) {
header("location: inv_fc.php"); //NEED TO ADD A CUSTOM POPUP FOR SUCCESS
}
else {
header("location: inv_fc.php"); //NEED TO ADD A CUSTOM POPUP FOR FAILURE
}
?>

Use session variables.
Start the session with session_start(), and then set the session variables using the $_SESSION array. Then in inv_fc.php, check for the existence of the session variable (you must also call session_start() in this file).
So, something like
session_start();
$_SESSION['success'] = ($result) ? TRUE : FALSE;
header('location: inv_fc.php');
// inside inv_fc.php
session_start();
if ($_SESSION['success'] == TRUE) {
// do success stuff
} else {
// do failure stuff
}
where we've used the ternary operator.

header("location: inv_fc.php?success=" . ($result ? 'y' : 'n'));
would be the easiest, then just look for that 'sucess' query parameter in the new page:
if ($_GET['success'] == 'y') {
... success ...
} else {
... epic fail ...
}

Related

Google Script Pass variable to templated HTMLService

I have a script which I have published as a web app. I wanted to change a default setting based on the URL used to run the web app. I am already opening one of two forms, but on one form, I want to have a radio button selected based on a second passed parameter. In the server side gs file I have:
function doGet(passed) {
switch(passed.parameter.form) {
case 'single':
var result = HtmlService.createTemplateFromFile('Single').evaluate();
result.setHeight(550);
result.setWidth(565);
break;
case 'grid':
default:
var result=HtmlService.createTemplateFromFile('GridView').evaluate();
result.setHeight(550);
result.setWidth(1285);
}
return result;
}
On Google's HTML Service: Templated HTML page there is a section Pushing variables to templates which seems to be what I want but I can't get it to work.
in my Single.html file I have:
<body>
<? if (data === "Ex") { ?> Existing <? } else { ?> New <? } ?>
... </body>
The html portion above is overly simplified and getting this to work will get me to my ends, which is a much larger page with input areas, etc.
In an attempt to get "Existing" to display in the resulting page, I have changed the above code to:
function doGet(passed) {
switch(passed.parameter.form) {
case 'single':
var result = HtmlService.createTemplateFromFile('Single');
result.setHeight(550);
result.setWidth(565);
result.data = 'Ex';
return result.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);
break;
case 'grid':
default:
var result=HtmlService.createTemplateFromFile('GridView').evaluate();
result.setHeight(550);
result.setWidth(1285);
return result;
}
}
and get errors
TypeError: Cannot find function setHeight in object HtmlTemplate. (line 131, file "Code")
Even if I remove the setHeight and setWidth resulting in just have the data as shown on the above referenced page I get errors.
Has anyone passed a variable to a page like this?
Looks like the method calls are just in the wrong order. The result.data = 'Ex'; should be before the .evaluate(), but the .setHeight() and .setWidth() must be applied afterwards. Modifying your last example slightly:
case 'single':
var result = HtmlService.createTemplateFromFile('Single');
result.data = 'Ex';
return result.evaluate()
.setHeight(550)
.setWidth(565)
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
break;
I tracked down the basic error to a missed } after your if statement in your HTML file. The code should look like:
<? if (data === "Ex") { ?> Existing <? } else { ?> New <? } ?>

Yii2 mpdf with barcode-generator

I'm using yii2 and installed two extension : mpdf , and yii2-barcode-generator-8-types.
Both of them were installed and configured properly and working well.
But what I can't do is to load barcode into pdf.
Here is my code :
Controller :
public function actionPdf()
{
$pdf = Yii::$app->pdf;
$pdf->content = $this->renderPartial('_pdf');
echo $this->renderPartial('_pdf');exit;
return $pdf->render();
}
View :
<div id="showBarcode"></div>
<?php
use barcode\barcode\BarcodeGenerator as BarcodeGenerator;
$optionsArray = array(
'elementId'=> 'showBarcode',
'value'=> '12345678',
'type'=>'code128',
);
echo BarcodeGenerator::widget($optionsArray);
?>
And it show something like this
but If I try to delete all code inside actionPdf() and just write
return $this->render("_pdf");
it show like this:
Please help!!!
I think your controller should be this
public function actionPdf()
{
$pdf = Yii::$app->pdf;
$pdf->content = $this->renderPartial('_pdf');
return $pdf->render();
}
The row with echo $this->renderPartial('_pdf');exit; don't must be used because it prevents the program to invoke correctly the render of the pdf page. If you use this instruction you displays only the render of "html like code" page like you see in result you posted moreover immediately after this instruction you exit form action without invoking the $pdf->render.

CKeditor returning 403 when submitting certain html tags

I've created a page where I have two input textareas and I add CKeditor (ver. 4) to both of them.
The first editor works fine, I've set config.allowedContent = true; in the config.js to stop stripping tags like <script> and everything works as expected.
I have another editor right below it, same settings, same setup, I just changed the ID of the textarea field. It works when I submit normal text, but as soon as I add a <script> tag, for example, and press the submit button of the form in which the editors are in it seems to reload the page, doesn't submit any data and firebug tells me that the server returns 403.
I tried isolating the editor, adding personal configuration. Nothing. The first textarea works like a charm, second one returns 403 if the text has unsafe tags in it.
My setup is as follows, I'm using this ckeditor helper to insert the editors where I need. Page is created with CodeIgniter as you guessed.
I got a config.js file in ckeditor folder.
I'm using a regular form, nothing fancy about it. It looks like this
<form action="http://domain.com/admin/articles/edit/47" method="post">
<div id="cke_ckeditor_en_container">
<textarea cols="75" rows="7" id="ckeditor_en" name="text_en" class="input-text is-col-text"><?php echo set_value('text_en', isset($text_en) ? htmlspecialchars_decode($text_en) : ''); ?></textarea>
<?php echo display_ckeditor($ckeditor_en); ?>
</div>
<input type="submit" value="submit" />
</form>
The form has another part of html for the other editor which is the same, with changed id and other attributes, and a checkbox, nothing relevant.
And got this in my controller
public function edit(){
$this->load->helper('ckeditor');
$id = (int)$this->uri->segment(4);
if (empty($id)){
$this->session->set_flashdata('error', 'Empty ID!');
redirect('admin/articles');
}
$data = $this->articles_model->fetch_article($id);
$data['page_title'] = "Edit `" . $data['title'] . "`";
$data['form_url'] = "admin/articles/edit/" . $id;
$data['ckeditor'] = array(
'id' => 'ckeditor',
'path' => 'js/ckeditor');
$data['ckeditor_en'] = array(
'id' => 'ckeditor_en',
'path' => 'js/ckeditor');
$data['edit'] = true;
if($this->input->post('submit')){
$this->save_article("update",$id);
}
$this->load->view("admin/articles",$data);
}
private function save_article($type='insert', $id=0){
$this->load->library('form_validation');
$this->form_validation->set_rules('title','Title','trim|xss_clean|max_length[150]|min_length[1]');
$this->form_validation->set_rules('text','Text','trim');
$this->form_validation->set_rules('title_en','Title EN','trim|xss_clean|max_length[150]|min_length[1]');
$this->form_validation->set_rules('text_en','Text EN','trim');
$this->form_validation->set_rules('top_menu','Show in top menu','trim|xss_clean|max_length[1]');
if ($this->form_validation->run() === FALSE)
{
return FALSE;
}
// make sure we only pass in the fields we want
$data = array();
$data['title'] = $this->input->post('title');
$data['text'] = htmlspecialchars($this->input->post('text'));
$data['title_en'] = $this->input->post('title_en');
$data['text_en'] = htmlspecialchars($this->input->post('text_en'));
$data['url'] = $this->toAscii($this->input->post('title'));
$data['url_en'] = $this->toAscii($this->input->post('title_en'));
$data['top_menu'] = $this->input->post('top_menu');
if($type == "insert"){
$data['time'] = date("YmdHis");
}
if ($type == 'insert'){
if($this->articles_model->insert($data)){
$this->session->set_flashdata('success', 'Article added successfully!');
}else{
$this->session->set_flashdata('error', 'An error occured!');
}
}else if ($type == 'update'){
if($this->articles_model->update($id, $data)){
$this->session->set_flashdata('success', 'Article `' . $data['title'] . '` edited successfully!');
}else{
$this->session->set_flashdata('error', 'An error ecc!');
}
}
redirect("admin/articles");
}
Safety, or unsafeness, to be exact, of my code is not relevant
edit
Adding config.js for ckeditor.
CKEDITOR.editorConfig = function( config ) {
config.filebrowserBrowseUrl = '/js/kcfinder/browse.php?type=files';
config.filebrowserImageBrowseUrl = '/js/kcfinder/browse.php?type=images';
config.filebrowserFlashBrowseUrl = '/js/kcfinder/browse.php?type=flash';
config.filebrowserUploadUrl = '/js/kcfinder/upload.php?type=files';
config.filebrowserImageUploadUrl = '/js/kcfinder/upload.php?type=images';
config.filebrowserFlashUploadUrl = '/js/kcfinder/upload.php?type=flash';
config.removeButtons = 'Underline,Subscript,Superscript';
config.allowedContent = true;
// Se the most common block elements.
config.format_tags = 'p;h1;h2;h3;pre';
// Make dialogs simpler.
config.removeDialogTabs = 'image:advanced;link:advanced';
};
I'm stumped and stupified, I've got no ideas on what to do. It seems that the one input has been cursed.
Any help appreciated, thank you.
This would be the result of your mod_security rules. Depending on how strict they are they help better protect scripts from being hacked through vulnerabilities, generally those exploited via POST's.
As I understand you are trying to add something to your second textarea. And CKEditor removing some "unsafe" tags. I won't be very secure, but this can help you:
config.extraAllowedContent = '*{*}';
You will add this to your config.js. This code provides you to add anything you want. And CKEditor won't delete "unsafe" tags.
Documenation for this method
https://www.bilisimkitabi.com/403-error-on-submit-of-ckeditor
You can add to following code in your .htaccess file
#ckeditor Post 403 problem
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
#ckeditor Post 403 problem

Prevent users from accessing admin area

Good Day,
am Facing a code error with my Admin control panel PHP page.
I want to prevent any other user from accessign this page unless his job_title= admin.
it works but even the admin himslef redirects back to login page again !!
here is the code
<?php
include('db.php');
?>
<?php
// Inialize session
#session_start();
ob_start();
// Check, if username session is NOT set then this page will jump to login page
if (!isset($_SESSION['name']) || ($_SESSION['job_title'] != "admin")) {
header('Location: index.php');
}
ob_flush();
?>
>?
<!-- STARTING HTML FORMAT !-->
?>
Any help ?
Try this, if still problems, let me know.
$_SESSION['name'] = 'dennis';
$_SESSION['job_title'] = 'admin';
if (!isset($_SESSION['name']) || (!isset($_SESSION['job_title']) ? false: ($_SESSION['job_title'] !== 'admin'))) {
echo 'Redirecting';
} else {
echo 'You\'re good! Not redirecting!';
}
This may be an easier way to understand, just put into a function.
$_SESSION['name'] = 'dennis';
$_SESSION['job_title'] = 'admin';
if (!isset($_SESSION['name']) || !isBoss()) {
echo 'Redirecting';
} else {
echo 'You\'re good! Not redirecting!';
}
function isBoss() {
if (isset($_SESSION['job_title']))
if ($_SESSION['job_title'] === 'admin')
return true;
return false;
}
I think your problem is a fubar newline :) Have added login page reference, which is unsetting the session variables you check for if logins fail.
Be very careful with where you put your <?php ?> tags - and in .inc files, such as db.php include - you may leave out the closing ?> tag on last line to avoid accidental ENTER, then CTRL + S failures, sneaking in an unwanted newline char in your output buffer (ob). IF ob_start is activated, nothing is written from server before you choose or script ends. ELSE if its not, default is that every \n will flush output and start the Content part of the payload.
login.php:
<?php
session_start(); // put this on top-most line in your script
$ok = check($_POST['user'], $_POST['pass']);
if($ok) {
$user = db_get_user_creds($_POST['user']);
$_SESSION['name'] = $user['name'];
$_SESSION['job_title'] = $user['job_title'];
} else {
// session_unset();
unset($_SESSION['name']);
unset($_SESSION['job_title']);
}
?>
admin.php
<?php
session_start(); // put this on top-most line in your script
// or, use ob_start at the very first line
// (with no widespace what so ever written out before it)
include('db.php');
?> I am writing out a newline here, session / header section is going to become unstable
<?php
// Inialize session
// #session_start(); moved up top
ob_start();
// Check, if username session is NOT set then this page will jump to login page
if (!isset($_SESSION['name']) || ($_SESSION['job_title'] != "admin")) {
header('Location: index.php');
}
ob_flush();
?>
See the 'I am writing out newline here' bit
A little deeper down rabbithole goes; the communication flow is like this:
1) HEADERS such as
Connection: keep-alive\r\n
Content-Type: text/html\r\n (etc)
2) DOUBLE NEWLINE (one newline with no previous chars on that line)
\r\n
3) CONTENTS
Body
Of Page

MediaWiki Extension question / suggestion

Complete beginner here. I want to create a new tab on each page that has a custom action. When clicked, it takes you to a new page which has custom HTML on it along with the text or the original article.
So far I could create a new Tab and could give a custom action mycustomaction to it. I am pasting what I did so far here. Please let me know if I am using the correct hooks etc. and what is a better way to achieve this basic functionality.
So far with their docs I have done this:
#Hook for Tab
$wgHooks['SkinTemplateContentActions'][] = 'myTab';
#Callback
function myTab( $content_actions) {
global $wgTitle;
$content_actions['0'] = array(
'text' => 'my custom label',
'href' => $wgTitle->getFullURL( 'action=mycustomaction' ),
);
return true;
}
#new action hook
$wgHooks['UnknownAction'][] = 'mycustomaction';
#callback
function mycustomaction($action, $article) {
echo $action;
return true;
}
This gives me error:
No such action
The action specified by the URL is invalid. You might have mistyped the URL, or followed an incorrect link. This might also indicate a bug in the software used by yourplugin
What I was doing wrong:
$content_actions[‘0’] should simply be $content_actions[] (minor nitpick)
$content_actions is passed-by-reference, it should be function myTab( &$content_actions ) {}
mycustomaction() should do something along the lines of
if ( $action == ‘mycustomaction’ ) {
do stuff; return false;
}
else {
return true;
}
It should use $wgOut->addHTML() instead of echo
Thanks a lot everyone for your help!