Is there a way to direct to a desired page by clicking the OK button in a JavaScript pop-up?
I am able to achieve it when not within php but when echoed im not sure.
else {
echo '<script language="javascript">';
echo 'alert("Wrong Username or Password")';
echo '</script>';
header("Location:login.php");
}
This headers OK but i would like it only to happen when the user clicks OK in the popup. Any suggestions?
Thanks
Just use this:
else {
?>
<script language="javascript">
alert("Wrong Username or Password");
location.href = "login.php";
</script>
<?php
}
Just use javascript to do this:
else {
var popup = window.confirm("Wrong Username or Password");
if (popup==true)
{
x="You pressed OK!";
window.location.href = "yourdomain.com/login.php";
}
else
{
x="You pressed Cancel!";
}
}
else {
echo '<script language="javascript">';
echo 'alert("Wrong Username or Password")';
echo 'window.location = "/login.php";'
echo '</script>';
}
should work just fine. It won't redirect until OK is pressed.
Related
I am new to php . and i am trying to pop up an alert box before the page action = "newpage.php" gets executed using the header function , but it isn't working and every time without the alert box being popped up, i get redirected to the next page on successful form submissions.
what should i do to pop an alert box on successful submission of data in my database before going to the next page
here's my code snippet that i have used to do so---->
if($conn->query($insertQuery)){
if($_POST) {
echo '<script> alert("Registered"); </script>';
}
else{
echo '<script> alert("Not"); </script>';
}
header("location:login.php");
if($conn->query($insertQuery)){
if($_POST) {
echo '<script> alert("Registered"); </script>';
}
else{
echo '<script> alert("Not"); </script>';
}
echo '<script> window.location.href = "login.php"; </script>';
Try This
I'm very new to PHP and JavaScript and I'm stuck. I spent a good 7 hours so far trying to figure this one out but I just do not know what or how to do it. So in my application, when you try to add a new row to my table, I have a html form to get the data. When you enter the data into the form, and press submit, it prompts for a password, and if the correct password is put in, it submits the form. It work just as I wanted it, here is what I have:
<button onclick="password()" type="button">Create New Hero</button>
<div>
<a id="discl">*-Required Fields</a>
</div>
<script>
function password(){
var password = prompt("Please Enter the Admin Password");
if(password != "shaun"){
alert("Wrong Password");
}
else{
if(validation()){
document.getElementById("form_id").submit();
}
else{
alert("One or more required fields were left empty, or tried to use <script>");
}
}
}
function validation(){
var f = document.getElementById("f_name").value;
var l = document.getElementById("l_name").value;
var d = document.getElementById("dob").value;
var a = document.getElementById("alias").value;
if(f == "" || l == "" || d == "" || f.includes("<script>") || l.includes("<script>") || a.includes("<script>")){
return false;
}
else{
return true;
}
}
</script>
Now I also have an edit and delete on my table. My problem is that with edit and delete, they are in tags with an href, and they are not buttons. This is how they originally looked when it worked without asking for a password:
echo "<a href='./edit.php?id={$row['id']}'>edit</a>";
So based on which row of the table it was, where you clicked edit or delete, based on the row's id from the database, it would edit or delete that table. Now my problem is I don't know how to properly set up the function so it asks for the password and redirect to the right url. This is what I have right now:
foreach($results as $row){
echo "<tr>";
echo "<td>{$row['first_name']}</td>";
echo "<td>{$row['last_name']}</td>";
echo "<td>{$row['date_of_birth']}</td>";
echo "<td>{$row['alias']}</td>";
echo "<td>{$row['active']}</td>";
echo "<td>{$row['hero']}</td>";
echo "<td>";
echo "<a href='./edit.php?id={$row['id']}'>edit</a>";
echo " | ";
echo "<a href='javascript: password()'>delete</a>";
echo "</td>";
echo "</tr>";
}
?>
<script type="text/javascript">
function password(){
var password = prompt("Please Enter the Admin Password");
if(password != "shaun"){
alert("Wrong Password");
}
else{
window.location.replace("./delete.php?id=<?php echo $row['id']; ?>");
}
}
</script>
The problem with this is that no matter which delete I click on on my table, it always deletes the last one, because the ID it's getting out after the url is the last available ID from my database. I'm not sure if I'm just missing something small, or I'm completely on the wrong path but any help would be appreciated because I'm very lost. Thank you
Will not comment on any security issues here, just the logic. You need to pass $row['id'] during PHP loop to your JS password() function that you want to use afterwards:
<?php
foreach ($results as $row) {
echo "<tr>";
echo "<td>{$row['first_name']}</td>";
echo "<td>{$row['last_name']}</td>";
echo "<td>{$row['date_of_birth']}</td>";
echo "<td>{$row['alias']}</td>";
echo "<td>{$row['active']}</td>";
echo "<td>{$row['hero']}</td>";
echo "<td>";
echo "<a href='./edit.php?id={$row['id']}'>edit</a>";
echo " | ";
echo "<a href='javascript: password(".$row['id'].")'>delete</a>";
echo "</td>";
echo "</tr>";
}
?>
<script type="text/javascript">
function password(delId) {
var password = prompt("Please Enter the Admin Password");
if (password != "shaun") {
alert("Wrong Password");
} else {
window.location.replace("./delete.php?id="+delId);
}
}
</script>
Ignoring the blatant security issue, I suggest this
<a href='#' class='edit' data-id='<?= $row['id'] ?>'>edit</a> |
<a href='#' class='delete' data-id='<?= $row['id'] ?>'>delete</a>
using
window.addEventListerner('load',function() {
document.querySelector('table').addEventListerner('click',function(e) {
e.preventdefault(); // stop the link
const tgt = e.target, id = tgt.getAttribute("id");
if (tgt.classList.contains("edit") {
location = '/edit.php?id='+encodeURIComponent(id);
}
else if (tgt.classList.contains("delete") {
let password = prompt("Please Enter the Admin Password");
if (password === "shaun") {
location.replace('/delete.php?id='+encodeURIComponent(id)=;
}
else {
alert("Wrong Password");
}
}
})
})
It is the target request, not the calling page which requires authentication, otherwise anybody could call the ./delete.php?... url and delete a row in the database.
This is usually done by maintaining a session, which remembers a login of the user. The delete.php request can then check whether the user is already logged in / has admin privileges, or demand the password when necessary.
I would like to redirect users as and when click "OK" in the alert box. Any hints will be appreciated
if( $num_rows == 0 ) {
// No results returned
echo "No results!";
} else {
$message = "Information already sent";
echo "<script type='text/javascript'>alert('$message');</script>";
}
echo "<script type='text/javascript'>
alert('$message');
document.location.href='your url';
</script>";
You can use window.location.href to redirect to any page in javascript, so that it will be redirected to test.php after use click OK in alertbox:
if( $num_rows == 0 ) {
// No results returned
echo "No results!";
} else {
$message = "Information already sent";
echo "<script type='text/javascript'>alert('$message');window.location.href='test.php';</script>";
}
echo "<script>alert('Update Not Successfully');document.location='pagename.php'</script>";
Using window.location.replace(url) is better, than window.location.href = url, because is disallows user to go back at this page using button "previous page in history" in browser.
It does a real redirect instead of just going to the next url:
if( $num_rows == 0 ) {
// No results returned
echo "No results!";
} else {
$message = "Information already sent";
echo "<script type='text/javascript'>alert('$message'); window.location.replace('test.php'); </script>";
}
This is a part of my code. The code is moving to the location, but the echo below is not working.
I am sure this header is affecting it somehow
else {
header('location: index.php');
echo '<script>
document.getElementById("nick").value = "invalid email";
document.getElementById("nick").className = "invalidemail";
</script>';
If you read something about header in php functions, you will came to know that nothing gets printed after header as you are giving instructions to redirect on index.php
However to achieve the same, you can use session variables.
So, on the page where you are redirecting to index.php:
$_SESSION['error']=true;
header('location: index.php');
On index.php, check if session variable is true:
if($_SESSION['error']== true)
{
echo '<script type="text/javascript">
document.getElementById("nick").value = "invalid email";
document.getElementById("nick").className = "invalidemail";
</script>';
}
What I have is php that updates the database field after a client signs a form. This works, but I want it to redirect to a new page after it is confirmed that the user signed it by clicking OK. It they click CANCEL it will leave them on the same page.
<?php
$username = 'joeblow';
require_once ("/mypath/include/connnect_db.php");
?>
<p>Please only submit this after above form is completely signed.</p>
<form id="item_complete" method="post">
<input name="submit" type="submit" form="item_complete" value="Submit After Signing">
</form>
<?php
if(isset($_POST['submit'])) { //if the submit button is clicked
$complete = 'c';
$query = "UPDATE mytbale SET mycolumn='c' WHERE username='$username'";
mysqli_query($con,$query) or die("Cannot Update");
echo "<script> confirmFunction(); </script>";
}
require_once ("/mypath/include/disconnect_db.php");
?>
<script type="text/x-javascript">
function confirmFunction(){
var r=confirm("Confirm that you have signed the form!");
if (r==true){
window.open("http://smartpathrealty.com/smart-path-member-page/");
}
else {
}
}
</script>
My problem is the javascript function does not execute after the php updtates the database.
I appreciate any advice or comments you have about this.
The problem is that you are using separate <script> tags and calling the function before it is defined. These two together do not work well. Also, I'm pretty sure that <script type="text/x-javascript"> does not work anyway since it's outdated and that you want <script type="text/javascript">
You can do the following:
Move function up and fix x-javascript:
<?php
$username = 'joeblow';
require_once ("/mypath/include/connnect_db.php");
?>
<p>Please only submit this after above form is completely signed.</p>
<form id="item_complete" method="post">
<input name="submit" type="submit" form="item_complete" value="Submit After Signing">
</form>
<script type="text/javascript">
function confirmFunction(){
var r=confirm("Confirm that you have signed the form!");
if (r==true){
window.open("http://smartpathrealty.com/smart-path-member-page/");
}
else {
}
}
</script>
<?php
if(isset($_POST['submit'])) { //if the submit button is clicked
$complete = 'c';
$query = "UPDATE mytbale SET mycolumn='c' WHERE username='$username'";
mysqli_query($con,$query) or die("Cannot Update");
echo "<script> confirmFunction(); </script>";
}
require_once ("/mypath/include/disconnect_db.php");
?>
Fiddle: Fiddle
You could post the form via ajax and in your php you can return a response to the javascript. That was you could use a callback to fire the new window.
Heres a jquery example:
$.post('/myscript.php', {foo: 'bar'}).done(function (response) {
window.open(......);
});
I believe you are using same page for landing and updating. What you need is to update your echo to add an onload.
echo "<script> window.onload=confirmFunction(); </script>";
Hope this what you are looking for.
<script type="text/javascript">
function confirmFunction(){
var r=confirm("Confirm that you have signed the form!");
if (r==true){
window.open("http://smartpathrealty.com/smart-path-member-page/");
}
else {
}
}
</script>
<?php
if(isset($_POST['submit'])) { //if the submit button is clicked
$complete = 'c';
$query = "UPDATE mytbale SET mycolumn='c' WHERE username='$username'";
mysqli_query($con,$query) or die("Cannot Update");
echo "<script> confirmFunction(); </script>";
}
require_once ("/mypath/include/disconnect_db.php");
?>
<script type="text/javascript">
window.onload = function(){
<?php
if(submitted){
echo "confirmFunction();";
}
?>
}
</script>
Try:
if (r==true) {
window.open("http://smartpathrealty.com/smart-path-member-page/");
}
else {
window.location = "PAGE THAT YOU WANT";
}