Jump to content

onClick to change a php variable?


pleek

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?

Link to comment
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>';


?>

Link to comment
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.

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.