tarleton Posted August 20, 2010 Share Posted August 20, 2010 I have a feeling someone is going to say AJAX but here goes, Currently I have a basic php / html page the goes something like this: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>BASIC HTML PAGE</title> </head> <body> <?php include 'home.inc.php'; ?> </body> </html> Now I've added some links: <li><a href="#">Add</a></li> What I'm after is when someone click on that link is it changes the <?php include 'home.inc.php'; ?> From the base file to <?php include 'add.inc.php'; ?> Help much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/211235-php-function-on-html-link/ Share on other sites More sharing options...
monkeytooth Posted August 20, 2010 Share Posted August 20, 2010 Ajax isn't needed per say.. depends on what your trying to do. Outside of load a different file as the include() file. You can do it through any number of measures that don't require JavaScript. Cookies, Sessions, POST, GET, other conditional variables... However if your looking to click on the link and have it change without loading the page again then yes JavaScript will be part of the equation. if you looking click, then page loads into another page.. you can try structuring your links like index.php?ref=home index.php?ref=add then in the template file your index.php file in this case. add a small script to find what the ref is in the URL and then act accordingly such as if(empty($_GET['ref'])){include 'home.inc.php';} elseif($_GET['ref'] == "home"){include 'home.inc.php';} elseif($_GET['ref'] == "add"){include 'add.inc.php';} else{include 'home.inc.php';} bare in mind this is a rather simplified idea of the concept I am attempting to convey, you will want to cleanse your values a bit before allowing them to run through your scripts. To help prevent people from hijacking your script or injecting something into it. Quote Link to comment https://forums.phpfreaks.com/topic/211235-php-function-on-html-link/#findComment-1101470 Share on other sites More sharing options...
tarleton Posted August 20, 2010 Author Share Posted August 20, 2010 Thankyou so much. I've managed to get that all working except for one problem. I have a php function which calls a text file and allows the user to edit it through their browser (extremely basic) This is that code: <?php // Calls file called records.txt $file_contents = file_get_contents('records.txt'); // creates Save function if(isset($_REQUEST['save'])) { // Creates variable $file_contents which is the contents of the records.txt file $file_contents = $_REQUEST['contents']; // Saves the variable of the $file_contents to records.txt file_put_contents('records.txt',$file_contents); } // Prints the textarea and Save button print "<form> <textarea name=\"contents\" cols='100%' rows='20'>$file_contents</textarea> <br /> <input type=\"submit\" name=\"save\"/ value='Save'>" ; ?> However instead now once I click save it returns to: http://localhost/index.php?contents=whatevercontentwas and of course the include script doesn't allow this. As it just returns u to the homepage, what should I change within the form to make this work? I've tried adding in an action but no luck. Quote Link to comment https://forums.phpfreaks.com/topic/211235-php-function-on-html-link/#findComment-1101475 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.