Jump to content

Archived

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

pleek

onClick to change a php variable?

Recommended Posts

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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>';


?>

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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