Good day. my problem is, the javascript alert doesn't work on else statement. In my else statement i want to show a javacript alert that the registration was succesful, but it doesn't appear.. here's my code
<?php
if(isset($_POST['potpot'])){
$file=$_FILES['image']['tmp_name'];
$image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name= addslashes($_FILES['image'] ['name']);
$image_size= getimagesize($_FILES['image'] ['tmp_name']);
if ($image_size==FALSE) { ?>
<script type='text/javascript'>alert('Oppss! Thats not an image!')</script>
<?php
}else{ ?>
<script type='text/javascript'>alert('Wait for admin confirmation! Check your email within 24 hours')</script>
<?php
move_uploaded_file($_FILES["image"] ["tmp_name"], "images/" . $_FILES["image"]["name"]);
$fullname= $_POST['name'];
$course=$_POST['course'];
$yeargrad=$_POST['year'];
$img=$_FILES['image']["name"];
$email=$_POST['email'];
mysql_query("INSERT INTO tblreg(fullname,course,
year_grad,img,email,username,password,status)
VALUES('$fullname',
'$course', '$yeargrad','$img', '$email', 'no', 'no', 'not')");
header('location:reg.php');
}
}
?>
alert does not appear because of redirect (header('location:reg.php'))
Related
I have already made registration and login pages for my web page. The question is that I want to display the name and the image of the user after login, in other words, I want to make some changes in my main page after login.
So this is my code:
lib.php
function checkUser($username,$password){
include "connection.php";
$result = mysqli_query($conn, "SELECT * FROM `data` WHERE `username` = '$username' AND `password` = '$password'") or die("No result".mysqli_error());
$row = mysqli_fetch_array($result);
$logic = false;
if (($row['username'] == $username) && ($row['password'] == $password)) {
echo "Login is successfull. Welcome <b>".$row['username']."</b>";
$logic = true;
}
else{
echo "Failed to login. Username or password is incorrect. Try again.";
}
return $logic;
}
function logIn($username, $password){
include ("connection.php");
$_SESSION["loggedInUser"] = $username;
$_SESSION["loggedInTime"] = time();
}
action.php
<?php
include "lib.php";
if(checkUser($username,$password)){
logIn($username, $password);
header("Location: index_en.php");//goes to my main page
?>
<li>Hello, <?php echo $username; ?></li>
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
console.log("successfull");
$(".nav_header").remove();//I tried to change my index_en.php page, but no changes appear there
</script>
<?php
}
?>
As I understood the question, so I think your code might be like this,
action.php
<?php
include "lib.php";
if(checkUser($username,$password)){
logIn($username, $password);
header("Location: index_en.php");//goes to my main page
?>
<li>Hello, <?php echo $username; ?></li>
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
console.log("successfull");
$(".nav_header").remove();//I tried to change my index_en.php page, but no changes appear there
</script>
<?php
}
?>
As, you want some change in Index_en.php(Home Page) when user is already logged in, your page should contain,
index_en.php
<?php
session_start();
if(isset($_SESSION['username'])){
echo "Hello, ".$_SESSION['username'];
}
?>
I've written a simple login script that connects to a db, and now I want to insert a paragraph with jQuery in my #loginbox which says 'Login failed' when
if (!$row = mysqli_fetch_assoc($result))
is true.
My thought was:
[function.js]
function loginfailed() {
$('#loginbox').html("<p>Login failed.</p>");
}
[login.php]
<head>
<script type="text/javascript" src="functions.js"></script>
</head>
<?php
include '../config.php';
include 'dbh.php';
session_start();
$uid = $_POST['uid'];
$pw = $_POST['pw'];
$sql = "SELECT * FROM user WHERE uid='$uid' AND pw='$pw'";
$result = mysqli_query($conn, $sql);
if (!$row = mysqli_fetch_assoc($result))
{
header('Location: ../index.php');
echo '<script> loginfailed(); </script>';
}
else
{
header('Location: ../index.php');
}
?>
But it doesn't work.
DON'T EVER STORE PASSWORDS IN PLAIN TEXT!!
Regarding your question.
The header function redirects to index.php and does not execute the echo. One solution can be to add a $_GET parameter and after the redirect check if it exists and echo the message or append it with JS.
if (!$row = mysqli_fetch_assoc($result))
{
header('Location: ../index.php?status=fail');
}
In the index.php file at the bottom (if you want to use JS/jQuery to show message)
<script>
var status = "<?php echo (!empty($_GET['status']) && $_GET['status'] === 'fail') ? 0 : 1; ?>";
if(!status) loginfailed();
</script>
Thanks guys, but i've found my own solution with the help of Allkin.
My header now redirects to
header('Location: ../index.php?status=fail');
and my #loginbox checks if status is set and then executes my loginfailed() function.
if(isset($_GET['status'])) {
echo '<script> loginfailed(); </script>';
}
Nothing easy like that!
Thanks for your help everyone.
I want to show JavaScript alert after successful data insertion in MySQL. How to do this? I have written this code but it shows JavaScript alert everytime I open this page and as soon as i click on OK of JavaScript alert it redirects me to finalmem.php, without the action of taking values from users!
$sql="INSERT INTO members VALUES ('$name', '$email', '$ybr', '$ach')";
if(!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
else
{
echo '<script language="javascript">';
echo 'alert("Successfully Registered"); location.href="finalmem.php"';
echo '</script>';
}
Thanks in advance.
Use is set isset($_POST['submit']) to check whether user submits the form or not
<?php
include 'SQLIDB.php';
if(isset($_POST['submit'])){
$name=$_POST['name'];
$email=$_POST['email'];
$ybr=$_POST['ybr'];
$ach=$_POST['ach'];
$sql="INSERT INTO members VALUES ('$name', '$email', '$ybr', '$ach')";
if(!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
else
{
echo '<script language="javascript">';
echo 'alert("Successfully Registered"); location.href="finalmem.php"';
echo '</script>';
}
}
?>
<form action="" method="post">
<input type="text" name="name">
<input type="text" name="email">
<input type="text" name="ybr">
<input type="text" name="ach">
<input type="submit" name="submit" value="Submit">
</form>
You need to know two things, one of them it's to confirm if your data it's saved succefully.
For this you can play with
mysql_affected_rows()
and this function will return the number of rows affected by your query. If zero, it was not inserted. If >= 1, it was.
So:
if ( mysql_affected_rows() >= 1 ){ /* inserted! now do something... */ }
If you are using an auto-incrementing column for row ID, you can use mysql_insert_id() as well:
if ( mysql_insert_id() > 0 ) { /* inserted! now do something... */ }
Then you can work with jQuery UI for show a dialog like this:
[https://jqueryui.com/dialog/][1]
You need tot load the .css and .js files to run jQuery and inside your code put this:
<script>
$(function() {
$( "#dialog" ).dialog();
});
</script>
And this in your view:
<div id="dialog" title="Basic dialog">
<p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
</div>
Normally this it's super ugly to do for me, because the best way it's doing this by AJAX.
You can try like this
<?php session_start();
//Include database detail here
if(isset($_POST['name'])){
$name = mysqli_real_escape_string($_POST["name"]);
$ybr = mysqli_real_escape_string($_POST["ybr"]);
email = mysqli_real_escape_string($_POST["email"]);
$ach = mysqli_real_escape_string($_POST["ach"]);
//Do your data validation here
$_SESSION['sname'] = $name;
$_SESSION['sybr'] = $ybr;
$_SESSION['semail'] = email;
$_SESSION['sach']= $ach;
$sql="INSERT INTO members VALUES ('$name', '$email', '$ybr', '$ach')";
if(!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
} else {
echo '<script language="javascript">';
echo 'alert("Successfully Registered"); location.href="finalmem.php"';
echo '</script>';
}
}
?>
I know this question is very common, I have search alot, and after alot of hard work, I am asking here.
I simply want to show an alert box and redirect it to a page.
The code below is redirecting me, but not showing me the alert box.
Please, let me know my mistake.
<?php
require_once('connection.php');
if(isset($_POST['person'])){ $name = $_POST['person']; }
if(isset($_POST['pwd'])){ $password = $_POST['pwd']; }
if(isset($_POST['position'])){ $pos = $_POST['position']; }
if(empty($name) or empty($password))
{
echo "<SCRIPT type='text/javascript'>
alert('Places should not be empty! Press Ok and try again!');
window.location.replace(\"addmember.php\");
</SCRIPT>";
}
else
{
//$query=mysqli_query($con,"insert into members (username, password,position) values ('$name','$password','$pos')");
echo "<SCRIPT type='text/javascript'>
alert('Added!');
window.location.replace(\"addmember.php\");
</SCRIPT>";
}
?>
I personally make this function to show an alert box and redirect it to a page.
function do_alert($msg,$link=NULL)
{
echo '<script type="text/javascript">
alert("' . $msg . '");
</script>';
if(!empty($link)) {
echo '<script type="text/javascript">
window.location="'.$link.'";
</script>';
}
}
Use this function both alert and redirect to a page use this
do_alert("Message","URL");
do_alert("Here this is your alert","addmember.php");
do_alert("Only your alert");
If the function 2nd parameter URL is empty then it'll only do alert.
try to change
window.location.replace(\"addmember.php\");
to
window.location='addmember.php';
I have a really silly issue i am trying to sort out. I have made this script that displays and deletes users from a database. however my alert function is not working. it performs the action of deleting the content in the database regardless of what I press when the alert appears. here is my code:
<?php include_once "includes/scripts.php"; ?>
<?php include_once "includes/connect.php";?>
<?php include_once "includes/cms_page_security.php";?>
<div id="cms_container"><br>
<br>
<h1>MANAGE USERS<img src="images/three_column_grid_line.png" alt="line"></h1>
<p class="logout_btn">Back</p>
<?php
$tbl="users";
$sql = "SELECT * FROM users";
$result = mysql_query($sql, $connect);
while($rows = mysql_fetch_array($result)){
echo $rows['user_name'];
echo ' ';
echo $rows['user_id']
?>
delete
<?php
} mysql_close();
?>
<script>
function alertFunction()
{
var r=confirm("Do you want to delete this user?");
if (r==true)
{
}
else
{
return (false);
}
}
</script>
</div><!--cms_container-->
</body>
</html>
I would just like the alert function to stop then process of deleting the content when i press cancel.
Change:
onClick="alertFunction()"
to:
onClick="return alertFunction()"
and in the function itself, change the false return to return false;. BTW, since a confirm returns true/false, you can change the first part of the condition to if (r)