0

this is my JS:

function showErrorMsg() {
    modal.style.display = "block";
}

I want to call that function via php:

if(empty($from) || empty($first_name) || empty($last_name) || empty($_POST['message'])) {
    // CALL showErrorMsg();
}

The PHP line is called whenever I click a button in a mail-form, and it checks if any of the fields are empty.

I tried this:

if(empty($from) || empty($first_name) || empty($last_name) || empty($_POST['message'])) {
        echo "<script type='text/javascript'>showErrorMsg();</script>"
    } else {
        mail($to,$subject,$message,$headers);
        mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
        echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";

    }

But that gave me an HTTP 500 error..

The JS is in its own file called modal.js.

NOTE: The JS is basically just a example. There's alot more confusing stuff in it, so a JS function is needed. Just wanted to clarify this before someone came and gave me tips on how PHP can change style properties. ;)

Jonathan
  • 685
  • 1
  • 10
  • 30
  • 2
    Have you checked your error log? – RGriffiths Mar 16 '17 at 08:25
  • 1
    @RGriffiths It just gives me `PHP Parse error: syntax error, unexpected '}', expecting ',' or ';'` Which doesn't really seem helpful. More like it doesn't even know what's going on, haha. The syntax error is in the echo-line – Jonathan Mar 16 '17 at 08:28
  • [What is the difference between client-side and server-side programming?](http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – hassan Mar 16 '17 at 08:28
  • 1
    JS should not trigger 500. Some other problem like you are missing `;`... – Vishal Kumar Sahu Mar 16 '17 at 08:28
  • I just cannot handle this. I forgot a ; on the echo-line.. Now back to the original question, how can I call the javascript via php? – Jonathan Mar 16 '17 at 08:32
  • Missing ; on echo. The error log seems to tell you exactly what the error is. Very helpful I think. It probably even tells you the line of the error. – RGriffiths Mar 16 '17 at 08:40
  • When you say "The PHP line is called whenever i click a button" how do you call the php? using ajax request ?If so you could simply return a json from the PHP file with status and a message and after the ajax request handle the result and if the status was true you could call the function else display the error message or do whatever you want to notify the user. – knetsi Mar 16 '17 at 09:01

1 Answers1

1

As @hassan linked above in his comment,

What is the difference between client-side and server-side programming?

The problem here is a misunderstanding between a script that is executed server-side vs client-side. Javascript (except NodeJS) is a client-side language. This means it doesn't get executed by the server, and cannot run server code. It is executed by the browser. In other words, all of your Javascript runs immediately AFTER your PHP script(s) complete.

This means that you need to output the Javascript in a way that uses a click listener to invoke an AJAX request to your PHP (server) by sending an HTTP request to one of your other PHP files.

Community
  • 1
  • 1