Why the else statement not working here? - ajax

The following code can't get correct response under the else statment:
function ajax_response(){
if(is_user_logged_in() && check_key() ){
$result = $do_something;
if($result){
ajax_response_string(1,'success!');
}else{
ajax_response_string(-1,'there is a problem, please try again!');
}
}else{
ajax_response_string(-1,'you must login first.');
}
}
When I log-out and try to click the link that trigger this ajax function, I got a response of "undefined", it is supposed to be "you must login first". Where I've gone wrong?

I just figured it out-- it's not the else statement, it's the ajax response url not working for logged out users. So, my question is not valid anymore. Please close it if necessary.

You have a syntax error at line $result= //do something because this line is not terminated. Try this:
function ajax_response()
{
if(is_user_logged_in() && check_key() )
{
$result = doSomething; //error was here as there was no line terminator (;) present.
if($result)
{
ajax_response_string(1,'success!');
}
else
{
ajax_response_string(-1,'there is a problem, please try again!');
}
}
else
{
ajax_response_string(-1,'you must login first.');
}
}

Related

How To Check If Notification Is Sent Successfully In Laravel

I am unable to find in the documentation of how to handle the notification (what to do if notification is sent successfully or not)
NotificationController
public function AskForLevelUp()
{
$admin = User::where('is_admin', 1)->first();
$sendNotification = Notification::send($admin, new LevelUpNotification(Auth::user()->id));
if ($sendNotification->success()) // **HOW TO DO THIS CORRECTLY**
{
return back()->with('success', 'Your request has been submitted, please wait for admin confirmation.');
}
else
{
return back()->with('failure', 'Something went wrong, your request is not submitted. Please try again later.');
}
}
I've also read that send() has void return value here. Does anyone know how to handle this?

Recaptcha V2 with Formtoemailpro

Has anyone edited formtoemailpro to use Recaptcha v2? This is the formtoemailpro developer version which gives you a PHP script: (https://formtoemail.com/developer_pricing.php)
I tried changing
// Check reCAPTCHA
if($reCAPTCHA)
{
require_once('recaptchalib.php');
$resp = recaptcha_check_answer($privatekey,$_SERVER["REMOTE_ADDR"],$_REQUEST["recaptcha_challenge_field"],$_REQUEST["recaptcha_response_field"]);
if(!$resp->is_valid)
{
$errors[] = "The reCAPTCHA wasn't entered correctly. Go back and try it again (reCAPTCHA said: " . $resp->error . ")";
}
unset($_REQUEST["recaptcha_challenge_field"]);
unset($_REQUEST["recaptcha_response_field"]);
}
to
// Check reCAPTCHA
if($reCAPTCHA)
{
//require_once('recaptchalib.php');
//$resp = recaptcha_check_answer($privatekey,$_SERVER["REMOTE_ADDR"],$_REQUEST["recaptcha_challenge_field"],$_REQUEST["recaptcha_response_field"]);
$resp = recaptcha_check_answer($privatekey,$_SERVER["REMOTE_ADDR"],$_REQUEST["g-recaptcha-response"]);
if(!$resp->is_valid)
{
$errors[] = "The reCAPTCHA wasn't entered correctly. Go back and try it again (reCAPTCHA said: " . $resp->error . ")";
}
unset($_REQUEST["g-recaptcha-response"]);
}
But got errors.
This page isn’t working
webeg.uk is currently unable to handle this request.
Test is at http://webeg.uk/aaq/
Can anyone point me to the right direction?
I think I have a solution, please feel free to test and critique:
In place of the existing reCaptcha lines in formtoemailpro as above, enter the following:
// Check reCAPTCHA
if($reCAPTCHA)
{
if(isset($_REQUEST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo '<h2>Please check the the captcha form.</h2>';
exit;
}
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$privatekey&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==true)
{
unset($_REQUEST['g-recaptcha-response']);
}
}
Thank you

Laravel Collective SSH results

I am performing SSH in Laravel whereby I connect to another server and download a file. I am using Laravel Collective https://laravelcollective.com/docs/5.4/ssh
So, the suggested way to do this is something like this
$result = \SSH::into('scripts')->get('/srv/somelocation/'.$fileName, $path);
if($result) {
return $path;
} else {
return 401;
}
Now that successfully downloads the file and moves it to my local server. However, I am always returned 401 because $result seems to be Null.
I cant find much or getting the result back from the SSH. I have also tried
$result = \SSH::into('scripts')->get('/srv/somelocation/'.$fileName, $path, function($line){
dd( $line.PHP_EOL);
});
But that never gets into the inner function.
Is there any way I can get the result back from the SSH? I just want to handle it properly if there is an error.
Thanks
Rather than rely on $result to give you true / false / error, you can check if the file was downloaded successfully in another way:
// download the file
$result = \SSH::into('scripts')->get('/srv/somelocation/'.$fileName, $path);
// see if downloaded file exists
if ( file_exists($path) ) {
return $path;
} else {
return 401;
}
u need to pass file name also like this in get and put method:
$fileName = "example.txt";
$get = \SSH::into('scripts')->get('/remote/somelocation/'.$fileName, base_path($fileName));
in set method
$set = \SSH::into('scripts')->set(base_path($fileName),'/remote/location/'.$fileName);
in list
$command = SSH::into('scripts')->run(['ls -lsa'],function($output) {
dd($output);
});

Ajax ResponseText with If statement

I am using Ajax to modify some data in a mysql database via a php file. I have written the code so that the php file echoes "OK" or "ERROR". I've checked with alert(ret) and it's working fine. But the problem is with the if (ret=="OK"). It's not getting inside this statement. Can anybody help me out?
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById('Store_cards_action_form_close').click();
ret = xmlhttp.responseText;
if (ret=="OK"){
alert("Congratulations. Transaction Successful.");
document.location.reload();
}
else{
alert("You have Insufficient Coins to buy this Card!");
}
}
}
xmlhttp.open("GET","script_card_transaction.php?" + para,true);
xmlhttp.send();
As mentioned in my comment, you probably have some whitespace characters around your response text. You could trim the response text or instead, work with a JSON formatted string. For example, in your PHP file
header('Content-type: application/json');
echo json_encode(array('status' => $status)); // where $status is 'OK' or 'ERROR'
exit;
Then, parse this as JSON in your JS
var ret = JSON.parse(xmlhttp.responseText);
if (ret.status == 'OK') {
// etc
I would probably go a bit further and use something less ambiguous than the strings "OK" and "ERROR". For example
echo json_encode(array('success' => $isSuccess)); // where $isSuccess is a boolean (true or false)
and the JS...
if (ret.success) {
// etc

CodeIgniter - UserAgent

I have the below code to check if the user is coming from a mobile, if they are I want to load a different page but it doesn't seem to be working...
Can anyone shed any light on the below:
if($xmlRefresh==1) {
$viewData['content'] = $this->load->view('newquote/policy_xml', $viewData);
$this->load->view('layout', $viewData);
} elseif ($this->agent->is_mobile()) {
$this->_showPage("newquote/policy_mobile", 'Customise', $viewData);
} else {
$this->_showPage("newquote/policy", 'Customise', $viewData);
}
I keep getting an internal server error, can anyone spot the problem...
Look at your system/core/Loader.php, function view() that will be $this->load->view returns void unless you add third parameter. try with:
$viewData2['content'] = $this->load->view('newquote/policy_xml', $viewData1, TRUE);
and
'''
$this->_showPage("newquote/policy_mobile", 'Customise', $viewData2);
Note: $viewData2 is different with $viewData1
are you calling this in controller?
What is the 500 (internal server) error??
Did you load the library user_agent??
$this->load->library('user_agent');
if($xmlRefresh==1) {
$viewData['content'] = $this->load->view('newquote/policy_xml', $viewData);
$this->load->view('layout', $viewData);
} elseif ($this->agent->is_mobile()) {
$this->_showPage("newquote/policy_mobile", 'Customise', $viewData);
} else {
$this->_showPage("newquote/policy", 'Customise', $viewData);
}
Finally if you want serve the mobile pages, use post post or pre hooks to determine the user agent before view is executed (if you want to play around)
i have one idea, may be help u
you can replace redirect()/$this->load->view() to $this->_showPage("newquote/policy_mobile", 'Customise', $viewData);

Resources