Jump to content

[SOLVED] changing php variable values through an html link


ag3nt42

Recommended Posts

I'm building a calendar and I have the php building the calendar interface on the fly...

I'm trying to figure out how I can change a variable value through an Onclick event or the like.

 

any ideas would be appreciated.. thankx everyone.

 

so say i have

 

<?php
$z=0;
?>

<a href='#'onclick='<?php echo($z++); ?>'>NEXT MONTH</a>

 

upon click the link the month will change because the variable value has changed.

only thing is i can't get it to work.

 

anyone know of a better way to do this?

Link to comment
Share on other sites

i have tried using javascript to change the value of z but z is also used thorughout a bunch of php functions and loops that i have in place to dynamically build this thing... it doesn't seem to want to let me change the value of a php variable using js

Link to comment
Share on other sites

You can not change an onclick value, at least I don't know how, so I do a work around. First I remove it, then I add it.

 

// I use remove and set attribute
tagname.removeAttribute('onclick');    // attribute name
tagname.setAttribute('onclick',(z+1))  // attribute name followed by its value

Link to comment
Share on other sites

JS is client side and php is server side.... JS CANNOT affect PHP values unless you use AJAX requests.

 

Post your actual code here so we can see it.

 

You won't want to set your value statically every time. You will look to the url and see if a value has been passed. e.g.

 

<?php

if(!isset($_GET['month']))
{
     $z = 0;
}
else
{
    $z = $_GET['month'];
}

?>

If month was passed through the url, then this would use that as the value of z, if not then $z is set statically.

 

Nate

 

Link to comment
Share on other sites

ok well i didn't want to do this but if it will help figure this out

 

<?php
//LIBRARY VARIABLES
$month = $_GET['month'];

$z=0;

$mL= array('January','February','March','April','May','June','July','August','September','October','November','December');

$m=$mL[$z];

$dL= array('31','29','31','30','31','30','31','31','30','31','30','31');

$d=$dL[$z];

$dn= array('Sunday','Monday','Tuesday','Wensday','Thursday','Friday','Saturday');

$y= '2008';

$x=0;



echo ('There are ' . $d .' days in ' . $m . ' ' . $y);

?>


<!-- MONTH/DAYs SWITCHER HTML //-->
<br /><br /><a href='schedule.php?month=<?php echo $z++; ?>'>NEXT MONTH >></a>


<!-- HTML //-->
<table id='dayTable'>
<tr>
<!-- END HTML //-->


<?php
// LIST OUR DAYS NAMES

while($x<=6)
{
echo("<td>" . $dn[$x] . "</td>");
$x++;
}
?>

<!-- HTML //-->
</tr>
<tr>
<!-- END HTML //-->

<?php
// THE VARIABLES

$i=0;
$n="";
$MakeDayB= "<td><div id='Daybox'>";
$MakeDayE= "</div></td>";

// START THE LOOP TO BUILD THE CALENDAR
while($i<=34)
{
echo $MakeDayB . $n . $MakeDayE;
$i++;

// START MONTH SPECIFIC CHECKS
//JAN
if($i>=2&&$y=="2008"&&$m==$mL[0])
{
	$n++;
}

if($i>=$dL[0]+2&&$m==$mL[0])
{
$n="";
}
//FEB
if($i>=5&&$y=="2008"&&$m==$mL[1])
{
	$n++;
}

if($i>=$dL[1]+5&&$m==$mL[1])
{
$n="";
}
//MAR


//BREAK THE CALENDAR DOWN EVERY 7 DAYS
if ($i==7||$i==14||$i==21||$i==28)
{
echo("</tr><tr>");
}
else
{
echo("");
}
}

?>
<!-- HTML //-->
</tr>
</table>

Link to comment
Share on other sites

JS is client side and php is server side.... JS CANNOT affect PHP values unless you use AJAX requests.

 

Post your actual code here so we can see it.

 

You won't want to set your value statically every time. You will look to the url and see if a value has been passed. e.g.

 

<?php

if(!isset($_GET['month']))
{
     $z = 0;
}
else
{
    $z = $_GET['month'];
}

?>

If month was passed through the url, then this would use that as the value of z, if not then $z is set statically.

 

Nate

 

 

I tried this but it doesn't seem to want to change the value of z

Link to comment
Share on other sites

<?php
//LIBRARY VARIABLES
if (!(isset($_GET['month']))){
$z=0;
}
else{
$z = $_GET['month'];
}

$mL= array('January','February','March','April','May','June','July','August','September','October','November','December');

$m=$mL[$z];

$dL= array('31','29','31','30','31','30','31','31','30','31','30','31');

$d=$dL[$z];

$dn= array('Sunday','Monday','Tuesday','Wensday','Thursday','Friday','Saturday');

$y= '2008';

$x=0;



echo ('There are ' . $d .' days in ' . $m . ' ' . $y);

?>


<!-- MONTH/DAYs SWITCHER HTML //-->
<br /><br /><a href='?month=<?php echo $z + 1; ?>'>NEXT MONTH >></a>


<!-- HTML //-->
<table id='dayTable'>
<tr>
<!-- END HTML //-->


<?php
// LIST OUR DAYS NAMES

while($x<=6)
{
echo("<td>" . $dn[$x] . "</td>");
$x++;
}
?>

<!-- HTML //-->
</tr>
<tr>
<!-- END HTML //-->

<?php
// THE VARIABLES

$i=0;
$n="";
$MakeDayB= "<td><div id='Daybox'>";
$MakeDayE= "</div></td>";

// START THE LOOP TO BUILD THE CALENDAR
while($i<=34)
{
echo $MakeDayB . $n . $MakeDayE;
$i++;

// START MONTH SPECIFIC CHECKS
//JAN
if($i>=2&&$y=="2008"&&$m==$mL[0])
{
	$n++;
}

if($i>=$dL[0]+2&&$m==$mL[0])
{
$n="";
}
//FEB
if($i>=5&&$y=="2008"&&$m==$mL[1])
{
	$n++;
}

if($i>=$dL[1]+5&&$m==$mL[1])
{
$n="";
}
//MAR


//BREAK THE CALENDAR DOWN EVERY 7 DAYS
if ($i==7||$i==14||$i==21||$i==28)
{
echo("</tr><tr>");
}
else
{
echo("");
}
}

?>
<!-- HTML //-->
</tr>
</table>

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.