Jump to content

Calling ajax function from from php file


fredted40x

Recommended Posts

Hi all,

 

Im trying to add AJAX to my mini message board, the basic pages of my message board are

-Homepage (index.php)

-View message subject page - shows message subjects from every message stored in the database and then include a link to view the full message

-Message page- this page shows the full message.

 

 

So far i have made two links on the left menu bar, one being to show the homepage in the div 'textone', and the other showing the message subjects page with the message sujects and a link next to each subject that people can click on to view the whole message.

 

So far this has worked with both links calling a seperate function that canges the div 'textone' innerHTML. Both AJAX functions calls seperate .php files, one including the homepage text and the other including the SQL code to display message subjects.

 

Im now trying to add a link next to all message subjects that calls a function in script.js, but im trying to call it from one of the php files that ajax has called and its not working as the ajax is only linked to the index.php file, is it possible to call a function in a different php file.

 

Heres a bit of code

 

index.php

<a href="#home" onclick="home()">Home</a><br/>
	<a href="#board" onclick="fetch()">Bulletin Board</a><br/>

 

and i have this

<script src="script.js"></script>

at the top in the HTML head.

 

 

The script.js has this code in it that calls the page to show bulleting subjects

 


var fetch = function () {
var xhr, target, changeListener;
target = document.getElementById("dynamicText1");
xhr = new XMLHttpRequest();	

changeListener = function () {
	if (xhr.readyState === 4) {
		if (xhr.status === 200) {
			target.innerHTML = xhr.responseText;
		} else {
			target.innerHTML = "<p>Something went wrong.</p>";
		}
	} else {

	}
};


xhr.open("GET", "bulletinindex.php");
xhr.onreadystatechange = changeListener;
xhr.send();
};

 

and the bulletinindex.php has this

 

 

	    $sql = mysql_query("#removed sql query#");
	while($r = mysql_fetch_array($sql)) {  
          $posted = date("jS M Y h:i",$r[posted]); 
	  echo "$r[bulletin]<br/> ";
	  $result = " <a href="#home" onclick="home()">Home</a><br/><hr>";
	  echo $result;
	}

 

 

 

i included the code

 <a href="#home" onclick="home()">Home</a><

just so i can try and get it working as i know the home function works correctly so once i get that working i can just call a seperate function to view the message, i thought it just added  the text into index.php so it would be like that link was actually in the index.php file but it doesnt work and says there is a error on the line

 

echo $result;

.

 

Any one got any ideas how i can get it to update textone in index.php?

 

Thank you!

 

p.s. sorry if this is too confusing

 

Link to comment
Share on other sites

sorry it wont let me modify my last post.

 

Hae just thought of another way of explaining it.

 

Basically i want a php file called from AJAX which is called from index.php to be able to make index.php call a function as if the user had clicked a link on index.php that calls a function

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.