Alert in Pug(Jade) after retrieving a value from the node server - javascript

I want to implement an alert when the page loads under an expecific circunstance ,retrieving the value from an Node server when the user or passwowrd are wrong.
But it just doesnt do anything. I had tested it and yes, ErrorCon(the variable i get from my Node server) get its expected value when the circunstance appear so the node part shouldnt be the problem
This is the code i am trying to apply, right in the body:
script.
(function (){
function init(){
var Errors=ErrorCon;
if (Errors==1){
alert("Wrong password");
}
}
window.addEventListener?
window.addEventListener('load',init,false):
window.attachEvent('onload',init);
})();
The same structure works on a different page yet here it just doesnt do anything.
Help

Related

Load php file within javascript (AJAX or others)

I am trying to finish one page of my website the last couple of hours while achieving the following.
While clicking on a button, the following should happen
Download link appears (done - works)
The mySQL table should be opened and a counter should be incremented
As far as I got the points. Javascript cannot handle that and thus we can use AJAX or jQuery. I was already checking out different posts and websites such as:
how to execute php code within javascript
https://www.w3schools.com/js/js_ajax_database.asp
and much more. However, I guess I do have problems with the AJAX syntax and I actually don't know if the requested php files is loaded/opened or not. Especially the second link given above is almost similar to what I am searching for. However, it does not work. To check if the php file is called, I set an alert which works if I do call the file explicitly in the browser. Maybe this does not work with AJAX as I expect it. Here the code to get more familiar with the inconstency I am doing.
The page code:
<?php
echo '<div><button onclick="incrementAndDownload('testPath', 'fileName'); ">Click me</button></div>';
?>
<script>
function incrementAndDownload (link, fileName)
{
$.ajax({
url: 'openfoam/increment.php',
success: function(data) {
// Print something if necessary
}
});
//- Open the link
// window.open(arguments[0], "_blank");
//- Increment download inside mysql
//var xhttp;
//xhttp = new XMLHttpRequest();
//xhttp.open("GET", "openfoam/increment.php?foo=nana", true);
//xhttp.send();
}
</script>
The increment.php looks as follows:
<?php
echo '<script type="text/javascript" language="Javascript">
alert("Test message if the script is called...");
</script>';
// Code for accessing the mysql database and manipulate the data
//$page_id = mysql_real_escape_string(html_entities($_POST['file']));
?>
Now when I click the button, the javascript is executed (e.g., if I uncomment the window.open) this works as expected. However, as already said, the second part is to open the database via php and increment a number (counter). For any reason, I am not able to figure out where the problem is located. I am even not sure if AJAX opens the increment.php file (the alert messages never appears so I guess it is never called). Any suggestion is appreciated and I hope that this question does not just contain a fundamental small error. Thank in advance, Tobi
It's not the way the AJAX works. If you call alert() on a destination page it won't show in your browser. Your case is very basic so I will keep my solution on a basic level.
In increment.php just echo something, it can be just OK string. So when you go to increment.php page you will see only OK, nothing more, nothing less.
Then go back to your javascript and check what is your response.
$.ajax({
url: 'openfoam/increment.php',
success: function(data) {
if (data == 'OK') {
console.log('It works, sir!');
}
}
});
If you don't see a message in a console after these modifications something doesn't work. However, I think your page is executed properly, but you just don't get feedback, because you don't handle the response (data param in your case).
Check it out and don't forget to give me a feedback!🤓

Why does Stripe's handleCardPayment not run in my Javascript function?

I am trying to use Stripes handleCardPayment to process a charge following a payment intent. I can't get the handleCardPayment function to run inside another Javascript function.
I have tried to debug my code step by step. The Javascript function runs and will print out an alert to the window.
The Javascript variables I set at the top of the function also get set.
The handleCardPayment() function however does not seem to run?
I have attempted a try/catch to see if I can catch the error coming from the request but nothing is logged.
I am a little stumped as to where the issue is with this? although I suspect it might be something basic that I am missing on calling the stripe function correctly.
function completePayment() {
// Assign client secret from PHP session variable
var clientSecret = "<?php echo $_SESSION['c_secret'] ?>";
try {
stripe.handleCardPayment(
clientSecret, cardElement, {
source_data: {
owner: {email: "<?php echo $_SESSION['m_usr_email'] ?>"}
}
}
).then(function(result) {
if (result.error) {
alert("Error in payment");
} else {
alert("Success in payment");
}
});
}
catch(error) {
console.log(error.message);
}
}
My payment intent is created on a separate PHP page. This works correctly and a payment intent is created in stripe with an associated client secret.
How the app currently functions:
User presses 'Subscribe' button.
Ajax request runs and POSTS to a separate PHP file
PHP file creates the payment intent and saves the client secret in a PHP SESSION variable
Ajax 'Complete function()' then calls 'completePayment();'
Javascript function 'completePayment();' is located just before the closing tag at the bottom of the page.
Debugging 'completePayment()' shows that the function does execute following the Ajax 'Complete function()' call.
stripe.HandleCardPayment fails to do anything.
cardElement is a global Javascript variable set when the card element is created (this is created on initial page load).
I have debugged both PHP SESSION variables and confirm that they have both been set with the correct information prior to using them in the handleCardPayment function.
Any suggestions on what I am doing wrong here?
I had same problem. It looks like the function it does not execute becouse the console logs does not appear but after searching I saw that I missing $intent->client_secret, before that, the js console not show me anything and I had to call only the function stripe.handleCardPayment without any code extra then the cosole show me $intent->client_secret It was undefined.
I don't speak english very well I hope that It will serve somebody

DotNetBrowser calling a JS function and returning a value from C#

I see the option where you can initiate the function call in .NET and return a value, but I don't see an option where you are able to initiate a function call in javascript calling a .NET function and then return a value to javascript, similar to what would happen if I was using ASP.NET or hitting a Web API.
So basically here is what I am doing. I have a modal that pops up when the user clicks to save game. They enter the name of the Save Game file, click OK and it calls a .NET function to check the DB to see if it's a duplicate save game file or not. If it is, it should return "Error! Duplicate file name! Please choose a unique file name." If it is unique it saves the information to a DB and returns a "Successfully saved game!" message, waits 2 seconds and then redirects to the main dashboard page.
I have everything working fine up until the point .NET is supposed to return the value. Currently the modal window just sits there and nothing returns to JS.
I have it set up like I normally would through calling the .Net function and using .then(function (response) to get the response value, but it is coming back as undefined.
I'm sure there has to be a way to do this, I just don't see how in the documentation as that only shows how to do it when initiating the function call from .NET...
EDIT: OMG...I just realized this was such a dumb question...I'm already doing that returning lookups from the database...the answer was to set it equal to a variable instead of trying to chain the function onto the end.
THIS:
var value = window.CRUD.Save(fileName, model);
if(value === "Duplicate!") ....
INSTEAD OF:
window.CRUD.Save(fileName, model).then(function(response) {
});
The article by the following link explains how to call .NET from JavaScript:
https://dotnetbrowser.support.teamdev.com/support/solutions/articles/9000109869-calling-net-from-javascript
Thank you for adding your answer to the question as an update.

Call to $.post() Jquery not passing parameters successfully

/**
* Created by quantumveil on 2/10/15.
*/
$(document).ready(function(){
$('a').on('click',function(){
//$href=$(this).attr('href');
//console.log($href);
var $convoid=$(this).attr('id');
console.log($convoid);
//$('#convoarea').load($href);
$.get('inbox.php',{convoid:$convoid},function(data){$('#convoarea').html(data);//the callback sets whati t had to
//now add the on click handler to send button
$('#send').on('click',function(){
var $msgbody=$('#sendbody').val();
console.log($msgbody);
///now what i have to do is sent this $msgbody to the inbox.php thru get k
$.post('inbox.php',{sendbody:$msgbody,convoid:$convoid},function(data){
$('#sendbody').before(data);
console.log('here'+$msgbody);
});//the callback function will append it to other messages :3
return false;
});
}//callback ends hre
);
return false;
});
//for send button
});
Hi. I'm trying to code a social networking site and I want the inbox to be responsive and dynamic. I've recently started learning Jquery for the purpose being. I've googled this before and found no help and so it brings me here on this community. The above Javascript/Jquery code is supposed to pass some post parameters when the SEND button is clicked. Another file, inbox.php, is to receive those and work appropriately. Now here's what bugs me. The callback function to $.post() is executed, so I'm assuming the parameters are being passed to inbox.php. But when I try accessing them in inbox.php using following line
if($msgbody=get_post('sendbody')&&$convoid=get_post('convoid'))
I only receive a value of 1 and not the message's actual body. Here's how the function get_post is defined
function get_post($var1){
if(isset($_POST[$var1])){$var=$_POST[$var1];
return filter($var);}
else return 0;
}
I've tried accessing them directly through $_POST['sendbody'] but an error of undefined index is being generated. Any help will be highly appreciated. (PS the other call to .get() in the beginning of this js file is passing the parameters so there's nothing wrong with file paths)
EDIT: It's fixed but I want an explanation. All I did was in the inbox.php changed the first line which was
if($msgbody=get_post('sendbody')
to
if(isset($_POST['sendbody'])$msgbody=$_POST['sendbody']
Now all I can wonder is if it has something to do with filter() function in the definition of my get_post() function. Anybody?

Why does .match() stop my code from executing?

I'm having trouble with a form I'm trying to validate. I'm trying to make sure for the email input that the email is the correct format, and if it is not I'll be able to send an error message to the user. Unfortunately, I can't get the return false; to execute.
Here is my current code:
function validate(){
var email = $('input.email').value;
var emailRE = /^.*#.+\..{2,5}$/;
if (email.match(emailRE)){
alert("This is true");
} else {
alert("This isn't true.");
}
return false;
}
When I execute this, the page reloads and the form submits, and neither alert goes off. However, when I take out the whole if, else section, the return false; executes, and the page doesn't reload. I also tried taking out just the '.match()', and the code executed fine. So what is wrong with the .match() part that's causing it to fail?
Any help is extremely appreciated. Thank you!
You have to use $('input.email').val() instead of $('input.email').value
http://jsfiddle.net/bMJH2/6/
value is for the DOM object. But the object you are dealing with is a jQuery object, so the proper way is to use val(). If you run it in Firefox/Firebug using value, it will show the error:
email is undefined
[Break On This Error] if (email.match(emailRE)){

Categories

Resources