JavaScript Function not working on call within php code - javascript

My function error() is not working on calling from php code.
so Kindly help me to resolve this problem. fucntion works properly when called outside the php code.but no output shown when called insisde the php code.
<html>
<head>
<title>Login Page</title>
<link rel="stylesheet" href="css/design.css">
<script type="text/JavaScript">
function error(){
document.getElementById("wrong-attempt").innerHTML="Wrong Username Or Password";
}
</script>
<?php
if(isset($_POST['submit'])){
echo '<script> error(); </script>';
}
?>
</head>
<body background="pics/background.jpg">
<div class="wrapper">
<center>
<div class="form-wrapper">
<form action="index.php" method="POST">
<div>
</div>
<h1>User Login</h1>
<div>
<img src="pics/login.png" width="100" height="100"/>
</div>
<div>
<input name="ID" placeholder=" Username" type="text" required="true"/></br>
<input name="pass" placeholder=" Password" type="password" required="true"></br>
<input type="image" src="pics/loginb.png" name="submit" value="Login" width="100"/>
</div>
</form>
<div id="wrong-attempt">
</div>
</div>
<center/>
</div>
</body>'
</html>

Put that PHP block after the wrong-attempt div element. You are calling the error() function which manipulates that element before it is even created in the browser.
As it has also been suggested in the comments, instead of complicating with an unnecessary JavaScript function call, just place that PHP if into the <div id="wrong-attempt"></div>element, and simply print out the message, or the whole element, to not even send this div if there is no error.
And put your JavaScript into the footer, just before the closing </body> tag, as also suggested in comments.

Related

Redirecting from a HTML button to a new page with JS

This is fairly simple, I'm trying to make the login button work, and redirect to a different file (placeholder.html) if what is in the email and password match the following:
Login: Admin
Password: Password123
I have yet to get the login button to work in the first place to redirect, and id like to keep it as a button element by using, the 'onlick=' with a function in js or an id for now.
Thank you so much for the help :)
Apologies if this is a little sloppy, one of my first times asking for help here haha
Here is what I had so far, which dent work fully:
<!DOCTYPE html>
<html>
<head>
<title>Login page</title>
<link href="style." rel="stylesheet" type="text/css" />
<script src="script.js"></script>
</head>
<body>
<div class="login-page">
<div name="login" class="form">
<div class="title login" style="font-size: 25px; font-family: Roboto; padding-bottom: 25px;">
Login Form
</div>
<form class="login-form" action="login.php" method="post">
<input type="email_" placeholder="email"/>
<input type="password" placeholder="password"/>
<button id="login_button">login</button>
<p class="message" style="font-family: Roboto, sans-serif;">Not registered?
Create an account
</p>
</form>
</div>
</div>
</body>
<script type="text/javascript">
document.getElementById("login_button").onclick = function () {
location.href = "#placeholder";
};
</script>
</html>
Welcome to StackOverflow, there are a few things that I wanted to cover here as part of the answer.
The function is running onClick, the way you're using location.href is invalid though. You are redirecting to an anchor (it starts with a #), The way this is implemented in browsers is that it will redirect your scroll positioning to the top of the element with ID, if you had an element on your page with the ID of placeholder (and the page height was more than 100%) it would move the scroll position of the page, not redirect.
I believe you're looking for
location.href = "placeholder.html";
You wanted the user to be redirected based on the username and password that they enter, I will provide the solution below but before that, I wanted to say that this isn't secure, the username and password are visible inside the source code and if you planned to use this to hide important information it will not work because the code is visible to anyone who accesses the webpage, you should be using a server-side language such as NodeJS or PHP to hide that information, I assume that this is just for practise/learning though.
The first thing you'll want to do is get the username and password.
Your <input type="email_" placeholder="email"/> should be <input type="text" placeholder="email"/>. For some reason you put the type as what looks to be a typo email_ and not email but since you said the username was admin that is just text.
I would start by giving an ID to both the username and password elements.
<input type="email" id="username" placeholder="email"/>
<input type="password" id="password" placeholder="password"/>
Then inside your function you can change it to check and redirect like below
document.getElementById("login_button").onclick = function () {
const username = document.getElementById("username").value
const password = document.getElementById("password").value
if (username === 'Admin' && password === 'Password123') {
location.href = "placeholder.html";
}
};
That will also be case-sensitive.
There are a couple of ways that you could do this, here is a simple example:
<!DOCTYPE html>
<html>
<head>
<title>Login page</title>
<link href="style." rel="stylesheet" type="text/css" />
<script src="script.js"></script>
</head>
<body>
<div class="login-page">
<div name="login" class="form">
<div class="title login" style="font-size: 25px; font-family: Roboto; padding-bottom: 25px;">
Login Form
</div>
<form class="login-form" action="login.php" method="post">
<input type="email_" placeholder="email"/>
<input type="password" placeholder="password"/>
<button id="login_button" onclick="login()">login</button>
<p class="message" style="font-family: Roboto, sans-serif;">Not registered?
Create an account
</p>
</form>
</div>
</div>
</body>
<script type="text/javascript">
function login() {
// Login logic here
if (loginSuccesful) {
window.location.href = "/placeholder.html";
}
}
</script>
</html>

Trying to create a random query-based search engine in JavaScript but I'm stuck

So, I have tried a number of solutions but none of them have worked and I am exhausted in terms of reading documentation at this point. This is my first post on StackOverflow. Please, be gentle.
I am trying to create a random search string query engine. Every time you type in a search form, it selects from an array that has been randomized to use a different search engine string. I can't figure out what I am doing incorrectly.
Here is my code so far:
<!DOCTYPE html>
<html>
<head>
<script>
var randomlinks=new Array()
randomlinks[0]="https://duckduckgo.com/?q="
randomlinks[1]="https://www.startpage.com/do/dsearch?query="
randomlinks[2]="https://dogpile.com/serp?q="
randomlinks[3]="https://google.com/search?q="
function randomlink(){
window.location=randomlinks[Math.floor(Math.random()*randomlinks.length)]
}
</script>
</head>
<body>
<div class="search-wrapper ">
<div class="input-holder ">
<form id="bar" role="search" method="get" class="search-form" action="randomlink()">
<input type="text" class="search-input"/>
</form>
</div>
</div>
</html>
Thank you in advance for your time!
I think this is what you're trying to do (I tried and it works).
In the form action add javascript to refer to the function then you can get the value from the input using getElementById and pass it as a parameter to your function :
<!DOCTYPE html>
<html>
<head>
<script>
var randomlinks=new Array()
randomlinks[0]="https://duckduckgo.com/?q="
randomlinks[1]="https://www.startpage.com/do/dsearch?query="
randomlinks[2]="https://dogpile.com/serp?q="
randomlinks[3]="https://google.com/search?q="
function randomlink(link){
window.location=randomlinks[Math.floor(Math.random()*randomlinks.length)]+link;
}
</script>
</head>
<body>
<div class="search-wrapper ">
<div class="input-holder ">
<form id="bar" role="search" method="GET" class="search-form" action="javascript:randomlink(document.getElementById('abc').value)">
<input type="text" class="search-input" id="abc"/>
<input type="submit"/>
</form>
</div>
</div>
</html>

How to create a foreground window to display extra content in html?

I hope to click "Upload Files" lable to display a foreground windows which include extra content, Submit and Close buttons in a html web page.
I write the following code, but it's ugly, and orginal UI is damaged when the is shown.
Is there a good way to do that? or a good control can do that?
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>WiFi File Transfer</title>
<meta charset="utf-8" />
<script src="js/jquery1.10.2.min.js?isassets=1" type="text/javascript"></script>
<script src="js/user.js?isassets=1" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$("#UploadWindows").hide();
$("#UploadFile").click(function() {
$("#UploadWindows").show();
});
$("#Close").click(function() {
$("#UploadWindows").hide();
});
});
</script>
</head>
<body>
<div id="container">
<div id="header">
<a href="#">
<span id="UploadFile">Upload Files</span>
</a>
</div>
<div id="UploadWindows">
<form action="" method="post" enctype="multipart/form-data">
Please select files to upload <br />
<input type="file" name="myupload" multiple="multiple" />
<br />
<input type="submit" value="Upload Files" />
<br />
<span id="Close">Close</span>
</form>
</div>
<div id="Div1">
Other Content!
</div>
</div>
</body>
</html>
BTW, I think the edit popup window of stackoverflow is very good, I don't know if there is simple way do to it.

Dropdown Div Doesn't Work

The following code does not drop down and up the div when clicked,It doesn't perform anything.But Im trying the exact code that works here http://jsfiddle.net/vNeXq/
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<script>
$('#login').click(function(){
$('#loginForm').slideToggle('fast');
});
</script>
</head>
<body>
<div id="loginWrapper">
<a id="login">Login</a>
<div id="loginForm">
<form name="login" method="post">
<input type="text" name="username" placeholder="Username" />
<input type="password" />
<input type="submit" value="Login" />
</form>
</div>
</div>
</body>
CSS File
#loginWrapper
{
width:400px;
background-color:#F0F0F0;
}
#login
{
cursor:pointer;
}
#loginForm
{
display:none;
}
Pretty sure you haven't included jQuery. The jQuery library is necessary for the script you want to run. You can download a copy of jQuery from here or you can use a copy hosted from Google. jQuery has to be included before the code you want to run.
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="loginWrapper">
<a id="login">Login</a>
<div id="loginForm">
<form name="login" method="post">
<input type="text" name="username" placeholder="Username" />
<input type="password" />
<input type="submit" value="Login" />
</form>
</div>
</div>
<!-- INCLUDE JQUERY -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#login').click(function(){
$('#loginForm').slideToggle('fast');
});
}):
</script>
</body>
</html>
If you have included jQuery in your project you maybe want to wrap your code with a function that executes when the page is loaded, so you are sure everything is there:
$(document).ready(function(){
$('#login').click(function(){
$('#loginForm').slideToggle('fast');
});
})
You can try like:
<script>
$(document).ready(function(){
$(document).on('click','#login',function(){
$('#loginForm').slideToggle('fast');
});
});
</script>
It seems that are your not including the jQuery library in the head of the html file.
Include the jquery library (it should be before your script):
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
In order for jQuery to modify the html document it must wait until the page is ready. You can do this by putting your function into:
$(document).ready(function() {
// your code here
});
So your script will end up looking like:
$(document).ready(function() {
$('#login').click(function(){
$('#loginForm').slideToggle('fast');
});
});

Submit function not getting called when form is submitted

The onSubmit tag does not seem to be working. I am trying to call the submit() javascript function when the submit button is clicked on the webpage. Here is the code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="login.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="jquery.serializeJSON.min.js"></script>
</head>
<body>
<script type="text/javascript">
function submit() {
.......
}
</script>
<div class="login-container">
<div class="title">Login Form</div>
<div class="form-fields">
<form name="login-form" id="login-form" onSubmit="return submit()">
<input type="text" name="username" id="username" placeholder="Username" required></input>
<input type="password" name="password" id="password" placeholder="Password" required></input>
<input type="submit" value="Log in" id="submit-button"></input>
</form>
</div>
</div>
</body>
There is a naming conflict.
The form has a native built in function which can be seen with
<form id="x" onsubmit="console.log(submit);"><input type="submit"/></form>
When you look at the console you will see
function submit() { [native code] }
So when you call submit() you are calling that native submit function aka document.getElementById("login-form").submit(); and not yours. To get around it, change the name.
Change the function name to something other than submit.
function xSubmit(){
}
and
<form name="login-form" id="login-form" onSubmit="return xSubmit()">
You can not use "submit" as the function name. Submit is a JavaScript keyword. Simply rename your function to something else.
function submitForm() {
console.log('I work now');
}
<form name="login-form" id="login-form" onSubmit="return submitForm()">
It should be a lowercase s.
<form name="login-form" id="login-form" onsubmit="return submit()">
check out http://www.w3schools.com/jsref/event_onsubmit.asp for more info.
EDIT:
Sorry, I did some testing and found out that it was the name submit.
Take a look at this fiddle http://jsfiddle.net/9z95chze/.
You also shouldn't need to say return

Categories

Resources