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
https://forums.phpfreaks.com/topic/258319-variables-and-includes-a-href/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.