0

I want to execute a JavaScript after update or delete operation. but the problem is the JavaScript does not show up and straight go to other page.

Here is my code:

<?php
session_start();

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("carrental", $con);
$password1 = $_POST['password1'];
$userid =$_SESSION['SESS_MEMBER_ID'];

mysql_query("UPDATE user SET password='$password1' WHERE user_id='$userid'");
echo "<script>alert('Password has been changed.')</script>";
header("location: adminProfile.php");
mysql_close($con);
?> 

I try to use include instead of header and it executed with a Notice. Need some advise..

1
  • You probably want to change the location from js as well after the alert returns Commented Dec 25, 2014 at 7:17

4 Answers 4

1

Try this:

In PHP, header function will not work if you use echo function in whole the page script.

Replace the line:
header("location: adminProfile.php");

with
echo '<script>window.location = "adminProfile.php";</script>';

Let me know if you need more help.

Sign up to request clarification or add additional context in comments.

Comments

1

Use for redirect

<script>
  window.location = "adminProfile.php";
</script>

Comments

0

if you use a location header the browser will receive a redirect request and document.location will change (at which point no javascript can run),

so, do the redirect, or the javascript.

you could try combining a refresh header instead of a location header, or do a javascript redirect after the alert.

Comments

0

header will never work if you will echo anything or try to print anything before that even space.

echo "<script>alert('Password has been changed.')</script>";// do not use this line.before
header("location: adminProfile.php");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.