klutzy Posted March 5, 2012 Share Posted March 5, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/258319-variables-and-includes-a-href/ Share on other sites More sharing options...
scootstah Posted March 5, 2012 Share Posted March 5, 2012 If you want to do that without a page refresh you're going to have to use AJAX to dynamically load in the new content. Quote Link to comment https://forums.phpfreaks.com/topic/258319-variables-and-includes-a-href/#findComment-1324165 Share on other sites More sharing options...
batwimp Posted March 5, 2012 Share Posted March 5, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/258319-variables-and-includes-a-href/#findComment-1324168 Share on other sites More sharing options...
klutzy Posted March 5, 2012 Author Share Posted March 5, 2012 Thanks for the responses! Well then, I should probably hurry and finish learning PHP, then move onto Javascript. I guess this is solved! Quote Link to comment https://forums.phpfreaks.com/topic/258319-variables-and-includes-a-href/#findComment-1324246 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.