-1

I need help on how to call an external PHP script from within JavaScript. See below for my example

INDEX.PHP

<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>

<script>
var myName = 'John';
phpResult = /* Need help calling script.php passing is myName and assigning result to phpResult */;

document.getElementById("demo").innerHTML = "The text from the intro paragraph is " + phpResult;
</script>

</body>
</html>

SCRIPT.PHP

<?php
    //script.php
        //Need help adding last name to John (i.e. Smith) and returning John Smith to calling javascript
?>
  • 3
    PHP runs on the server. Its job is to generate the page with the HTML/JavaScript that gets ran by the browser. By the time your browser is running the JavaScript, PHP is long since finished. You need to look into AJAX to make a *new* request to the server. – gen_Eric Jun 25 '14 at 18:29
  • second point is if you WANT TO ADD new name or lAST name then you need to create a form to proced further – M.chaudhry Jun 25 '14 at 18:30
  • possible duplicate of [Call php function from javascript](http://stackoverflow.com/questions/7165395/call-php-function-from-javascript) – Protomen Jun 25 '14 at 18:39

2 Answers2

-1

You can simply write

phpResult = '<?php echo $sometext; ?>';

But,

  • the js script needs to be in a php file. you cannot use this in .js file obviously.
  • this will not evaluate any runtime variables on the client side. because the page will already contain vars rendered by the php script from the server.

I use this method all the time specially for rendering php arrays and objects as js objects/arrays.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
astroanu
  • 3,901
  • 2
  • 36
  • 50
-1

First things first: What are you asking is cannot be done in the way you think. When a page is requested, the server runs the php file, and after that it will send you the javascript/html/css page to you. So when you see the page the php script has already done. Javascript is running on your machine, so that's why you can handle with it user interactions.

But, if you send a post request to the script.php, sending the information you want, then you will be able to interact with a php file. To do this, you need jquery. Here is the script how you can do it in jquery:

$.ajax({
  type: "POST",
  url: "yoursite/script.php",
  data: "name="+myName,
  success: function(data){
phpResult = data;
document.getElementById("demo").innerHTML = "The text from the intro paragraph is " + phpResult;

}
});

You can download jquery from here: http://jquery.com/download/

After you have downloaded, you have to link it to your index.php the jquery.js file, as a normal javascript file.

In your php file, you can access the name with $_POST["name"], and the information you want to send back, you have to actually print it. Something like this:

if(isset($_POST["name"])){
$result = "";
//Do something with the name, and fill accordingly the $result variable
// Something like:
$result = $_POST["name"]." Smith";
echo $result;
}
Geseft
  • 317
  • 1
  • 7