All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Call PHP Function From JavaScript

Last Updated : Mar 11, 2024

Call PHP Function From JavaScript

In this tutorial we will show you the solution of call PHP function from JavaScript, here we used ajax method in javascript for call php file so we need to import jquery cdn link because we need jquery library support.

In html block we just defined two para tags here we passing one para texts to another <p> tag by passing value to javascript then we appends that to another para then we passed it to txt.php for do word wrap process and displayed on webpage.

Step By Step Guide On Call PHP Function From JavaScript :-

Here we defined two para tags with class attributes ‘straightline,wwtxt’, the class ‘straightline’ will have some texts and ‘wwtxt’ had no texts so in javascript we appending class ‘straightline’ text on class ‘wwtxt’ and it pointing data then we calling txt.php by ajax method.

We passing that pointed data to txt.php to do word wrap process and displayed.

In php we using word wrap after 60 characters so class ‘wwtxt’ will looks broken into multi line of texts not like class ‘straightline’.

<!DOCTYPE html>
<html>
    <head>
        <title>CALL PHP FROM JS</title>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    </head>
    <body>
        <p class="straightline">the united states officially the united states of america, commanly known as america is a country primarily located in north amierica consisting</p>
        <p class="wwtxt"></p>
        <script>
            fetch('txt.php',{
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            },
            body: "text=" +document.querySelector("p.straightline").innerText
            })
            .then(response=>response.text())
            .then(data=>document.querySelector("p.wwtxt").innerHTML=data);
        </script>
    </body>
</html>
Txt.php
<?php
$text=$_POST['text'];
$res=wordwrap($text,60,"<br>");
echo $res;
?>
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. Here we need to call php file from javascript so we created html file. In our html file we imported jquery cdn link because we need library support of jquery.
  4. Then we created two <p> tag with class attributes ‘straightline,wwtxt’, in class ‘straightline’ we provided some texts and class ‘wwtxt’ is empty.
  5. Within script we called txt.php file then we passing class ‘straightline’ texts as a parameter so in txt.php file we collecting text and stored to variable ‘$text’.
  6. Then using wordwrap() method we breaking straight line text at 60th character for make multi line of texts and we printed using echo() method.
  7. In script that data response collected and appends that multi line text on class ‘wwtxt’ para tag.

Conclusion :-

In conclusion we are able to know how to call php function from javascript.

First we need to start our xampp server then we load this program on browser we can see result of ‘straightline’ class <p> tag texts and below that we broken multi line paragraph presented.

We can also call php function from javascript for do some other process like calculations or just print something,etc.. but we can use this concept result will be same.

I hope this tutorial on call PHP function from JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪