Redirect to Previous Page After Message - javascript

I want to go back to previous page after pressing "OK" on the function message prompted.
Php Code
function function_alert($message) {
// Display the alert box
echo "<script>alert('$message');
document.location='javascript://history.go(-1)';
</script>";
}
// Function call
function_alert("This email has already subscribed");
}
Alert Box
Thank you so much!

Tried this
function function_alert($message) {
echo "<script>
alert('$message')
history.back()
</script>";
}

Related

Alert Box before PHP action = "newpage.php" gets executed

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

Adding alert button to cart page in woocommerce

I'm trying to add a button under the product name in the cart page for each item which I did, but my alert isn't showing when the button is clicked
function request_button( $product_title, $cart_item, $cart_item_key ) {
$_request = '<div class="request-button"><button type="button" value="save_request" onclick="myFunction()">Request tailormeasurement</button></div>';
return $product_title. '<br/>' . $_request;
if(!$item[$_request]){
?>
<script type="text/javascript">
function myFunction() {
alert("You have requested tailor measurement");
}
</script>
<?php
}
}
add_action('woocommerce_cart_item_name', 'request_button' , 25);
You write return so the function will exit and not continue the rest of code in this function, the if statement that contains tag will not rendered
try use echo or print instead of return here

How to stop messages to show if page refresh when we get a GET parameter?

Hi i am trying to implement notifications when certain event happens in PHP. Suppose a user is changing its password and after the form is submitted the action takes it to update.php, if the password was succesfully changed the page will redirect to change.php?err=1 where 1 has a noty notification which shows password changed succesfully. If there was some problem it redirects to change.php?err=2.
The code for updating password in update.php :-
$sql="UPDATE user_credentials SET password=? WHERE id=?";
$stmt = $result->prepare($sql);
$stmt->bind_param("si", $hash,$id);
if($stmt->execute()) {
header('Location: ../change.php?err=1');
}
else {
header('Location: ../change.php?err=2');
}
The below code for is for showing messages in change.php.
if(isset($_GET['err']))
{
$error_id = $_GET['err'];
if ($error_id == 1) {
echo "<script> noty({text: 'Password Changed Successfully',layout: 'topRight',timeout: 2500,closeWith: ['click', 'hover'],type: 'success'});</script>";
}
else
if ($error_id == 2) {
echo "<script> noty({text: 'Something went wrong',layout: 'topRight',timeout: 2500,closeWith: ['click', 'hover'],type: 'success'});</script>";
}
}
Now if the page is refereshed it will show the message again and again. I want to show the message only when it is redirected not on refreshes.
I thought of a workaround using sessions like this:-
if ($error_id == 1) {
if(!isset($_SESSION['notify'])) {
$_SESSION['notify'] = TRUE;
echo "<script> noty({text: 'Password Changed Successfully',layout: 'topRight',timeout: 2500,closeWith: ['click', 'hover'],type: 'success'});</script>";
}
}
But it stops the message on refreshes but it doesn't show when page is redirected.
I am just starting to learn php so please let me know what I am doing wrong or what else could be better way to solving this problem. Any help is highly appreciated.
Well in addition to
if(isset($_GET['err'])){}
you can add:
if(isset($_GET['err']) && $_SERVER['HTTP_REFERER'] == YOUR_PREV_PAGE){}
In your case it is update.php
This kind of functionality typically works best when you use an ajax call, that means: without refreshing the page.
You could call update.php in the background (see docs of the link pasted above), and decide in the callback of that ajax function what notification to show, based on the response of your update.php script.
EDIT: simple example
$.ajax({
url: "/update-password.php",
data: {
user: "some-user",
pass: "new-pass"
}
}).done(function(response) {
// Show notification, decide on contents based on response
});
Use SESSION instead of GET for error messages. Set an error at the time it's detected, prior to your header() call:
session_start();
$sql="UPDATE user_credentials SET password=? WHERE id=?";
$stmt = $result->prepare($sql);
$stmt->bind_param("si", $hash,$id);
if($stmt->execute()) {
$_SESSION['error'] = "You password was changed.";
header('Location: ../change.php');
}
else {
$_SESSION['error'] = "We could not change your password.";
header('Location: ../change.php');
}
In change.php (and in any other page were a message might be set), do something similar to this:
session_start();
if (strlen($_SESSION['error'])) {
echo $_SESSION['error'];
$_SESSION['error'] = false;
}

Alert msg box display blank page behind

I am displaying a message box at the client browser. It works fine but i see a blank page at the back when this alert message box pops up. But I want to the user to see his browsing page always and pop up this message box. Here is my code.`
if($_POST['submit']=="Save")
{
$language=$_POST['pbx_lan'];
if($language!="")
{
$query="update `account_detail` set `prompt_language`= '$language' WHERE `org_id`='".$_SESSION['account_id']."'";
$result = $mysqli->query($query) or die($mysqli->error);
$_SESSION['check_update'] = "1";
// setcookie("msg","Language Successfully Updated",time()+5,"/");
echo '<script>
confirm("You have selected '.$language.'");
window.location = "'.SITE_URL.'index.php?view=pbx_prompts."
</script>';
// header("location:".SITE_URL."index.php?view=view_service");
}
else
{
setcookie("err","No Language Selected.Please Select Language",time()+5,"/");
header("location:".SITE_URL."index.php?view=view_service");
}
}`
When an confirm() is called the page stops loading. When you add the confirm() at the end of the page, the page is loaded before the confirm() is shown
<body>
TEST123
<script>
confirm('test');
</script>
</body>
instead of
<body>
<script>
confirm('test');
</script>
TEST123
</body>
For your code this means that
echo '<script>
confirm("You have selected '.$language.'");
window.location = "'.SITE_URL.'index.php?view=pbx_prompts."
</script>';
has to move to the bottom of your body tag
Also copy the required if statements so that the code is only executed when neccesary.
The complete code at the bottom of your body tag should be something like this:
if (!empty($language) && $_POST['submit'] == "Save") {
echo '<script>
confirm("You have selected '.$language.'");
window.location = "'.SITE_URL.'index.php?view=pbx_prompts."
</script>';
}

Load JavaScript before Redirecting (CodeIgniter)

This is my code. I already tried, flush(); window_location.. but still it fails. What shall I do? I also tested if the value of $result is being passed and it's fine. The only problem is the JavaScript is not loading. I have other codes with this coding structure but works fine and shows the JavaScript before loading.
function accsettings()
{
$this->load->model('admin_model');
$result = $this->admin_model->accsettings();
if ($result==0)
{
echo '
<script type="text/javascript">
alert("Congratulations! Your profile has been updated.");
</script>
';
$res2 = $this->admin_model->accset_audit();
}
else if ($result==1)
{
echo '<script type="text/javascript"> alert("Error! Current password is not correct."); </script>';
}
else
{
echo '<script type="text/javascript"> alert("New password does not match with the confirmation."); </script>';
}
redirect('/main/accsettings');
}
I would not recommend this way to show 'JavaScript' alert message, if you want to show validation message then please set variable controller and check on view.
In you code you are using headerlocation method to redirect page that is not show up result on browser.
Please use refresh method to show buffer result on browser
redirect('/main/accsettings', 'refresh');
//similar to
header( "refresh:0;url=/main/accsettings" );
You should do the redirect also inside the javascript when you want this to be executed.
Here's how you could achieve this:
<script type="text/javascript">
alert("Error! Current password is not correct.");
setTimeout(function () {
window.location.href = "http://www.google.nl"; //will redirect to google.
}, 2000); //will call the function after 2 secs.
</script>

Categories

Resources