Javascript - Always Fires, Want to Fire On Save

59 views Asked by At

In a form when a user clicks save I want a messagebox dialogue to display a confirmation message. These are selected php statements, I don't think the issue is here :

session_start();

if (isset($_POST['Submit']) == 'save')

$_SESSION['message'] = "Record Saved";

In the javascript the messagebox is hidden on click :

<script>
$(function(){
$(document).click(function(){  
$('.messagebox').hide();
});
});
</script>

If I use this php inside the HTML the messagebox is displayed by default on page load even where there is no message to display. Clicking it hides it.

<?php if(isset($_SESSION['message'])){ ?>
<div class="messagebox">
<?php echo $_SESSION['message']; unset($_SESSION['message']); ?>
</div>
<?php } ?>

I've tried the not empty function but the box doesn't load even on save :

<?php if(isset($_SESSION['message']) && !empty($message)): ?>
<div class="messagebox">
<?php echo $_SESSION['message']; unset($_SESSION['message']); ?>
</div>
<?php endif; ?>

css :

.messagebox {
position:absolute;
background:#810000;
color:#ffffff;
border:3px solid #000000;
font-family: 'Montserrat', sans-serif;
font-size:20px;
height:20px;
width:160px;
z-index:1;
padding:12px 0px 12px 0px;
margin:800px 0px 0px 340px;
text-align:center;
border-radius:10px;
}
1

There are 1 answers

1
Lelio Faieta On

I would write this:

if (isset($_POST['Submit']) == 'save')

as

if (isset($_POST['Submit']) && $_POST['submit'] == 'save')

This would fire the event correctly