I have a javascript function that takes information from a form and send it to my database. I would like to add a PHP Exception to it. Something like "if "whatever" execute the javascript function, else throw the Exception message". I'm kinda new to javascript and I don't know what is a good way to call a javascript function from a php file.
Here's the code:
index.php
<?php
/*
Template Name: X
*/
get_header(); ?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>X</title>
    <script src="jquery-1.10.2.min.js"></script>
</head>
<body>
<div style="margin-top:30px;" align="center">
<form id="myForm" action="sendMessage.php" method="post">
<textarea style="resize:none" cols="80" rows="4" name="message" placeholder="Entrez votre message ici"></textarea><br />
<button   style="margin-top:2px; margin-bottom:10px;" id="sub">Envoyer</button>
</form> 
</div> 
<script src="sendMessage.js"></script>
<div id="output">
</div>
</body>  
</html>
<?php get_footer(); ?>
sendMessage.js
$("#sub").click( function() {
$.post( $("#myForm").attr("action"),
$("#myForm :input").serializeArray(), 
function(info) { $("#result").html(info); } );
clearInput();
clickSub();     
});
$("#myForm").submit( function() {
    return false;   
});
function clickSub(){
$.ajax({
            url: "showMessage.php",
            type: "POST",
            async: true, 
            dataType: "html",
            success: function(data) {
                $('#output').html(data); 
            }
});
}
Basically what I'd like to do is to execute the javascript / ajax with a PHP condition.
 
     
    