pintee Posted May 9, 2013 Share Posted May 9, 2013 (edited) Hi everyone, When I put this in html it works fine: <a href="" onclick="loadPage('page.php'); return false"> Create Character </a><br/>; But when I do this: echo '<a href="" onclick="loadPage('page.php'); return false"> Create Character </a><br/>'; it gives me a syntax error. All I have done is put ' ' around the link. What is the right syntax?? Edited May 9, 2013 by pintee Quote Link to comment https://forums.phpfreaks.com/topic/277838-echo-a-link-in-php-not-working/ Share on other sites More sharing options...
pintee Posted May 9, 2013 Author Share Posted May 9, 2013 Sorry for the double post! Quote Link to comment https://forums.phpfreaks.com/topic/277838-echo-a-link-in-php-not-working/#findComment-1429296 Share on other sites More sharing options...
Barand Posted May 9, 2013 Share Posted May 9, 2013 you are using single quotes inside a string enclosed by single quotes - escape them echo '<a href="" onclick="loadPage(\'page.php\'); return false"> Create Character </a><br/>'; Quote Link to comment https://forums.phpfreaks.com/topic/277838-echo-a-link-in-php-not-working/#findComment-1429298 Share on other sites More sharing options...
pintee Posted May 9, 2013 Author Share Posted May 9, 2013 (edited) Thanks for the reply. OK, so now it parses. Also the link shows up, but when I click on it, the link goes nowhere. I know the link works, because I have tried it outside of php....i.e. just <a href="" onclick="loadPage('page.php'); return false"> Create Character </a><br/> Edited May 9, 2013 by pintee Quote Link to comment https://forums.phpfreaks.com/topic/277838-echo-a-link-in-php-not-working/#findComment-1429299 Share on other sites More sharing options...
cyberRobot Posted May 9, 2013 Share Posted May 9, 2013 (edited) Is loadPage() a PHP function? If so, you need to switch to a client-side language like JavaScript. Sorry, I just reread the original post. What does your current code look like? Edited May 9, 2013 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/277838-echo-a-link-in-php-not-working/#findComment-1429300 Share on other sites More sharing options...
pintee Posted May 9, 2013 Author Share Posted May 9, 2013 No, no....loadPage is javascript. Quote Link to comment https://forums.phpfreaks.com/topic/277838-echo-a-link-in-php-not-working/#findComment-1429301 Share on other sites More sharing options...
pintee Posted May 9, 2013 Author Share Posted May 9, 2013 (edited) the following is functions.js: <script type="text/javascript"> function loadPage(url){ $("#wrapper").empty(); $("#wrapper").load(url, function(){ $("#wrapper").find($('a')).each(function(){ $(this).on('click', function(e){ loadPage($(this).attr('href')); e.preventDefault(); }); }); }); } </script> page.php <?php // First we execute our common code to connection to the database and start the session define('MyConst', TRUE); include('database.class.php'); include('table.class.php'); include('user.class.php'); include('loginattempts.class.php'); include('timer.class.php'); include('functions.php'); include('loginf.php'); $dbo = database::getInstance(); $dbo -> connect("chanology.db.10835750.hostedresource.com", "chanology", "chanology", "XtuV1439!", array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')); secSessionStart(); // At the top of the page we check to see whether the user is logged in or not if(empty($_SESSION['user'])) { // If they are not, we redirect them to the login page. header("Location: login.php"); // Remember that this die statement is absolutely critical. Without it, // people can view your members-only content without logging in. die("Redirecting to login.php"); } // Everything below this point in the file is secured by the login system // We can display the user's username to them by reading it from the session array. Remember that because // a username is user submitted content we must use htmlentities on it before displaying it to the user. ?> <!DOCTYPE html> <html> <head> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"> </script> </head> <body> <a href="page2.php">Link text</a> </body> </html> and the following is private.php <?php // First we execute our common code to connection to the database and start the session define('MyConst', TRUE); include('database.class.php'); include('table.class.php'); include('user.class.php'); include('loginattempts.class.php'); include('timer.class.php'); include('functions.php'); include('loginf.php'); $dbo = database::getInstance(); $dbo -> connect("chanology.db.10835750.hostedresource.com", "chanology", "chanology", "XtuV1439!", array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')); secSessionStart(); // At the top of the page we check to see whether the user is logged in or not if(empty($_SESSION['user'])) { // If they are not, we redirect them to the login page. header("Location: login.php"); // Remember that this die statement is absolutely critical. Without it, // people can view your members-only content without logging in. die("Redirecting to login.php"); } // Everything below this point in the file is secured by the login system // We can display the user's username to them by reading it from the session array. Remember that because // a username is user submitted content we must use htmlentities on it before displaying it to the user. ?> <!DOCTYPE html> <html> <head> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script src="functions.js"></script> </head> <body> <div id="wrapper"> <?php $stmt = $dbo->getConnection()->prepare("SELECT count(character_name) FROM playercharacter JOIN `character` ON (playercharacter.character_id = `character`.character_id) WHERE user_id = :user_id"); $query_params = array(':user_id'=>$_SESSION['user'][user_id]); // Execute the prepared query. $result = $stmt->execute($query_params); $rows = $stmt->fetch(PDO::FETCH_NUM); $createCharacters = 4 - $rows[0]; for($i = 0; $i < $createCharacters; $i++) { echo '<a href="" onclick="loadPage(\'page.php\'); return false"> Create Character </a><br/>'; } // for($i = 0; $i < $rows[0]; $i++) { // echo `<a href="<?php $_SESSION[playerCharacter] = $rows[0]; ?>" onclick="loadPage('page.php'); return false">Create Character</a> <br />`; // } ?> </div> </body> </html> Edited May 9, 2013 by pintee Quote Link to comment https://forums.phpfreaks.com/topic/277838-echo-a-link-in-php-not-working/#findComment-1429302 Share on other sites More sharing options...
Barand Posted May 9, 2013 Share Posted May 9, 2013 If you view the HTML source you should have exactly the same code when it was echoed by PHP Quote Link to comment https://forums.phpfreaks.com/topic/277838-echo-a-link-in-php-not-working/#findComment-1429303 Share on other sites More sharing options...
pintee Posted May 9, 2013 Author Share Posted May 9, 2013 If you view the HTML source you should have exactly the same code when it was echoed by PHP Yes. I did just that. it echoes: <a href="" onclick="loadPage('page.php'); return false"> Create Character </a><br/> So it should work just fine! Quote Link to comment https://forums.phpfreaks.com/topic/277838-echo-a-link-in-php-not-working/#findComment-1429304 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.