How to display message in PHP using certain string in the URL? - php

I have WHMCS setup, and i am trying to make a message bubble that would show when someone access it with the specific link.
Like when someone access this link, they will see normal page - https://example.com/billing/clientarea.php?action=details
But if someone access https://example.com/billing/clientarea.php?action=details&status=incomplete
They will get a message.
I have already setup the message, and it is showing on the default link. But i do not know how to set it up on the 2nd link only? I am using WHMCS.
Can anyone guide me?
Code for message bubble.
<div class="alert alert-danger">
<strong>The following errors occurred:</strong>
<ul>
<li>Please enter your address and click on save to proceed. Once Saved, then only you will be able to access the client area.</li>
</ul>
</div>
EDIT: Solution Added
thanks a ton for your help #symlink, your method works on PHP, but for WHMCS/smarty php, it needs other code, a very simple code that too, lol.
{if $smarty.get.status eq incomplete}
{include file="$template/includes/alert.tpl" type="info" msg="Please fill the form to continue with our services."}
{else}
{/if}

If the $GET param exists, add a class to the message div that makes it appear:
PHP/HTML
<?php
$class = "";
if(isset($GET["status"]) && $GET["status"] === "incomplete"){
$class = "show";
}
?>
<div class="alert alert-danger <?php echo $class ?>">
<strong>The following errors occurred:</strong>
<ul>
<li>Please enter your address and click on save to proceed. Once Saved,
then only you will be able to access the client area.</li>
</ul>
</div>
CSS
.alert.alert-danger{
display: none;
}
.alert.alert-danger.show{
display: block;
}

If you edit templates, it makes it harder to keep your WHMCS up to date. I would recommend using a hook to output some JS to inject the appropriate alert to your page.
Perhaps the ClientAreaFooterOutput hook point? https://developers.whmcs.com/hooks-reference/output/#clientareafooteroutput
Something like:
<?php
add_hook('ClientAreaFooterOutput', 1, function($vars) {
$status = App::getFromRequest('status'); //This is handy and sanitised
return <<<HTML
jQuery(document).on('ready', function() {
if ('{$status}' == 'incomplete') {
//Add your code here to output the alert where you wish
}
});
HTML;
});

Related

WordPress - IF statement not working within archive.php

I'm looking to add a simple IF statement to check whether a custom field is selected, and if so, add the code 'display:none'.
I have the code I want to add:
if( get_field('my_custom_field') )
{
echo "";
}
else
{
echo "style='display:none'";
}
But, if I add this code where I want it, it throws an error, plus, becaus eof where it's being placed, no matter what other php type code I add, it throws up an error, I believe it's because it's in some kind of loop it's not allowing it, hopefully you'll be able to assist, here is the code it's sitting within:
$offices.= '
<div class="office_section" id="office_part_'.$locator.'">
<h1>'.$officetitle.'</h1>
'.$room_specs.'
<div class="office_dimensions_holder" *** this is where I would like to
add custom IF statement, but doesn't allow me to ***>
<div class="dimensions">
<div class="dimension_sml">Dimensions: </div>
'.$dimension1.'
I imagine it's because it's within the $offices.= ' call, so would appreciate the correct syntax to allow me to check the custom field I have recently added is true and if not...then add a display:none.
Looks like this question about php syntax...
Correct one should looks like:
$offices.= '
<div class="office_section" id="office_part_'.$locator.'">
<h1>'.$officetitle.'</h1>
'.$room_specs.'
<div class="office_dimensions_holder" '.(get_field('my_custom_field') ? "" : "style='display:none'").'>
<div class="dimensions">
<div class="dimension_sml">Dimensions: </div>
'.$dimension1.'
I ended up entering this outside my code and then called the conditional inside it:
$showdimensions = get_field('show_dimensions');
if(!empty($showdimensions)){
$showdimensions = 'style="display:none"';
} else {
$showdimensions = '';
}
Then within the original code added:
'.$showdimensions.'
to pull in the display:none if the checkbox was not checked.

defining custom urls in codeigniter

In codeigniter we can define the url that should be displayed in the browser. I have a login page which goes well when the credentials are given right. I need to show an error message in the same page itself if the credentials are wrong. For that i have did the followings.
<div style="<?php echo ($status==='err')?'display:block':''?>" class="form-login-error">
<h3>Invalid login</h3>
<p>Please enter correct email and password!</p>
</div>
and
$data['status'] = (isset($_GET['status']))?$_GET['status']:true;
in controllers's index function
if($userdata['status'] == 'success')
{
if(!empty($userdata['userdet']))
{
$this->session->set_userdata($userdata['userdet']);
redirect('dashboard');
}
}else{
redirect('login?status=err');
}
The code works well. but my doubt is can i change the url pattern? It is now showing loclhost/myproject/login?status=err.Can i change this to localhost/myproject/loginerror? I tried to define in routes, but it is showing page not found error. Thanks in advance.
You are making a simple thing much complex by using URL's param's here.
I would suggest using CI session Flashdata. The benefit of using Flashdata is that the session variable is maintained only for the next page and then the session variable is cleared automatically so it is very useful in cases where we need to display some messages to users.
Change your code to something like this :
<div style="<?php echo ($this->session->flashdata('error') )?'display:block':''?>" class="form-login-error">
<h3>Invalid login</h3>
<p>Please enter correct email and password!</p>
</div>
and in your controller :
if($userdata['status'] == 'success')
{
if(!empty($userdata['userdet']))
{
$this->session->set_userdata($userdata['userdet']);
redirect('dashboard');
}
}else{
$this->session->set_flashdata('error', 1);
redirect('login');
}
use the codeigniter application/config/routes.php
$route['myproject/login/(:any)']="myproject/login/$1";
youcan hold the value of parameter in action of controller.

my php get request doesnt appear in the requested php file

Basically, I am trying to have a user click a button on one php page and it will print a new php page. This function WORKS. The problem is I am trying to have the userID transferred over to the 2nd page and printed on the new page. However, for some reason this seems to be getting lost. When I alert the user# right before the get request is made, the # shows pops up. But when the new page appears in the printer dialog, the userID is missing. Any suggestions would be greatly appreciated.
Function from php page 1:
$("#printing").on('click',function(){
alert (user);
$.get("flyer.php", {userNow: user});
$('body').append('<iframe src="flyer.php?userNow" id="printIFrame" name="printIFrame"></iframe>');
$('#printIFrame').bind('load',
function() {
window.frames['printIFrame'].focus();
window.frames['printIFrame'].print();
}
);
});
php page 2:
<html>
<body>
<div class="wrapper">
<h1>Tai Chi</h1>
<h2>Sign Up Now!</h2>
<h3>Benefits of Tai Chi:</h3>
<ul>
<li>It enhances harmony between body and mind</li>
<li>It improves your internal energy level</li>
<li>Improves your muscle strength and endurance</li>
<li>Reduces back pain</li>
<li>Lowers daily stress</li>
<li>Can slow down the aging process</li>
</ul>
<div id="contact">For more information, please email R-adler#neiu.edu</div>
<div id="userID">
<? echo UserID.$user = $_GET['userNow'] ?>
</div>
</div>
</body>
Try to concatenate the user value like this:
$("#printing").on('click',function(){
alert (user);
$('body').append('<iframe src="flyer.php?userNow='+user+'" id="printIFrame" name="printIFrame"></iframe>');
$('#printIFrame').bind('load',
function() {
window.frames['printIFrame'].focus();
window.frames['printIFrame'].print();
}
);
});
Check also in the url if you get the uservalue passed there.
There is no value being passed in the GET array.
Use the link as flyer.php?userNow="+user

Hide/Show Input's based on mysql data

I have an active session in my page using: $_session_start(); I want to hide part of my form and show another based on the users previous entries.
Basically I offer Direct Deposit and Paper Check I want to be able to have the user only see the fields required for them to complete previously (in signup so the values are already in the database)
Right now the database table is set up like this:
paper_check with a value of 1 (yes pay this way) or 0 (no pay this way)
and the same thing with direct deposit. I need a way to show/hide the fields associated with each based on the users previous selections.
I typed this up but it doesn't seem to do anything. Please help me!!!!! (The class names hide and show are in my css and working properly!)
<?php
if ($_SESSION['direct_deposit'] == 1)
{
$doc->getElementById('check_payable_to')->className+" hide";
}
else
{
$doc->getElementById('name_on_account')->className+" hide";
$doc->getElementById('check_payable_to')->className+" show";
}
?>
I don't see a className property in the PHP DOM library. It looks like this is the way to add a class to an element:
$doc->getElementById('check_payable_to')->setAttribute('class',
$doc->getElementById('check_payable_to')->getAttribute('class') . " hide");
Do you have error reporting enabled? It should be warning about the unknown property.
If you want to do this in Javascript instead of PHP, have your PHP do something like:
<script>
var direct_deposit = <?php echo $_SESSION['direct_deposit']; ?>;
if (direct_deposit == 1) {
/* Do what you want */
} else {
/* Do what else you want */
}
</script>
In your question you said "I have an active session in my page using: $_session_start();" You shouldn't use $_session_start(). It should be session_start() .
className+" hide" should be className .= " hide"
By wrapping each section in it's own div like so:
<div id="paperCheck class="show">
<!--Your Paper Check Information here-->
</div>
<div id="directDeposit" class="show">
<!--Your Direct Deposit Information here-->
</div>
And then by using javascript you are able to do the following:
var direct_deposit = <?php echo $_SESSION['direct_deposit']; ?>;
if (direct_deposit == 1)
{
document.getElementById('paperCheck').className ="hide";
}
else
{
document.getElementById('directDeposit').className ="hide";
}

Remove this URL string when login fails and simply show div error

My developer built our registration page to display a div when logins failed based on a string in the URL.
When logins fail this is added to the URL /login?msg=invalid
The PHP in my login.phtml which displays the error messages based on the msg= parameter is
<?php
$msg = "";
$msg = $_GET['msg'];
if($msg==""){ $showMsg = ""; }
elseif($msg=="invalid"){ $showMsg = ' <div class="alert alert-error">
<a class="close" data-dismiss="alert">×</a>
<strong>Error!</strong> Login or password is incorrect!
</div>'; }
elseif($msg=="disabled"){ $showMsg = "Your account has been disabled."; }
elseif($msg==2){ $showMsg = "Your account is not activated. Please check your email."; }
?>
In the controller the redirect to that URL is
else //email id does not exist in our database
{
//redirecting back with invalid email(invalid) msg=invalid.
$this->_redirect($url."?msg=invalid");
}
I know there are a few other validation types for disabled accounts etc. I'm in the process of redesigning the entire interface and would like to get rid of this kind of validation so that the div tags display when logins fail but not show the URL strings.
If it matters the new div I want to display is
<div class="alert alert-error alert-login">
Email or password incorrect
</div>
I'd like to replace the php my self in my login.phtml and controller but not a good programmer. What can I replace $this->_redirect($url."?msg=invalid"); with so that no strings are added to the URL and display the appropriate div tags?
Thanks
Passing a msg through a GET parameter is a bit weird to me.
See this thread discussing other ways to send a message to the page, using sessions, cookies or database, here.
I'm not familiar with the zend framework, but there has to be a way to set a "flash" message through the session.
change this in loginAction of your contorller
$this->_redirect($url."?msg=invalid");
to
$this->view->message = $msg;
then add to your code to login.phtml
<?php if ($this->message) :?>
<div class="alert alert-error alert-login">
Email or password incorrect
</div>
<? endif; ?>
... your form code there

Categories