Jump to content

variables and includes: a href


klutzy

Recommended Posts

Hello everybody, I have a question about changing variables.

 

In a website template I've created, I would like to only include some code from another file into the page if the variable changes. Example:

 

<?php 
$load_page1 = 0; 
?>
<a href="index.php#!/page1" onClick="<?php $load_page1 = 1; ?>">Page 1</a>

<?php 
if($load_page1 == 1)
{ 
include('pages/page1.php'); 
} 
?>

 

The problem I'm having is

onClick="<?php $load_page1 = 1; ?>"

always sets the variable to "1" even if not clicked. So the question is, how would I make it so the default is "0" and only after clicking the link, the variable is set to "1" and then the new code from the other file is included into the page.

 

Please let me know if I need to clarify anything and thanks for any help in advance.

Link to comment
Share on other sites

You have two problems with your code. First, the single = is an assignment operator, not a comparison operator (like == or ===). So you are always assigning 1 to $load_page1.

 

Second, HTML buttons don't work that way with PHP. What you are thinking of is javascript, which will let you assign values to variables, or call functions, etc., with a button click. But PHP will just evaluate the page and assign 1 to that variable (or TRUE or FALSE if you change it to a comparison operator) and insert that into the HTML.

 

scootstah is correct. You need to use AJAX or something javascript-y in order to do what you want.

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.