Jump to content

echo a link in php not working


pintee

Recommended Posts

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 by pintee
Link to comment
Share on other sites

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 by pintee
Link to comment
Share on other sites


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 by pintee
Link to comment
Share on other sites

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!

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.