Note that there are some explanatory texts on larger screens.

plurals
  1. POCorrect way to execute Javascript based on output from PHP
    text
    copied!<p>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. </p> <p>Here's the code: </p> <p>index.php </p> <pre><code>&lt;?php /* Template Name: X */ get_header(); ?&gt; &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt; &lt;title&gt;X&lt;/title&gt; &lt;script src="jquery-1.10.2.min.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div style="margin-top:30px;" align="center"&gt; &lt;form id="myForm" action="sendMessage.php" method="post"&gt; &lt;textarea style="resize:none" cols="80" rows="4" name="message" placeholder="Entrez votre message ici"&gt;&lt;/textarea&gt;&lt;br /&gt; &lt;button style="margin-top:2px; margin-bottom:10px;" id="sub"&gt;Envoyer&lt;/button&gt; &lt;/form&gt; &lt;/div&gt; &lt;script src="sendMessage.js"&gt;&lt;/script&gt; &lt;div id="output"&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; &lt;?php get_footer(); ?&gt; </code></pre> <p>sendMessage.js </p> <pre><code>$("#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); } }); } </code></pre> <p>Basically what I'd like to do is to execute the javascript / ajax with a PHP condition.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload