Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
The Code is:
<?php include 'connection.php';
if($logged_in=='yes')
{
echo "hell yea!"; //just for test and it works
?>
<script type="text/javascript">
alert("test 1 2 3"); //works too.
$("#form1").hide(); //to hide login form, but doesn't work
</script>
<?php
}
?>
I think question is quite clear. What is the reason? Thanks in advance.
EDIT: The session events are work fine by connection.php. No problem logging in or logout etc.
Here is my HTML code:
<div id="form1" name="form1" >
<label for="username"></label>
<input type="text" name="username" id="username" placeholder="Username" class="textbox"/>
<label for="password"></label>
<input type="password" name="password" id="password" placeholder="Password" class="textbox"/>
<span id="submit" class="submit">Log in</span>
</div>
<div>
<span id="logged_in" class="logged_in">Hello user etc.</span> //normally it is hidden from css.
</div>
and here is how I login:
$("#submit").click(function(){
$.ajax({
type:"POST",
url:"login.php",
data: '{"username":"'+$("#username").val()+'","password":"'+$("#password").val()+'"}',
dataType: "json",
success: function(data) {
if(data.ok=="done!"){
$("#form1").hide();
$("#giris").show();
$("#giris").html('Hello '+data.name+' '+data.lastname+' My Account');
}
else if(data.ok=="No such user!")
alert("No such user");
else
alert(data.ok); //activation required
}
});
});
EDIT: alert() line. HTML code added. Jquery part is added. And this forum tells me my post is mostly code so I think I have type something here. And I think I just did.
Try this code. Hope it will work.
<?php include 'connection.php';
if($logged_in=='yes')
{
echo "hell yea!"; //just for test and it works
?>
<script type="text/javascript">
$(document).ready(function(){$("#form1").hide(); });
</script>
<?php
}
?>
where dou you get tehe value of the session????
<?php
session_start();
// store session data
$_SESSION['logged_in']='yes';
?>
then
<?php include 'connection.php';
//////if($logged_in=='yes')
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']=='yes')
{
echo "hell yea!"; //just for test and it works
?>
<script type="text/javascript">
$("#form1").hide(); //to hide login form, but doesn't work
</script>
<?php
}
?>
check this link: http://www.w3schools.com/php/php_sessions.asp
I hope this help you men!
Related
im trying to make a facebook style post and comment section but i dont know how to display the data inserted to my database without refreshing the page...
this code is for saving the data to my db. i use window.location.reload(); to reload my page so that the data will be displayed on my page..
<script>
$(document).ready(function() {
$('input[name="mycomment"]').on('keyup', function(e){
e.preventDefault();
var comments = $(this).val();
var sid = $(this).closest("div#userspost").find("input[type='hidden']").val();
if(e.keyCode == 13){
if(comments.length)
$.ajax({
url: "../controller/post_controller.php",
type: "POST",
data:{
"id":sid,
"comments":comments,
},
success: function(data)
{
window.location.reload();
}
});
else
alert("Please write something in comment.");
}
});
});
</script>
using this script i can display my comment on a post i need to refresh the page first for me to be able to show the comment.
<?php
foreach ($post_model->getcomment() as $value) {
if($postid == $value['post_uid']){
?>
<div id="mycomments">
<div class="col-lg-12" style="background:#eff9c7;">
<img src="./<?php echo $value['image']?>" class="pull-left" style="border-radius:50%;margin-top:10px;" width="7%" height="7%" />
<p style="margin-top:18px;line-height:15px;"><strong class="font-1" style="margin-left:10px;"><?php echo $value['firstname'].' '.$value['lastname']?></strong> <?php echo $value['pc_comment']?><br>
<span class="" style="margin-left:10px;font-size:.9em;color:gray;"><abbr class="timeago" title="<?php echo $value['pc_datesend']?>"></abbr></span>
</p>
</div>
</div>
<?php
}
}
?>
what im trying to do is that this is where i want to display my comments from my db. i tried researching about append/load but i dont exactly know how this works. is there any idea that i can display my comment in this script?
<div id="mycomments">
<div class="col-lg-12" style="background:#eff9c7;">
<img src="./<?php echo $value['image']?>" class="pull-left" style="border-radius:50%;margin-top:10px;" width="7%" height="7%" />
<p style="margin-top:18px;line-height:15px;"><strong class="font-1" style="margin-left:10px;"><?php echo $value['firstname'].' '.$value['lastname']?></strong> <?php echo $value['pc_comment']?><br>
<span class="" style="margin-left:10px;font-size:.9em;color:gray;"><abbr class="timeago" title="<?php echo $value['pc_datesend']?>"></abbr></span>
</p>
</div>
</div>
I decided to write some pseudo code for you here. If you don't know how to store and fetch comments I would recommend looking into MYSQL. It's relatively simple (for simple things), so that shouldn't be too big of a problem. YouTube tutorials will be your blessing there.
You should have at least three files to properly implement this:
uploadComment.php
<?php
//process the comment upload
echo $_POST['comment'];
?>
getComment.php
<?php
//however you serve commnets, MYSQL, maybe?
//make sure it's properly formatted with HTML
?>
index.html
<form>
<input type="text" name="comment" id="comment" value="Comment here" />
<button id="submit">Submit</button>
</form>
<div id="comments">
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
$("#submit").click(function () {
$.post("uploadComment.php", {
comment : $("#comment").val()
}, function (data) {
//comment posted.
refreshComments();
});
});
function refreshComments() {
$.get("getComments.php", function(data) {
$("#comments").html(data);
});
}
setInterval(refreshComments,5000);
</script>
Note: Although it may be annoying, if you want immediate satisfaction, append the new comment to the end and then invoke refreshComments. (But I wouldn't recommend doing this because it would force you to update multiple locations in your code whenever you change your comment HTML format).
I have a problem here which I have solved with two different solutions. I would like to know:
Which solution is the most secure?
Which solution is more efficient, basically which one requires the least bandwith for user?
Which solution in the long run is easiest to maintain?
Is there a possibility that any of the solutions might get outdated in the near future? Example if they change how jQuery .hide() works or anything else, whatever.
Here are my solutions:
Solution 1 with jQuery and some php.
jQuery Code:
$(document).ready(function(){
$.get( "hide_forms_until_logged_in.php", function( data ) {
console.log(data.response);
if(data.response == "false") {
$("form").hide();
} else {
$("form").show();
}
}, "json");
});
HTML markup:
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="file" /> <br/>
<input type="submit" name="submit" value="Upload"/>
</form>
hide_forms_until_logged_in.php:
include 'session.php';
if (isset($_SESSION['login_user'])) {
echo json_encode(array("response"=>"true"));
} else {
echo json_encode(array("response"=>"false"));
}
Solution 2 with mostly php:
hide_forms_until_logged_in.php:
include 'session.php';
if (isset($_SESSION['login_user'])) {
echo "<form action='' method='POST'
enctype='multipart/form-data'>
<input type='file' name='file' /> <br/>
<input type='submit' name='submit' value='Ladda upp'/>
</form>";
} else {
echo "Logga in for att ladda upp filer";
}
And inside the page:
<?php include "hide_forms_until_logged_in.php" ?>
I know I could have made a function and call it inside the include there, for the sake just put the include there.
The form itself before posting anything to database has a check if the user is logged in or not.
This question already has answers here:
jQuery AJAX submit form
(20 answers)
Closed last year.
I am trying to post a data on run time and I want to see this data same time. But I don't understand clearly how can I do. I think my problem is I could not choose the element correctly. What is wrong on my code ? Thanks all from now :)
My Error is : Notice: Undefined index: username in C:\wamp\www\eva\check.php on line 3
Line 3 : echo $username = $_POST["username"];
form.php
<html>
<head>
<title>jQuery</title>
</head>
<body>
<div class="formwrapper">
<form method="post" action="" name="form">
<input type="text" id="rname" class="inputa" name="uname" placeholder="Username">
<div id="feedback"></div>
</form>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script src="myScript.js"></script>
</body>
</html>
check.php
<?php
echo $username = $_POST["uname"];
?>
myScript.js
$(document).ready(function() {
$("#feedback").load("check.php").show();
$("#rname").keyup(function() {
$.post("check.php", { username: uname.value },
function(result){
$("#feedback").html(result).show();
})
});
});
You are POSTing the value as username and trying to retrieve it as uname. Rename your key in PHP:
$username = $_POST['username']
It's probably also a good idea to check if the value exists and return a nice error message instead of letting your code fail.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have an image upload script using PHP with a simple multiple file select and then an upload function like below:
mysql_connect("localhost", "root", "") or die("error");
mysql_select_db("repo") or die("error");
$imgerror = '';
if(isset($_POST['log'])){
foreach($_FILES['files']['tmp_name'] as $key => $name_tmp){
$name = $_FILES['files']['name'][$key];
$tmpnm = $_FILES['files']['tmp_name'][$key];
$type = $_FILES['files']['type'][$key];
$size = $_FILES['files']['size'][$key];
$dir = "content/images/".$name;
$move = move_uploaded_file($tmpnm,$dir);
if($move){
$hsl = mysql_query("insert into files values('','$client','$name','$type','$size',now())");
if ($hsl){
$imgerror = "IMAGE(S) UPLOADED SUCCESSFULLY";
} else {
$imgerror = "CANNOT CONNECT TO DATABASE";
}
} else {
$imgerror = "NO IMAGES SELECTED";
}
}
}
HTML:
<div class="uploadContainer">
<div><i><?php echo $imgerror ?></i></div>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="files[]" multiple>
<input type="submit" name="log" value="Upload">
</form>
</div>
I did find some information using jQuery from another question here but have no idea how I would implement this into my code, or maybe someone could suggest an alternative. All I am trying to do is select the files, and make it automatically submit without pressing a submit button.
Any help would be appreciated,
Thanks
The onchange event works for inputs type file. Next code auto-submits "once files have been selected" (tested in Mozilla Firefox) :
<html>
<head>
<script type="text/javascript">
function on_change ()
{ alert( "File(s) chosen!" +
"\n\n" +
"Click to submit files to upload." );
document.getElementById( "frm" ).submit();
}
</script>
</head>
<body>
<form id="frm" action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="files[]" multiple onchange="on_change()">
<input type="submit" name="log" value="Upload">
</form>
</body>
</html>
Of course, you will have to remove the JavaScript "alert" window, it's there to show that the "onchange" event works.
I am trying to make a basic login screen using Javascript, in which I don't have much experience. I managed to get this much, but for some reason, whenever I enter text into the input box it will redirect me, no matter whether it is the correct password or not.
Also, a second question, to determine whether or not a user has logged in (to redirect them to the login page), would I have to use something other than JS, such as PHP?
Code:
<!doctype html>
<html>
<head>
<title>Login</title>
<script type="text/javascript">
function login()
{
if (password = "cat")
{
location.assign("home.html");
}
}
</script>
</head>
<body>
Password: <input type = "password" name = "password">
<input type = "button" value = "Login" onclick = "login()">
</body>
</html>
currently you are using if(password = "cat") which is actually an assignment operator not used for comparison. If you don't know the type of data to be compared use == otherwise you can use ===.
In otherword
== is used to compare the values only.
=== is used to compare the values as well as type.
Check like this
if(password === "cat")
And for your another query if you want to make a web app or something. You should use server side interaction using PHP or WPF or any other like JSON etc.
This question has been asked by many before. The code needs to use an Ajax request to the server.
Also , you do need a server side language to do the validation . Something like below, example is using Jquery library
HTML
<body>
<?php session_start(); ?>
<div id="profile">
<?php if(isset($_SESSION['user_name'])){
?>
<a href='logout.php' id='logout'>Logout</a>
<?php }else {?>
<a id="login_a" href="#">login</a>
<?php } ?>
</div>
<div id="login_form">
<div class="err" id="add_err"></div>
<form action="login.php">
<label>User Name:</label>
<input type="text" id="user_name" name="user_name" />
<label>Password:</label>
<input type="password" id="password" name="password" />
<label></label><br/>
<input type="submit" id="login" value="Login" />
<input type="button" id="cancel_hide" value="Cancel" />
</form>
</div>
<div id="shadow" class="popup"></div>
</body>
Javascript
<script type="text/javascript">
$(document).ready(function(){
$("#login_a").click(function(){
$("#shadow").fadeIn("normal");
$("#login_form").fadeIn("normal");
$("#user_name").focus();
});
$("#cancel_hide").click(function(){
$("#login_form").fadeOut("normal");
$("#shadow").fadeOut();
});
$("#login").click(function(){
username=$("#user_name").val();
password=$("#password").val();
$.ajax({
type: "POST",
url: "login.php",
data: "name="+username+"&pwd="+password,
success: function(html){
if(html=='true')
{
$("#login_form").fadeOut("normal");
$("#shadow").fadeOut();
$("#profile").html("<a href='logout.php' id='logout'>Logout</a>");
}
else
{
$("#add_err").html("Wrong username or password");
}
},
beforeSend:function()
{
$("#add_err").html("Loading...")
}
});
return false;
});
});
</script>
PHP code
<?php
session_start();
$username = $_POST['name'];
$password = md5($_POST['pwd']);
$mysqli=mysqli_connect('localhost','username','password','database');
$query = "SELECT * FROM user WHERE username='$username' AND password='$password'";
$result = mysqli_query($mysqli,$query)or die(mysqli_error());
$num_row = mysqli_num_rows($result);
$row=mysqli_fetch_array($result);
if( $num_row >=1 ) {
echo 'true';
$_SESSION['user_name']=$row['username'];
}
else{
echo 'false';
}
?>
This is really the wrong way to implement login. Any password checking has to happen on the server, because otherwise the password will be visible on the client, and it will be trivial for users to see the required password. Furthermore, please, please, please, please do not create yet another username/password login... this is what OAuth2 is intended to solve; let the identity experts handle login, and simply delegate to another identity provider.
That being said, the error in your code is that you use a single equals sign ("=") which performs assignment, whereas you really intend to use the double equals ("==") or triple equals ("===") for comparison.