pleek Posted November 14, 2008 Share Posted November 14, 2008 ok i have this script <?php $Catagory = "A"; echo' <div id="roms"> <TABLE> <TR> <TD> <div style="height: 390px; width: 200px; overflow: auto;">'; if ($Catagory = "A") { echo 'This is the A\'s!<br />'; } else { echo' This is what will appear when you select "A" as the catagory </div>'; } echo' </TD> <TD VALIGN="TOP"> <B><A HREF="#A">A</A></B><BR> <B>B</B><BR> <B>C</B><BR> <B>D</B><BR> <B>E</B><BR> <B>F</B><BR> <B>G</B><BR> <B>H</B><BR> <B>I</B><BR> <B>J</B><BR> <B>K</B><BR> <B>L</B><BR> <B>M</B><BR> <B>N</B><BR> <B>O</B><BR> <B>P</B><BR> <B>Q-Z</B><BR> </TD> </TR> </TABLE> </div>'; ?> But im totally lost on one thing. I want it so that when you click "B" for igsample. it will change $Catagory to "B". How do i do that? Quote Link to comment https://forums.phpfreaks.com/topic/132652-onclick-to-change-a-php-variable/ Share on other sites More sharing options...
Gighalen Posted November 14, 2008 Share Posted November 14, 2008 Mmm. You could set it up as a form, so that when you submit the page to itset, you just redeclare the variable using the user input. like if they click on a button with a value of B, it will set $catagory (which is category, ftw) to B. Quote Link to comment https://forums.phpfreaks.com/topic/132652-onclick-to-change-a-php-variable/#findComment-689903 Share on other sites More sharing options...
rarebit Posted November 14, 2008 Share Posted November 14, 2008 or use javascript... Quote Link to comment https://forums.phpfreaks.com/topic/132652-onclick-to-change-a-php-variable/#findComment-689904 Share on other sites More sharing options...
foxtrotwhiskey Posted November 14, 2008 Share Posted November 14, 2008 Hey Pleek, You're dealing in two completely different realms. Using OnClick deals with client side scripts (like JavaScript) and PHP is server side. Meaning it generates the HTML that eventually is sent to a users browser. Once that HTML is on the user's browser PHP has no jurisdiction over it. It is then up to the client side scripts to take over. That said there are two ways to do what you want. 1. Drop PHP completely and use JavaScript. Out of the scope for this thread, but if you need help give me a PM. 2. Keep PHP and have B linked to 'scriptname.php?category=B' and the same script will reload, and you can access this variable with $_GET['category'] and do whatever you'd like with it. For Example: <?php $category = $_GET['category']; echo' <div id="roms"> <TABLE> <TR> <TD> <div style="height: 390px; width: 200px; overflow: auto;">'; switch($category) { case 'A': echo 'This is A'; break; case 'B': echo 'This is B!!'; break; case 'C': echo 'This is C!!!!'; break; // and so on } echo' </TD> <TD VALIGN="TOP"> <B><A HREF="#A">A</A></B><BR> <B><A HREF="?category=B">B</A></B><BR> <B><A HREF="?category=C">C</A></B><BR> <B>D</B><BR> <B>E</B><BR> <B>F</B><BR> <B>G</B><BR> <B>H</B><BR> <B>I</B><BR> <B>J</B><BR> <B>K</B><BR> <B>L</B><BR> <B>M</B><BR> <B>N</B><BR> <B>O</B><BR> <B>P</B><BR> <B>Q-Z</B><BR> </TD> </TR> </TABLE> </div>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/132652-onclick-to-change-a-php-variable/#findComment-689908 Share on other sites More sharing options...
pleek Posted November 15, 2008 Author Share Posted November 15, 2008 ok, thanks man. I don't know y i didn't think of doing that esp sense another part of my code uses that. Thanks for the help man, ill try that soon when im at my home pc. Quote Link to comment https://forums.phpfreaks.com/topic/132652-onclick-to-change-a-php-variable/#findComment-690648 Share on other sites More sharing options...
chronister Posted November 15, 2008 Share Posted November 15, 2008 You can essentially combine the 2 technologies with Ajax. It would allow you to run PHP code triggered by a javascript onClick event. There are some pretty good and easy Ajax scripts here on PHPFreaks. But there is no way to directly change php vars with javascript, it would have to be a javascript trigger that uses Ajax to run the php. Quote Link to comment https://forums.phpfreaks.com/topic/132652-onclick-to-change-a-php-variable/#findComment-690658 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.