0

I have searched for almost similar questions but none of those gave me the right answer. I have a fully working file_exist code in if else statement here. but when i placed it inside a function it doesn't work anymore. Here's the code:

if (file_exists($_SERVER['DOCUMENT_ROOT']."/Project/events/folder-01/event-01.txt")) {  

            $myFile = ($_SERVER['DOCUMENT_ROOT']."/Project/events/folder-01/event-01.txt");
            $fh = fopen($myFile, 'r');
            $theData = fread($fh, filesize($myFile));
            fclose($fh);

            echo $theData;

         echo "The file exists." ;                      
    }

    else {
        echo "The file $filename does not exist";
    }

When I place it inside a function which isn't working:

function readexisting(){
    if (file_exists($_SERVER['DOCUMENT_ROOT']."/Project/events/folder-01/event-01.txt")) {  

        $myFile = ($_SERVER['DOCUMENT_ROOT']."/Alchemy/events/folder-01/event-01.txt");
        $fh = fopen($myFile, 'r');
        $theData = fread($fh, filesize($myFile));
        fclose($fh);

        echo $theData;

         echo "The file exists." ;                      
    }

    else {
    echo "The file $filename does not exist";
    }

}

Also, I want to call the function in image click event. here's the code if it would help:

<div class="Thumb popup_element shadow clearfix" id="u2413"><!-- group -->
    <img class="grpelem" id="u2471" alt="This Week's Events" src="images/blank.gif" onclick="readexisting()"/><!-- state-based BG images -->
   </div>

I hope you could help me with this. thank you so much in advance!

2
  • 2
    Are you trying to call php function in js event? Good luck Commented Sep 30, 2013 at 8:25
  • Hi Alma, no there is no js event here. Can I use js event? would that be a better way? thanks Commented Sep 30, 2013 at 8:38

6 Answers 6

2

With onclick= you assign a JavaScript event handler, not a PHP function.

I don't know what your code is supposed to do, but perhaps you can execute the function when something is POSTed to your file.

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

1 Comment

if i create a JS function like function callPHP() how can this call the other php function? please show me some codes. thanks
2

You are trying to call a PHP function the same way as a JavaScript function. This just doesn't work.

onclick="readexisting()"

You can only call JavaScript functions like this.

1 Comment

can you show me how to create a JS function that would call the php function? and how can i place the working code inside the php function to be called? thanks a lot!
0

onclick= is used to call javascript function or to run javascript code you can not call a php function using onclick="", but as you are saying you are getting desired output without function is just because when you clicked on image it just render the same page having your first code and when you moved this code to a function, and as functions does not run by its own you need to call them thats why in second case you did not get expected output.

If you want to call php function using javascript use AJAX

Calling a PHP function using javascript

Create a test folder ex. ajax (yourhost/ajax/)

view.php (yourhost/ajax/view.php)

<html>
    <head>
        <title>Simple AJAX Example</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    </head>
    <body>
        <div class="Thumb popup_element shadow clearfix" id="u2413"><!-- group -->
          <img class="grpelem" id="u2471" alt="This Week's Events" src="images/blank.gif" onclick="readexisting()"/><!-- state-based BG images -->
        </div>

        <script>
            function readexisting() {
                 jQuery.ajax({
                    type: "POST",
                    url: 'controller.php',
                    data: {action: 'readexisting', arguments: 'your data'}, 
                    success:function(data) {
                                         data = data.split("~:~");
                                         alert(data[0]); // message
                                         alert(data[1]); // content
                    }
                });
            }
        </script>
    </body>
</html>

controller.php

<?php
    include_once("model.php");

    $obj = new Model();

    switch($_POST["action"]){ 
        case 'readexisting': 
            $obj->readexisting();
        break;      
    } 
?>

model.php

<?php
class Model {

   public function readexisting() {
       if (file_exists($_SERVER['DOCUMENT_ROOT']."/Project/events/folder-01/event-01.txt")) {  
            $myFile = ($_SERVER['DOCUMENT_ROOT']."/Project/events/folder-01/event-01.txt");
            $fh = fopen($myFile, 'r');
            $theData = fread($fh, filesize($myFile));
            fclose($fh);

            echo "The file exists. ~:~".$theData ;                      
       } else {
            echo "The file $filename does not exist";
       }
   }

}
?>

9 Comments

thanks for explaianing. now i'm clear about the problem. can you suggest a way or code so i can place a js event/function in the "onclick =" wherein the js function calls the working code? shoudl I place the working code in a php function? thanks a lot! I appreciate your answer :)
can i call php function by using js function alone? i have'nt tried using AJAX yet. thanks
please check the updates answer I will update it more it does not help you.
thanks Subodh. i will try and test your code now. i'll let you know about the result shortly thanks again
Hi again, i tried it but nothing happens when i click the image. hope you can still help me with this. thanks
|
0

You can't call the PHP function from HTML/JS. PHP already finished it's work on server side when HTML/JS start working on the client side.

About file_exists - it doesn't change it's behavior in- or outside of function. So the problem should be in fil path. MAy be it's bacause you have /Project/events/folder-01/event-01.txt in the 1st case and /Alchemy/events/folder-01/event-01.txt in the 2nd? (Alchemy != Project)

1 Comment

Hi I changed the path so it would be the same. How can i place the working code in a php function? thanks
0

It's not possible to call a PHP-Function with onclick (directly) without reloading/loading the page.

But you can use <a> tag for alternative solution and call that PHP function.

2 Comments

@Kaye, that would depend on your situation. If you don't want to load/reload your page then the best suggestion for you is to use jquery and ajax if not then you can give you a sample
can you give me a sample? plaese feel free to edit my code. thanks again
0

you can't call the PHP function From HTML tag like Javascript Function. you have to change your function to javascript function to invoke it like this

onclick="readexisting()"

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.