...

View Full Version : How to remember selected values in all dropdowns?



Bra1nSOFT
10-10-2012, 06:37 PM
Hello,

I have a plugin, which is calculating pregnancy weeks. But I want it to save the selected values after user has selected the submit button. Because now, when user clicks on submit button, all the values reset.

Here's the code


function pregwcalc_add_page()
{
add_submenu_page('plugins.php', 'Pregnancy week calculator', 'Pregnancy week calculator', 8, __FILE__, 'pregwcalc_options');
}

// pregwcalc_options() displays the page content for the pregwcalc Options submenu
function pregwcalc_options($widget_mode=false)
{
// Read in existing option value from database
$pregwcalc_table = stripslashes( get_option( 'pregwcalc_table' ) );

// See if the user has posted us some information
// If they did, this hidden field will be set to 'Y'
if( $_POST[ 'pcalc_update' ] == 'Y' )
{
// Read their posted value
$pregwcalc_table = $_POST[ 'pregwcalc_table' ];


// Save the posted value in the database
update_option( 'pregwcalc_table', $pregwcalc_table );

// Put an options updated message on the screen
?>
<div class="updated"><p><strong><?php _e('Options saved.', 'pregwcalc_domain' ); ?></strong></p></div>
<?php
}

// Now display the options editing screen
echo '<div class="wrap">';
// header
echo "<h2>" . __( 'Pregnancy week calculator Options', 'pregwcalc_domain' ) . "</h2>";
// options form
?>

<?php if(!$widget_mode):?>
<form name="form1" method="post" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>">
<?php endif;?>
<input type="hidden" name="pcalc_update" value="Y">

<p><?php _e("<p>You can use this calculator in two ways: as a standard Wordpress widget or by placing it in your post or page. For the latter please include the tag <b>[pregnancy-calculator]</b> in the content of your page or post and the calculator will appear there.</p>
<p>These options are accessible both from the \"Pregnancy calculator\" page under your Plugins menu or from your Widgets section.</p>
<p>Check out some more of our <a href='http://calendarscripts.info/free-calculators.html' target='_blank'>free calculators</a>.</p>
<p>CSS class definition for the predictor wrapper div &lt;div&gt;:</p>", 'pregwcalc_domain' ); ?>
<textarea name="pregwcalc_table" rows='5' cols='70'><?php echo stripslashes ($pregwcalc_table); ?></textarea>
</p><hr />

<?php if(!$widget_mode):?>
<p class="submit">
<input type="submit" name="Submit" value="<?php _e('Update Options', 'pregwcalc_domain' ) ?>" />
</p>

</form>
<?php endif;?>
</div>
<?php
}

function pregwcalc_datechooser($name,$value="")
{
$daySelected = '';
if (isset($_POST['day'])) { $daySelected = $_POST['day']; }


$months=array('','Sausis','Vasaris','Kovas','Balandis','Gegužė','Birželis','Liepa','Rugpjūtis',
'Rugsėjis','Spalis','Lapkritis','Gruodis');

if(empty($value)) $value=date("Y-m-d");

$parts=explode("-",$value);

$day=$parts[2]+0;
$month=$parts[1]+0;
$year=$parts[0];

$chooser="";

$chooser.="<select name=".$name."day>";
for($i=1;$i<=31;$i++)
{
if($i==$day) $selected='selected';
else $selected='';
$chooser.="<option $selected>$i</option>";
}
$chooser.="</select> ";

$chooser.="<select name=".$name."month>";
for($i=1;$i<=12;$i++)
{
if($i==$month) $selected='selected';
else $selected='';
$chooser.="<option $selected value=$i>$months[$i]</option>";
}
$chooser.="</select> ";

$chooser.="<select name=".$name."year>";
for($i=(date("Y")-1);$i<=(date("Y")+1);$i++)
{
if($i==$year) $selected='selected';
else $selected='';
$chooser.="<option $selected>$i</option>";
}
$chooser.="</select> ";

return $chooser;
}

function pregwcalc_generate_html()
{
//construct the calculator page
$prcalc="<style type=\"text/css\">
.pregwcalc_table
{
".get_option('pregwcalc_table')."
}
</style>\n\n";

if(!empty($_POST['calculator_ok']))
{
//last cycle date
$date="$_POST[dateyear]-$_POST[datemonth]-$_POST[dateday]";

//convert to time
$lasttime=mktime(0,0,0,$_POST[datemonth],$_POST[dateday],$_POST[dateyear]);

//first fertile day
$firstdaytime=$lasttime + $_POST[days]*24*3600 - 16*24*3600;
$firstday=date("Y.m.d",$firstdaytime);

//last fertile day
$lastdaytime=$lasttime + $_POST[days]*24*3600 - 12*24*3600;
$lastday=date("Y.m.d",$lastdaytime);

//current date
$currentDate = strtotime(date("Y-m-d"));

//pregnancy week
$pregnancyweektime=$currentDate - $lasttime;
$pregnancyweeknumber=$pregnancyweektime / 86400 / 7;
$pregnancyweek=ceil($pregnancyweeknumber);

//have to adjust due date?
$diff=$_POST[days] - 28;

//due date $date + 280 days
$duedatetime=$lasttime + 280*24*3600 + $diff*24*3600;
$duedate=date("Y.m.d",$duedatetime);

//due date in days
$duedateindaystime= $duedatetime - $currentDate;
$duedateindaysvalue=$duedateindaystime / 86400;
if ( $duedateindaysvalue <= 0 )
{
$duedateindays = 0;
}
else {
$duedateindays=$duedateindaysvalue;
}


//the result is here



$prcalc.='<div class="nestumo-skaiciuokle">
<form method="post">
Pirmoji paskutinių mėnesinių diena:<br /><br />
'.pregwcalc_datechooser("date",date("Y-m-d")).'<br><br>
Mėnesinių ciklo trukmė: <select name="days">';

for($i=20;$i<=45;$i++)
{
if($i==28) $selected='selected';
else $selected='';
$prcalc.="<option $selected value='$i'>$i</option>";
}

$prcalc.='</select>
<p align="left"><input type="submit" name="calculator_ok" value="Skaičiuoti"></p>
</form>
</div><br />';

if ($duedateindaysvalue < -7 )
{
$prcalc.='<div class="pregwcalc_table">
<h2> Vaikelis turėjo gimti </h2>
</div>';
}

if ($duedateindaysvalue >= -7 & $pregnancyweeknumber <= 41 &$pregnancyweeknumber >= 5 )
{

$prcalc.='<div class="nestumo-skaiciuokle">

<p> <a href="http://www.mamuturgus.lt/nestumas/nestumo-kalendorius/savaite/'.$pregnancyweek.'/">
<img class="nestumo-savaites-nuotrauka" src="http://www.mamuturgus.lt/wp-content/uploads/2012/05/nestumo-savaite-'.$pregnancyweek.'.jpg" height="150" width="180"></a>
<a class="nestumo-savaite" href="http://www.mamuturgus.lt/nestumas/nestumo-kalendorius/savaite/'.$pregnancyweek.'/"> '.$pregnancyweek.' nėštumo savaitė, iki gimdymo liko '.$duedateindays.' dienų. </a>
<br />
<br />
Apytikslė gimdymo data: <strong>'.$duedate.'.</strong>
<br />
<br />
<br />
<span class="nestumo-savaites-aprasymas"> <a href="http://www.mamuturgus.lt/nestumas/nestumo-kalendorius/savaite/'.$pregnancyweek.'/"> Išsamiai apie tavo nėštumo savaitę</a></span>
</p>
</div>';
}
if ($pregnancyweeknumber > 0 & $pregnancyweeknumber < 5 )
{
$prcalc.='<div class="nestumo-skaiciuokle">
Jei mėnesinės vėluoja, gali būti, kad tu laukiesi. <br />
<a href="http://www.mamuturgus.lt/nestumas/nestumo-kalendorius/savaite/'.$pregnancyweek.'/">
<img class="nestumo-savaites-nuotrauka" src="http://www.mamuturgus.lt/wp-content/uploads/2012/05/nestumo-savaite-'.$pregnancyweek.'.jpg" height="150" width="180"></a>
Jei tikrai pastojai, <br />apytikslė gimdymo data būtų: <strong>'.$duedate.' </strong> <br />
<p>Tuomet tai būtų <a class="nestumo-savaite" href="http://www.mamuturgus.lt/nestumas/nestumo-kalendorius/savaite/'.$pregnancyweek.'/"> '.$pregnancyweek.' nėštumo savaitė. </a></p><br />
<p class="nestumo-savaites-aprasymas"><a href="http://www.mamuturgus.lt/nestumas/nestumo-kalendorius/savaite/'.$pregnancyweek.'/"> Išsamiai apie tavo nėštumo savaitę</a></p>
</div>';
}
if ($pregnancyweeknumber <= 0)
{
$prcalc.='<div class="nestumo-skaiciuokle">
Jūsų sekantis vaisingiausias periodas yra nuo <strong>'.$firstday.' iki '.$lastday.'</strong>.<br ><br />
Jeigu pastosite šio termino metu, vaikelis gims <strong>'.$duedate.'</strong>
</div>';
}
}
else
{
$prcalc.='<div class="nestumo-skaiciuokle">
<form method="post">
Pirmoji paskutinių mėnesinių diena:<br /><br />
'.pregwcalc_datechooser("date",date("Y-m-d")).'<br><br>
Mėnesinių ciklo trukmė: <select name="days">';

for($i=20;$i<=45;$i++)
{
if($i==28) $selected='selected';
else $selected='';
$prcalc.="<option $selected value='$i'>$i</option>";
}

$prcalc.='</select>
<p align="left"><input type="submit" name="calculator_ok" value="Skaičiuoti"></p>
</form>
</div>';
}

return $prcalc;
}



I would be very pleased if you could help me with this problem. Since I have very little knowledge about the php, please write the solution as it would be for a noob :)

Thanks!

Dormilich
10-11-2012, 08:10 AM
essentially, all you have to do is check which option corresponds to the submitted value and give it the selected attribute

Bra1nSOFT
10-11-2012, 10:41 AM
Thanks, but maybe you could point me where I should add this code and how the code should look? Because I have no idea where to look for..;/

Dormilich
10-11-2012, 11:37 AM
where you’re already setting the selected attribute: lines #81, #90, #99, #173, #237.

Bra1nSOFT
10-11-2012, 02:04 PM
Sorry for asking so much dumb questions, but how can I check the submitted value? Because I practically have no understanding in php, only in some logic algorithms (from learning in my school)...

I managed to program the logical algorithms by myself, but to do something more complicated, I am stuck...

Dormilich
10-12-2012, 07:30 AM
the submitted value is in the $_POST array ($_POST["name_of_dropdown"]).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum