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 he
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)
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()"
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";
}
}
}
?>
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.
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.
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.