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
[php]
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 <div>:</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’).”
}
\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="pregwcalc_table">
<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="pregwcalc_table">
<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="196">
<span class="nestumo-savaite"> '.$pregnancyweek.' nėštumo savaitė, iki gimdymo liko '.$duedateindays.' dienų. </a> </span>
<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="pregwcalc_table">
Jei mėnesinės vėluoja, gali būti, kad tu laukiesi. <br />
Jei tikrai pastojai, 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="pregwcalc_table">
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="pregwcalc_table">
<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;
}
// This just echoes the text
function pregwcalc($content)
{
if(!strstr($content,"[pregnancy-calculator]")) return $content;
$prcalc=pregwcalc_generate_html();
$content=str_replace("[pregnancy-calculator]",$prcalc,$content);
return $content;
}
// the widget object
class pregwcalc extends WP_Widget {
/** constructor */
function pregwcalc() {
parent::WP_Widget(false, $name = ‘Pregnancy week calculator’);
}
function form()
{
pregwcalc_options(true);
}
function widget($args, $instance)
{
echo pregwcalc_generate_html();
}
}
add_action(‘admin_menu’,‘pregwcalc_add_page’);
add_filter(‘the_content’, ‘pregwcalc’);
add_action(‘widgets_init’, create_function(’’, ‘return register_widget(“pregwcalc”);’));
?>
[/php]
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!