Javascript in PHP

Hi, I have a form and I want to display a pop-up before submitting the data.Problem is I cant call confirmation box at onsubmit or onclick event, as pop-up will appear only if no validation errors are there.

Something like this:

<?PHP if(empty($err_array)){ ?>
			<script>
			var ans = window.confirm('Are you sure you want to submit this form?');
					if(ans == true){ </script>
							<?PHP dbInsert($tbl,$insert_array);
                            echo "<div class='alert alert-success'>
                            <button class='close' data-dismiss='alert' type='button'>&times;</button>
                            Data Inserted
                            </div>";
                            unset($_POST); ?>
					<script>   } </script>
<?PHP } ?>

I am trying to do this since long.Is there any solution to display confirmation window like this?

THis sounds like you would want to use ajax to submit the form data to the php script, have the script check the data for any issues, and then output a response indicating whether the data is good or bad. The web page can then wait for that response, and if it’s good the page can do the confirm, before submitting that form data back php script.