0

I'm trying to get a function called that calls a php function with an input.

javascript function (picNum is an integer):

function hello(picNum) {
   var pictureNumber = picNum;

   var phpFunc = "<?php 
      include 'otherfile.php';
      otherFileFunc(" + pictureNumber + ") //This is where the problem is, the input(pictureNumber) wont go through
   ?>";
   echo phpFunc;
}

otherfile.php

<?php
   function otherFileFunc($i) {
      $final = $i + 1;
      echo $final;
   }
?>

this code pretty much says if you do onclick="hello(1)" then the output or phpFunc should be 2 because you add one in the otherfile.php, but no matter the input the output is always 1 so I'm guessing the input at where I marked just isn't going through.

DONT TELL ME IT DOESNT WORK BECAUSE IT DOES. if i put an integer instead of " + pictureNumber + " it works perfectly!

any help is appreciated :)

2
  • 3
    You are confused about the difference between server-side and client-side. Put simply there is no way this will work. Read up on Ajax since that's what you need. Commented Apr 3, 2014 at 22:51
  • Once PHP is ran on a page it won't run again. The reason it works perfectly with an integer is because you run the PHP function with a number and the function runs. The pictureNumber variable won't change if you call it from JS since PHP can only run once. Commented Apr 3, 2014 at 23:35

2 Answers 2

1

Unfortunately you won't be able to call php from javascript.

Php is run from the server and javascript is run on a client (usually, the exception being node.js. However even in the instance of node.js, php is not used as javascript has replaced its functionality)

If you need to have javascript "call" a server function you will need to look into ajax requests so that the server can then run a function and return it to the client.

Sign up to request clarification or add additional context in comments.

Comments

1

You have to use Ajax bro:

Javascript:

 function hello(picNum) {
   var pictureNumber = picNum;
   $.ajax({
     url: "otherfile.php",
     data: {"picNum":pictureNumber},
     type:'post',
     dataType:'json',
     success: function(output_string){
       PictureNumber = output_string['picturenumber'];
       alert(PictureNumber);
     }
   });
 }

PHP otherfile.php:

$picNum = $_POST['picNum'];
function otherFileFunc($pic){
  $final = $pic + 1;
  return $final;
}
$outputnumber = function($picNum);
$array = ('picturenumber' => $outputnumber);
echo json_encode($array);

Note: Untested

EDIT, tested:

javascript:

function hello(picNum) {
   var pictureNumber = picNum;
   $.ajax({
     url: "otherfile.php",
     data: {"picNum":pictureNumber},
     type:'post',
     dataType:'json',
     success: function(output_string){
       pictureNumber = output_string['picturenumber'];
       alert(pictureNumber);
     }
   });
 }
hello(1); //sample

PHP otherfile.php:

$picNum = $_POST['picNum'];
$picNum = 1;
function otherFileFunc($pic){
  $final = $pic + 1;
  return $final;
}
$outputnumber = otherFileFunc($picNum);
$array = array('picturenumber' => $outputnumber);

echo json_encode($array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.