Skip to content
Advertisement

run PHP script in JS file

I have minimal knowledge in php and js. Im trying to get value from my form once submit button has been click then trigger my php script.

Js file:

JavaScript

Here’s my php script:

JavaScript

Here’s my form code in index.php. I have 3 fields name, email and message:

JavaScript

I get console.log empty values. Is this the right path or calling php is simply not doable?

Update
It now echos true and message sent.

enter image description here

Advertisement

Answer

Based on this answer, change the way you’re calling the php function.

JavaScript

Also, try using $_REQUEST instead of $_POST in PHP file, as in:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement