Running PHP, Laravel and Selenium, using the Modelizer/Selenium package. I have a simple form (made with LaravelCollective) that has no hidden elements or fancy javascript.
The form and test code are posted below.
I get
1) can_search_by_city::testCanSearchByCity
Modelizer\Selenium\Exceptions\CannotFindElement: Cannot find element: Search by City isn't visible on the page
I've reviewed all the similar questions. Nothing that is not super-specific to another problem.
Here is my code and then my form...
$this->visit('/login')
->type($this->User->email, 'email')
->type('secret', 'password')
->press('Login')
->see('Searches')
->type('queensbury ny','address')
->type('100','distance')
->wait(20)
->click('Search by City')
->see('Glens Falls #81')
;
}
and then my form...
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title> Welcome
- Elk RV Parking</title>
<!-- Fonts -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css" integrity="sha384-XdYbMnZ/QjLh6iI4ogqCTaIjrFk87ip+ekIjefZch0Y+PvJ8CDYtEs1ipDmPorQ+" crossorigin="anonymous">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:100,300,400,700">
<!-- Styles -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<style>
body {
font-family: 'Lato';
}
.fa-btn {
margin-right: 6px;
}
</style>
</head>
<body id="app-layout">
<nav class="navbar navbar-default navbar-static-top">
<div class="container">
<div class="navbar-header">
<!-- Collapsed Hamburger -->
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#app-navbar-collapse">
<span class="sr-only">Toggle Navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<!-- Branding Image -->
<a class="navbar-brand" href="http://localhost:8000">
Elk RV Parking
</a>
</div>
<div class="collapse navbar-collapse" id="app-navbar-collapse">
<!-- Left Side Of Navbar -->
<ul class="nav navbar-nav">
<li>Home</li>
</ul>
<!-- Right Side Of Navbar -->
<ul class="nav navbar-nav navbar-right">
<!-- Authentication Links -->
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
Ed Greenberg <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li><i class="fa fa-btn fa-sign-out"></i>Logout</li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading">Welcome to Elk RV Parking</div>
<div class="panel-body">
<h1>Searches</h1>
<h2>by distance from a city or address</h2>
<form method="POST" action="http://localhost:8000/search/address" accept-charset="UTF-8" id="search-by-city"><input name="_token" type="hidden" value="CBphF66rzzk3MbYiNpLK2EsC1jxqVAt6XwRQA9vo">
<input name="_token" type="hidden" value="CBphF66rzzk3MbYiNpLK2EsC1jxqVAt6XwRQA9vo">
<div class="row">
<div class="col-md-8">Enter a city, state, a state, an address, anything that Google Maps would properly search</div>
<div class="col=md-4">Distance</div>
</div>
<div class="row">
<div class="col-md-8"><input size="50" name="address" type="text" value=""></div>
<div class="col=md-4"><input name="distance" type="text"></div>
</div>
<div>
<div><input type="submit" value="Search by City"></div>
</div>
</form>
<h2>byLodge Number</h2>
<form method="POST" action="http://localhost:8000/search/lodgenumber" accept-charset="UTF-8" id="search-by-lodge-number"><input name="_token" type="hidden" value="CBphF66rzzk3MbYiNpLK2EsC1jxqVAt6XwRQA9vo">
<input name="_token" type="hidden" value="CBphF66rzzk3MbYiNpLK2EsC1jxqVAt6XwRQA9vo">
<div class="row">
<div class="col-md-8">Enter a lodge number</div>
</div>
<div class="row">
<div class="col-md-8"><input size="10" name="lodge_number" type="text" value=""></div>
</div>
<div class="row">
<div class="col-md-8"><input type="submit" value="Search by Lodge Number"></div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<!-- JavaScripts -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js" integrity="sha384-I6F5OKECLVtK/BL+8iSLDEHowSAfUo76ZL9+kGAgTRdiByINKJaqTPH/QVNS1VDb" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
</body>
</html>
My understanding is that Selenium looks for the element by it's value. In ths case, "Search by City". I don't see why it's not visible.
You may want to use press API to perform the required operation. As, based on the information available on Modelizer/Laravel-Selenium wiki, click looks for the name, id or text property of the anchor tag. Hence, you can try following:
$this->visit('/login')
->type($this->User->email, 'email')
->type('secret', 'password')
->press('Login')
->see('Searches')
->type('queensbury ny','address')
->type('100','distance')
->wait(20)
->press('Search by City')
->see('Glens Falls #81')
Related
I have some Error when queryin in Microsoft Access
I got error "Warning: odbc_exec(): SQL error: [Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'CHECKTIME BETWEEN ts '2021-08-01'} AND ts '2021-08-30'}'., SQL state 37000 in SQLExecDirect in C:\laragon\www\mondelez-report-access\getLog.php on line 38
37000" when trying to insert Microsoft access.
this is my code in getLog.php
<?php
// setup database for your microsoft Access
// you can setup password in you microsoft Access
// this is a variable for your connection in odbc
// "zkConnection" is your ODBC Data Source Administrator
$conn = odbc_connect("zkConnection", "", "");
// create condition for testing conecction
if ($conn) {
// echo "<br>Connection Established</br>";
} else {
echo "Koneksi Gagal";
}
$from_date = $_REQUEST['from_date'];
$to_date = $_REQUEST['to_date'];
$query = "INSERT INTO TransactionLog (USERID, CHECKTIME, Name, lastname, CardNo, DEPTID, DEPTNAME, SUPDEPTID)
SELECT CHECKINOUT.USERID, CHECKINOUT.CHECKTIME, USERINFO.Name, USERINFO.lastname, USERINFO.CardNo, DEPARTMENTS.DEPTID, DEPARTMENTS.DEPTNAME, DEPARTMENTS.SUPDEPTID
FROM
(CHECKINOUT
LEFT JOIN USERINFO ON USERINFO.USERID = CHECKINOUT.USERID)
LEFT JOIN DEPARTMENTS ON DEPARTMENTS.DEPTID = USERINFO.DEFAULTDEPTID
WHERE CHECKTIME BETWEEN {ts '" . $from_date . "'} AND {ts '" . $to_date . "'} ";
$letsgo = odbc_exec($conn, $query);
if ($letsgo === false) {
die(print_r(odbc_error(), true));
}
header("location: logTransaction.php");
?>
and this is my view for progress getLog.php
<?php
// For starting Session in php
session_start();
// We check if user login or not
// Check with this syntax
if (isset($_SESSION['id'])) {
?>
<DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Mondelez International</title>
<link rel="shortcut icon" href="../mondelez-report/assets/img/mondelez-logo.png" />
<!-- CSS Load Area -->
<link rel="stylesheet" href="assets/plugins/bootstrap/css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="assets/plugins/font-awesome/css/font-awesome.min.css" type="text/css" />
<!-- CSS Custom Load Area -->
<link rel="stylesheet" href="assets/css/mondelez.css" type="text/css" />
</head>
<body>
<?php
include 'database.php';
?>
<!-- Navigation Bar -->
<nav class="navbar navbar-default" style="background-color: var(--mondelez-white); border-color: var(--mondelez-primary);">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.php">
<img alt="Brand" src="../mondelez-report/assets/img/mondelez-logo.png" alt="logo" class="img img-responsive" style="width: 100px; height: 100px; margin-top: -40px;">
</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>Calculate Data</li>
<li>Transaction Log</li>
<li class="dropdown">
Reports <span class="caret"></span>
<ul class="dropdown-menu">
<li>Attendance Reports</li>
<li>Working Days Reports</li>
<li>Transaction Log Reports</li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
admin<span class="caret"></span>
<ul class="dropdown-menu">
<li>Logout</li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
<!-- Navigation Bar End -->
<!-- Header -->
<div class="container">
<div class="row">
<div class="col-md-12">
<img class="mondelez-box-home-and-reports-container-image" src="../mondelez-report/assets/img/mondelez-logo.png" alt="logo" />
</div>
<div class="col-md-12">
<span class="mondelez-box-home-and-reports-container-text">Transaction Log</span>
</div>
</div>
</div>
<!-- Header End -->
<br />
<!-- Form Reports -->
<div class="container">
<div class="row">
<div class="col-md-12">
<form action="getLog.php" method="post" id="sample_form">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Period From : </label>
<input type="date" name="from_date" required="required" class="form-control">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Period To : </label>
<input type="date" name="to_date" required="required" class="form-control">
</div>
</div>
<div class="col-md-12">
<div class="form-actions">
<button type="submit" class="btn btn-primary" style="float: right;" name="submit">
<i class="fa fa-download" style="padding-right: 5px;"></i>
Get Log
</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
<!-- Form Reports End -->
<!-- Footer -->
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="footer">
Powered by <a class="no-link" href="https://asiasekuriti.co.id/" target="_blank">PT Asia Sekuriti Indonesia.</a>
</div>
</div>
</div>
</div>
<!-- Footer End -->
<!-- JS Load Area -->
<script src="assets/plugins/jquery/jquery.min.js"></script>
<script src="assets/plugins/bootstrap/js/bootstrap.min.js"></script>
<!-- JS Custom Load Area -->
<script src="assets/js/mondelez.js"></script>
</body>
</html>
</DOCTYPE>
<?php
} else {
echo "Invalid Request";
}
?>
please help, I already try many tutorial for fix this issue but still not working
Date values in Access are not text, so your code should result in a finished expression like this:
WHERE CHECKTIME BETWEEN #2021/01/31# AND #2021/12/31#;
Quick Note : This thread was closed for being a duplicate question however the links I found were to do with 'Preventing MySQL Injection Attacks', which was not the answer I was looking for. If reposting this question goes against any guidelines, I don't mind taking this post down.
I've been working on an online booking website and the projects works when I run it on WAMP. I decided to get 1 month hosting on ecowebhosting.com and I've uploaded my project/files as well as change my 'db.php' file to match the settings of phpMyAdmin ( host, username, password, etc. ) however I am not able to sign in.
This is the login page for my website : https://gyazo.com/07f6bb065971b20ba07628d2a68cf1b0
And you are able to register, and the user has been registered when I checked the database on phpMyAdmin however I am not able to login as this occurs when I login in any account : https://gyazo.com/312725f461d4790a99b1b47e97a97066
I thought it was something to do with the 'dashboard.php' file, so I removed the 'include("auth_session.php")' to see if anything was wrong with the file, but it worked as normal. Here is a copy of the code for 'login.php':
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Modern Haircut Designs</title>
<!-- Bootstrap css style sheet -->
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
<!-- For the seperate stuff -->
<link href="coursework_style.css" rel="stylesheet">
<!-- Make sure to add this to the coursework style sheet so less files -->
<link href="login_style.css" rel="stylesheet">
<!-- To enable the javascript shenanigans -->
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
<?php
require('db.php');
session_start();
if (isset($_POST['username'])) {
$username = stripslashes($_REQUEST['username']);
$username = mysqli_real_escape_string($con, $username);
$password = stripslashes($_REQUEST['password']);
$password = mysqli_real_escape_string($con, $password);
$query = "SELECT * FROM `users` WHERE username='$username'
AND password='$password'";
$result = mysqli_query($con, $query) or die(mysql_error());
$rows = mysqli_num_rows($result);
if ($rows == 1) {
$_SESSION['username'] = $username;
header("Location: dashboard.php");
} else {
//Might be necessary to create an error page so the user knows?
header("Location: login.php");
}
} else{
?>
<nav class="navbar navbar-fixed-top navbar-inverse">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/index.php">Modern Haircut Designs</a>
</div>
<div class="collapse navbar-collapse" id="navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="actif">Home</li>
<li>Services</li>
<li>Our Team</li>
<li>Reviews</li>
<li>Contact</li>
<li>Login</li>
</ul>
</div>
</div>
</nav>
<section class="login-cover">
<div class="wrapper fadeInDown">
<div id="formContent">
<div class="fadeIn first">
<img src="img/login_icon.jpg" id="icon" alt="MHC Logo" />
</div>
<form method="post" name="login">
<input type="text" id="login" class="fadeIn second" name="username" placeholder="Username" required>
<input type="password" id="password" class="fadeIn third" name="password" placeholder="Password" style="background-color: #f6f6f6;border: none;color: #0d0d0d;padding: 15px 32px;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;margin: 5px;width: 85%;border: 2px solid #f6f6f6;transition: all;0.5s ease-in-out;border-radius: 5px 5px 5px 5px;" required>
<input type="submit" class="fadeIn fourth" value="Login" name="submit">
</form>
<div class="formFooter">
<a class="underlineHover" href="register.php" id="register">Register Here</a>
<a class="underlineHover" href="#" id="forgot_password">Forgot Password?</a>
</div>
</div>
</div>
</section>
<?php
}
?>
<footer style="bottom: 0; width: 100%;">
crafted with ♥ in Bangladesh by ###
</footer>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
</body>
</html>
As requested, here's a markup for the 'dashboard.php' file:
<?php
//Reminder to include this for the booking part
include("auth_session.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Modern Haircut Designs</title>
<!-- Bootstrap Stuff -->
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
<!-- Coursework Style that's seperate -->
<link href="coursework_style.css" rel="stylesheet">
</head>
<body>
<nav class="#">
<div class="container">
<div class="#">
<button type="button" class="#">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="#" href="#top">Modern Haircut Designs</a>
</div>
<div class="#" id="#">
<ul class="#">
<li class="actif">Home</li>
<li>Services</li>
<li>Our Team</li>
<?php
if($_SESSION['username'] == 'admin'){
?><li>Manage Appointments</li><?php
}else{
?><li>View Appointments</li>
<li>Book Appointment</li>
<?php
}
?>
<li>Logout</li>
</ul>
</div>
</div>
</nav>
<div class="cover" id="top">
<div class="cover-text">
<h1>Hey, <?php echo $_SESSION['username']; ?>!</h1>
<p class="lead">Book an appointment now by clicking the button below</p>
Get started!
</div>
</div>
<section id="services">
<h2>Here is a list of the services we provide</h2>
<div class="container">
<div class="row">
#Text about the different types of haircuts
</section>
<section id="team">
<div class="container">
#Pictures and quotes of the different team members
</div>
</section>
<section id="contact-us">
<div class="container">
#ContactInformation here
</div>
</section>
<footer>
crafted with ♥ in Bangladesh by ###
</footer>
<!-- jQuery for javascript stuff-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Bootstrap javascript code / might remove tho -->
<script src="bootstrap/js/bootstrap.min.js"></script>
</body>
</html>
The solution, as pointed out by #rickdenhaan, was to move the block of php code containing the 'header(###)' section to the start of the file. Another solution I found was to use javascript to open the file.
I'm currently having some issues with creating a webpage that's only accessible once a user has logged in.
I've looked through various threads here, but to no avail. Any help with this would be greatly appreciated.
Here's my code:
login.php
<?php
Include('connect.php');
if (isset($_REQUEST['Submit']))
{
if($_REQUEST['user_id']=="" || $_REQUEST['password']=="")
{
echo " Field must be filled";
}
else
{
$sql1= "select * from student where email= '".$_REQUEST['user_id']."' && password ='".$_REQUEST['password']."'";
$result=mysql_query($sql1)
or exit("Sql Error".mysql_error());
$num_rows=mysql_num_rows($result);
if($num_rows>0)
{
session_start($_SESSION['Login']);
Echo "You have logged in successfully";
header("Location: statistics.html");
}
else
{
echo "Wrong username or password.";
}
}
}
?>
<!DOCTYPE html>
<html class="no-js">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>PHP Login Form</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/main.css">
</head>
<body>
<div class="container">
<div class="row" style="margin-top:20px">
<div class="col-xs-12 col-sm-8 col-md-6 col-sm-offset-2 col-md-offset-3">
<form name="form_login" method="post" action="login.php" role="form">
<fieldset>
<h2>Please Sign In</h2>
<hr class="colorgraph">
<div class="form-group">
<input name="user_id" type="text" id="user_id" class="form-control input-lg" placeholder="Email Address">
</div>
<div class="form-group">
<input type="password" name="password" id="password" class="form-control input-lg" placeholder="Password">
</div>
<span class="button-checkbox">
<button type="button" class="btn" data-color="info">Remember Me</button><!-- Additional Option -->
<input type="checkbox" name="remember_me" id="remember_me" checked="checked" class="hidden">
<hr class="colorgraph">
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<input type="submit" name="Submit" value="Login" class="btn btn-lg btn-success btn-block">
</div>
</div>
</fieldset>
</form>
</div>
</div>
</div>
</body>
</html>
And statistics.html (Page that should only be accessible when logged in)
<?php
include ("login.php")
session_start();
if(!isset($_SESSION['Login']))
{
header("Location:login.php");
die();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Personal Website</title>
<link rel="stylesheet" href="../../CSS/stylesheetmain.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
</head>
<body>
<!--Navigation Bar-->
<div class="row">
<div class="darkgrey column col-md-8 col-md-offset-2 col-xs-12 col-s-12">
<nav class="navbar navbar-background-color">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"><!--Reference: Bootstrap, 2015. getbootstrap.com. [Online] Available at: http://getbootstrap.com/ [Accessed 01 April 2015]-->
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="../index.html"></a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li class="active">Home</li><!--Edits made: Removed active button -->
<li>About Me</li>
<li>Blog</li>
<li>Contact Me</li>
<li>Login</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
</div>
</div>
<div class="row">
<div class="navbarbottom column col-md-8 col-md-offset-2 col-xs-12 col-s-12"></div>
</div>
<!--Page Title-->
<div class="row">
<div class="title mediumbluetext col-md-8 col-md-offset-2 col-xs-0 col-s-0">
<center><h1>Statistics</h1></center>
</div>
</div>
<!--Main Body-->
<div class="row">
<!--Left Column Spacer-->
<div class="maintextleftbackground column col-md-2 col-xs-0 col-s-0">
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-52f8f8c0164b330f" async="async"></script>
</div>
<!--Middle Column-->
<div class="maintext80 column col-md-8 col-xs-12 col-s-8 col-s-offset-2"><br>
<div id="main-chart-container"></div>
<div id="breakdown-chart-container"></div>
<div id="embed-api-auth-container"></div>
<div id="view-selector-container"></div>
</div>
<!--Right Column Spacer-->
<div class="maintextrightbackground column col-md-2 col-xs-0 col-s-0">
</div>
</div>
<!--Footer Bar-->
<div class="row">
<div class="darkgrey column col-md-8 col-md-offset-2 col-xs-12 col-s-12">
<nav class="navbar-background-color">
<div class="container-fluid">
<p class="navbar-text navbar-right">SiteMap</p>
</div>
</nav>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script>
(function(w,d,s,g,js,fs){
g=w.gapi||(w.gapi={});g.analytics={q:[],ready:function(f){this.q.push(f);}};
js=d.createElement(s);fs=d.getElementsByTagName(s)[0];
js.src='https://apis.google.com/js/platform.js';
fs.parentNode.insertBefore(js,fs);js.onload=function(){g.load('analytics');};
}(window,document,'script'));
</script>
</body>
</html>
No matter what I try, I can't seem to get the code to work. The files are in the same directory as well.
Any help with this would be greatly appreciated.
A few things wrong here but you're on the right track.
You are VERY susceptible to SQL injection. Please read about it.
Your form uses POST so your PHP should use $_POST and not $_REQUEST
You cannot set a session variable by passing it through to session_start. You need to set the variable like so: $_SESSION['isLoggedIn'] = true.
You are echoing right before you issue a header command. You cannot output anything before redirecting.
I am trying to get my navbar to change when logged in so that the register and sign in turn into the user's username I have my code for this and I am not understanding why it's not working...
signin.php
<?php
include 'header.php';
include 'ini.php';
$username = "";
$password = "";
if(isset($_POST['login'])) {
if ($userQuery->execute()) {
while ($row = $userQuery->fetch()) {
$username = $_POST["username"];
$password = $_POST["password"];
$userQuery = $pdo->prepare("SELECT * FROM users WHERE username LIKE :username");
$addUserQuery->bindParam(":username", $username);
if (
username == $row['User_Username'] && password == $row['User_Password']
) {
$_SESSION['loggedin'] = "true";
$_SESSION['username'] = $username;
}
}
}
}
?>
<div class="container">
<div class="row">
<div class='col-md-3'></div>
<div class="col-md-6">
<div class="login-box well">
<form action="index.php" id="login" name="login">
<legend>Sign In</legend>
<div class="form-group">
<label for="username">Username</label>
<input value='' id="username" placeholder="Username" type="text" class="form-control" />
</div>
<div class="form-group">
<label for="password">Password</label>
<input id="password" value='' placeholder="Password" type="text" class="form-control" />
</div>
<div class="input-group">
<div class="checkbox">
<label>
<input id="login-remember" type="checkbox" name="remember" value="1"> Remember me
</label>
</div>
</div>
<div class="form-group">
<input type="submit" id="login" name="login" class="btn btn-default btn-login-submit btn-block m-t-md" value="Login" />
</div>
<span class='text-center'>Forgot Password?</span>
<div class="form-group">
<p class="text-center m-t-xs text-sm">Do not have an account?</p>
Create an account
</div>
</form>
</div>
</div>
<div class='col-md-3'></div>
</div>
</div>
<?php
include 'footer.php';
?>
header.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Web Development company providing HTML, CSS, PHP and JS in your local area">
<meta name="author" content="Kieran Brownfield">
<title>Web Development Penzance - Providing Web Development in your area</title>
<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet"/>
<link href="css/main.css" rel="stylesheet"/>
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<header class="header">Web Development Penzance</header>
<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-static-top" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
Home
</li>
<li>
About
</li>
<li>
Contact
</li>
<li>
Gallery
</li>
<li>
Meet the Team
</li>
</ul>
<ul class="nav navbar-nav pull-right">
<?php
if (isset($_SESSION['loggedin'])) { ?>
<li><p class="navbar-text navbar-right">Signed in as: <?php echo $_SESSION["username"] ?> </p></li>
<li>Logout</li>
<li>Account</li>
<?php } else { ?>
<li>Register</li>
<li>Login</li>
<?php } ?>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
In order to access the $_SESSION variable across page loads, add session_start() to the top of both of these files. Docs.
I have 3 files.
1.) The file for displaying it
<div id="tweet">
</div>
this is where it should be displayed.
2.)the php file that gets the data from the database
function ajaxGetTweets()
{
$stmtSelectTweets = $this->db->prepare('SELECT * FROM tweets');
$stmtSelectTweets->setFetchMode(PDO::FETCH_ASSOC);
$stmtSelectTweets->execute();
$data = $stmtSelectTweets->fetchAll();
print_r(json_encode($data));
}
this function return the result in json, i got the right format when i use print_r
3.) the js file for ajax
$( document ).ready(function() {
$.get("homescreen/ajaxGetTweets", function( o )
{
for(var i = 0; i < o.length; i++)
{
$("#tweet").append("<div>"+o[i].tweet+"</div>");
}
}, "json");
});
when leaving the "json" at the end of the $.get function i got a result in my div as undefined, when using "json", i got nothing.
in the console i got a warning like:
Synchronous XMLHttpRequest on the main thread is deprecated because of
its detrimental effects to the end user's experience. For more help,
check http://xhr.spec.whatwg.org/.
Output: <!DOCTYPE HTML>
<html>
<head>
<title>Twitter</title>
<link rel="stylesheet" href="http://localhost/mvc_tut/public/css/bootstrap.min.css">
<script type="text/javascript" src="http://localhost/mvc_tut/public/js/jquery-2.1.4.min.js"></script>
<script type="text/javascript" src="http://localhost/mvc_tut/public/js/bootstrap.min.js"></script>
</script>
</head>
<body>
<div id="header">
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Brand</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>Startseite<span class="sr-only">(current)</span></li>
<li>Über uns</li>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
</div>
<div id="content">
ERROR
</div>
<div id="footer">
Footer
</div>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<title>Twitter</title>
<link rel="stylesheet" href="http://localhost/mvc_tut/public/css/bootstrap.min.css">
<script type="text/javascript" src="http://localhost/mvc_tut/public/js/jquery-2.1.4.min.js"></script>
<script type="text/javascript" src="http://localhost/mvc_tut/public/js/bootstrap.min.js"></script>
<script type="text/javascript" src="http://localhost/mvc_tut/views/homescreen/js/homescreen.js"></script>
</script>
</head>
<body>
<div id="header">
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>Home<span class="sr-only">(current)</span></li>
<li>Notifications</li>
<li>Messages</li>
</ul>
<a class="navbar-brand" href="#">Brand</a>
<form class="navbar-form navbar-left" role="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Search</button>
<button type="submit" class="btn btn-default">Tweet</button>
</form>
<form class="navbar-form" action="homescreen/logout" method="post">
<button type="submit" class="btn btn-default">Logout</button>
</form>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
</div>
<div id="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-4">
<div class="row">
</div>
<div class="row">
</div>
</div>
<div class="col-md-4">
<form class="form-control" id="tweetInsert" action="http://localhost/mvc_tut/homescreen/ajaxInsertTweet" method="post">
<textarea name="tweetText" rows="3" cols="40"></textarea>
<button type="submit" name="submitTweet">Tweet</button>
</form>
<div class="row">
<div id="tweet">
</div>
</div>
</div>
<div class="col-md-4">
<div class="row">
</div>
<div class="row">
</div>
</div>
</div>
</div>
</div>
<div id="footer">
Footer
</div>
</body>
</html>
Let me rephrase it a little:
$.getJSON("homescreen/ajaxGetTweets", function(o) {
var h="";
for(var i=0; i<o.length; i++) h+="<div>"+o[i].tweet+"</div>";
$("#tweet").html(h);
});
This way it's better to understand and a lot faster (because you only write the content once, allowing the browser to render it only 1 time). But your version should also have worked; if it didn't, maybe we need to check out the structure of your JSON to see if that field really exists, etc.
If the returned JSON is not an array, for example, maybe you'll need a different for syntax to walk along it.
UPDATE
GrafiCode Studio was right! You should change the "print_r" to a plain "print" when you return the JSON. Didn't spot that one! He should get the points...