Reputation: 43
I want to write a code that if you pressed the button and the username and password was correct. then it shows you a table that contains photos and a link to their Instagram.
when table tag is used inside of a function in script tag the function breaks and there is no function in html itself. so where should I put my table to be shown when the button has been pushed?
here is my code:
<html>
<head>
<title>balalar</title>
<script type="text/javascript">
function check(){
var username=document.getElementById("username").value;
var password=document.getElementById("password").value;
if(username == "user1" && password == "1234"){
alert("hello user")
<!--want to put the table here. but it breaks the function-->
}
else
{
alert("chek username and password");
}
}
</script>
</head>
<body>
<form align="center">
username:<input type="text" name="username" id="username" /><br>
</br>
password:<input type="password" name="password" id="password"/><br>
</br>
<input type="button" value="login" id="login" onclick="return
check()"/>
</form>
</body>
</html>
here is my table :
<!DOCTYPE html>
<html>
<link rel=stylesheets; href="css.css">
<head>
<title>BALALAR</title>
<style>
table, th, td {
border:5px solid black;
border-collapse: collapse;
}
th, td {
padding: 30px;
}
tr:nth-child(even) {
background: pink;
}
th {
background: gray;
}
</style>
</head>
<body>
<table align="center">
<caption style="color: #ff99ac"> <h1>[BALALAR]</h1> </caption>
<tr> <!--heading------------------------------------------------------------>
<th>photo</th>
<th>name</th>
<th>age</th>
<th>info</th>
<th>instagram</th>
</tr>
<tr> <!--lucifer------------------------------------------------------------>
<td> <center> <img src="https://scontent-ams3-1.cdninstagram.com/vp/17d60601e17a27d3a0196470f00c55a4/5CF8DA8C/t51.2885-15/e35/50850072_2165205533518446_3117212119304625148_n.jpg?_nc_ht=scontent-ams3-1.cdninstagram.com" height="200px" width="200px"> </center> </td>
<td> <center>lucifersexdoll</center> </td>
<td> <center>18</center> </td>
<td> <center>instagram star and tiktoker</center> </td>
<td> <center> <a href="https://www.instagram.com/lucifersexdoll/?hl=en"> <img src="https://www.edigitalagency.com.au/wp-content/uploads/new-instagram-logo-png-transparent.png" height="50px" width="50px"> </a> </center> </td>
</tr>
<tr> <!--belle delphine----------------------------------------------------->
<td> <center> <img src="https://scontent-dfw5-1.cdninstagram.com/vp/6331b02f48e920975e98434c595fd4ee/5CE47C32/t51.2885-15/e35/41760483_210825032975228_2673466538809180596_n.jpg?_nc_ht=scontent-dfw5-1.cdninstagram.com" height="200px" width="170px"> </center> </td>
<td> <center>belle delphine</center> </td>
<td> <center>19</center></td>
<td> <center>instagram star and snapchat thot</center></td>
<td> <center> <a href="https://www.instagram.com/belle.delphine/?hl=en"> <img src="https://www.edigitalagency.com.au/wp-content/uploads/new-instagram-logo-png-transparent.png" height="50px" width="50px"> </a> </center> </td>
</tr>
<tr> <!--Georgia Twinn------------------------------------------------------>
<td> <center> <img src="https://scontent-frx5-1.cdninstagram.com/vp/b4cdaabe45fd41c4d36e18c074a0cb71/5CEFBA3C/t51.2885-15/e35/43986219_288982548424250_1512646608107907870_n.jpg?_nc_ht=scontent-frx5-1.cdninstagram.com" style="transform: rotate(-90deg);" height="200px" width="200" > </center> </td>
<td> <center>Georgia Twinn</center> </td>
<td> <center>15</center> </td>
<td> <center> instagram star and youtuber</center> </td>
<td> <center> <a href="https://www.instagram.com/georgialeetwinn/"> <img src="https://www.edigitalagency.com.au/wp-content/uploads/new-instagram-logo-png-transparent.png" height="50px" width="50px" > </a> </center> </td>
</tr>
<tr> <!--lilsaggester------------------------------------------------------->
<td> <center> <img src="https://scontent-ams3-1.cdninstagram.com/vp/b7b351d27e76a70aed0dc97ca2946b31/5CF7E289/t51.2885-15/e35/44478694_259861178046212_7053862547100372241_n.jpg?_nc_ht=scontent-ams3-1.cdninstagram.com" height="200px" width="200px" > </center> </td>
<td> <center>hannah fabel</center> </td>
<td> <center>16</center> </td>
<td> <center>tiktoker</center> </td>
<td> <center> <a href="https://www.instagram.com/aestheticallyhannah/"> <img src="https://www.edigitalagency.com.au/wp-content/uploads/new-instagram-logo-png-transparent.png" height="50px" width="50px" > </a> </center> </td>
</tr>
<tr> <!--cyxll-------------------------------------------------------------->
<td> <center> <img src="https://scontent-ams3-1.cdninstagram.com/vp/fe0da5e3353e102fd30a771e7f18d793/5CEA7C9B/t51.2885-15/e35/50510784_321000775215940_7823002975742791621_n.jpg?_nc_ht=scontent-ams3-1.cdninstagram.com" height="200px" width="250px"> </center> </td>
<td> <center>cyxll</center> </td>
<td> <center>unknown</center> </td>
<td> <center>instagram user</center> </td>
<td> <center> <a href="https://www.instagram.com/cyxll/?hl=en"> <img src="https://www.edigitalagency.com.au/wp-content/uploads/new-instagram-logo-png-transparent.png" height="50px" width="50px"> </a> </center> </td>
</tr>
<tr> <!--gal gadot---------------------------------------------------------->
<td> <center><img src="https://scontent-ams3-1.cdninstagram.com/vp/85ba393e3d05f6047fea8ac9ee61edd4/5D250597/t51.2885-15/e35/40265259_229384887926581_2920908417464813508_n.jpg?_nc_ht=scontent-ams3-1.cdninstagram.com" height="200px" width="200px"> </center> </td>
<td> <center>gal gadot</center> </td>
<td> <center>33</center> </td>
<td> <center>actress</center> </td>
<td> <center> <a href="https://www.instagram.com/gal_gadot/?hl=en"> <img src="https://www.edigitalagency.com.au/wp-content/uploads/new-instagram-logo-png-transparent.png" height="50px" width="50px"> </a></center> </td>
</tr>
</table>
</body>
</html>
want to put different tables for different users later.
Upvotes: 1
Views: 130
Reputation: 103
I see you are leaving the table and the login page in 2 different html files. put it in an html page and handle in the check function ():
function check(){
var username=document.getElementById("username").value;
var password=document.getElementById("password").value;
if(username == "user1" && password == "1234"){
alert("hello user")
<!--want to put the table here. but it breaks the function-->
document.getElementsByTagName("table")[0].style.display = "block";
document.getElementsByTagName("form")[0].style.display = "none";
}
else
{
alert("chek username and password");
}
}
<html>
<head>
<title>balalar</title>
<style>
table, th, td {
border:5px solid black;
border-collapse: collapse;
}
th, td {
padding: 30px;
}
tr:nth-child(even) {
background: pink;
}
th {
background: gray;
}
table{
display: none
}
</style>
</head>
<body>
<form align="center">
username:<input type="text" name="username" id="username" /><br>
</br>
password:<input type="password" name="password" id="password"/><br>
</br>
<input type="button" value="login" id="login" onclick="check()"/>
</form>
<table align="center">
<caption style="color: #ff99ac"> <h1>[BALALAR]</h1> </caption>
<tr> <!--heading------------------------------------------------------------>
<th>photo</th>
<th>name</th>
<th>age</th>
<th>info</th>
<th>instagram</th>
</tr>
<tr> <!--lucifer------------------------------------------------------------>
<td> <center> <img src="https://scontent-ams3-1.cdninstagram.com/vp/17d60601e17a27d3a0196470f00c55a4/5CF8DA8C/t51.2885-15/e35/50850072_2165205533518446_3117212119304625148_n.jpg?_nc_ht=scontent-ams3-1.cdninstagram.com" height="200px" width="200px"> </center> </td>
<td> <center>lucifersexdoll</center> </td>
<td> <center>18</center> </td>
<td> <center>instagram star and tiktoker</center> </td>
<td> <center> <a href="https://www.instagram.com/lucifersexdoll/?hl=en"> <img src="https://www.edigitalagency.com.au/wp-content/uploads/new-instagram-logo-png-transparent.png" height="50px" width="50px"> </a> </center> </td>
</tr>
<tr> <!--belle delphine----------------------------------------------------->
<td> <center> <img src="https://scontent-dfw5-1.cdninstagram.com/vp/6331b02f48e920975e98434c595fd4ee/5CE47C32/t51.2885-15/e35/41760483_210825032975228_2673466538809180596_n.jpg?_nc_ht=scontent-dfw5-1.cdninstagram.com" height="200px" width="170px"> </center> </td>
<td> <center>belle delphine</center> </td>
<td> <center>19</center></td>
<td> <center>instagram star and snapchat thot</center></td>
<td> <center> <a href="https://www.instagram.com/belle.delphine/?hl=en"> <img src="https://www.edigitalagency.com.au/wp-content/uploads/new-instagram-logo-png-transparent.png" height="50px" width="50px"> </a> </center> </td>
</tr>
<tr> <!--Georgia Twinn------------------------------------------------------>
<td> <center> <img src="https://scontent-frx5-1.cdninstagram.com/vp/b4cdaabe45fd41c4d36e18c074a0cb71/5CEFBA3C/t51.2885-15/e35/43986219_288982548424250_1512646608107907870_n.jpg?_nc_ht=scontent-frx5-1.cdninstagram.com" style="transform: rotate(-90deg);" height="200px" width="200" > </center> </td>
<td> <center>Georgia Twinn</center> </td>
<td> <center>15</center> </td>
<td> <center> instagram star and youtuber</center> </td>
<td> <center> <a href="https://www.instagram.com/georgialeetwinn/"> <img src="https://www.edigitalagency.com.au/wp-content/uploads/new-instagram-logo-png-transparent.png" height="50px" width="50px" > </a> </center> </td>
</tr>
<tr> <!--lilsaggester------------------------------------------------------->
<td> <center> <img src="https://scontent-ams3-1.cdninstagram.com/vp/b7b351d27e76a70aed0dc97ca2946b31/5CF7E289/t51.2885-15/e35/44478694_259861178046212_7053862547100372241_n.jpg?_nc_ht=scontent-ams3-1.cdninstagram.com" height="200px" width="200px" > </center> </td>
<td> <center>hannah fabel</center> </td>
<td> <center>16</center> </td>
<td> <center>tiktoker</center> </td>
<td> <center> <a href="https://www.instagram.com/aestheticallyhannah/"> <img src="https://www.edigitalagency.com.au/wp-content/uploads/new-instagram-logo-png-transparent.png" height="50px" width="50px" > </a> </center> </td>
</tr>
<tr> <!--cyxll-------------------------------------------------------------->
<td> <center> <img src="https://scontent-ams3-1.cdninstagram.com/vp/fe0da5e3353e102fd30a771e7f18d793/5CEA7C9B/t51.2885-15/e35/50510784_321000775215940_7823002975742791621_n.jpg?_nc_ht=scontent-ams3-1.cdninstagram.com" height="200px" width="250px"> </center> </td>
<td> <center>cyxll</center> </td>
<td> <center>unknown</center> </td>
<td> <center>instagram user</center> </td>
<td> <center> <a href="https://www.instagram.com/cyxll/?hl=en"> <img src="https://www.edigitalagency.com.au/wp-content/uploads/new-instagram-logo-png-transparent.png" height="50px" width="50px"> </a> </center> </td>
</tr>
<tr> <!--gal gadot---------------------------------------------------------->
<td> <center><img src="https://scontent-ams3-1.cdninstagram.com/vp/85ba393e3d05f6047fea8ac9ee61edd4/5D250597/t51.2885-15/e35/40265259_229384887926581_2920908417464813508_n.jpg?_nc_ht=scontent-ams3-1.cdninstagram.com" height="200px" width="200px"> </center> </td>
<td> <center>gal gadot</center> </td>
<td> <center>33</center> </td>
<td> <center>actress</center> </td>
<td> <center> <a href="https://www.instagram.com/gal_gadot/?hl=en"> <img src="https://www.edigitalagency.com.au/wp-content/uploads/new-instagram-logo-png-transparent.png" height="50px" width="50px"> </a></center> </td>
</tr>
</table>
</body>
</html>
If you want to display only one row in the table, set the id for it and set display: block
for the row with the corresponding id
Upvotes: 0
Reputation: 31
You could leave your table in HTML body... and make #tableID {display: none;}
In JS:
function check(){
var username=document.getElementById("username").value;
var password=document.getElementById("password").value;
if(username == "user1" && password == "1234"){
alert("hello user")
document.getElementById('tableID').style.display = 'block';
}
else {alert("chek username and password");}
}
But even if there is too specific code, when you have to place your HTML in JS, you must escape all special characters (search > JavaScript string escape)
Mostly with symbol \
... sometimes with []
or HTML-code
Ex.,
<td> <center> <img src="https://scontent-ams3-1.cdninstagram.com/vp/17d60601e17a27d3a0196470f00c55a4/5CF8DA8C/t51.2885-15/e35/50850072_2165205533518446_3117212119304625148_n.jpg?_nc_ht=scontent-ams3-1.cdninstagram.com" height="200px" width="200px"> </center> </td>
Must be replaced with:
<td> <center> <img src="https:\/\/scontent-ams3-1.cdninstagram.com\/vp\/17d60601e17a27d3a0196470f00c55a4\/5CF8DA8C\/t51.2885-15\/e35\/50850072_2165205533518446_3117212119304625148_n.jpg?_nc_ht=scontent-ams3-1.cdninstagram.com" height=\'200px\' width=\'200px\'> <\/center> <\/td>
You can try such operations here - - https://liveweave.com/ It has comfortable coloring, which tells, if there is a mistake.
P.s. 'could you explain a little more?':
<table id="AnyName"> ... </table>
Each HTML element can have a unique Id, which can be targeted from JavaScript.
Also, if you select classes in CSS with .class {}
... ID can be selected with #id {}
. So, we make initially invisible table with #AnyName {display: none}
And after click... if username and password match - we get element by ID and make it visible:
document.getElementById('AnyName').style.display = 'block';
It says "Take element with this ID, and change it's style"
There could be .style.border = '1px solid red';
or .style.backgroundColor = 'red';
But we need to make it visible... so, display = 'block';
Upvotes: 1
Reputation: 256
There are many ways to achieve this. I got two for you:
Here some code:
function getAjax(url, success) {
var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
xhr.open('GET', url);
xhr.onreadystatechange = function() {
if (xhr.readyState>3 && xhr.status==200) success(xhr.responseText);
};
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.send();
return xhr;
}
// example request
//getAjax('http://foo.bar/?p1=1&p2=Hello+World', function(data){ console.log(data); });
<html>
<head>
<title>balalar</title>
<script type="text/javascript">
function check(){
var username=document.getElementById("username").value;
var password=document.getElementById("password").value;
if(username == "user1" && password == "1234"){
alert("hello user")
// 1. Solution - Redirect to another "Page"
window.location.href = "/table.html?username="+username;
// 2. Solution - do an Ajax Call and render response
getAjax("/table.html?username="+username, function(response){
// replace body with new content ("table")
document.getElementsByTagName('body')[0].innerHTML = response;
});
}
else
{
alert("chek username and password");
}
}
</script>
</head>
<body>
<form align="center">
username:<input type="text" name="username" id="username" /><br>
</br>
password:<input type="password" name="password" id="password"/><br>
</br>
<input type="button" value="login" id="login" onclick="return
check()"/>
</form>
</body>
</html>
for both examples you need a file called table.html
with the table content next to your index.html
Upvotes: 0