Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-10-2012, 05:37 PM   PM User | #1
Bra1nSOFT
New to the CF scene

 
Join Date: Oct 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Bra1nSOFT is an unknown quantity at this point
Question How to remember selected values in all dropdowns?

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 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 <= )
            {
        
$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 < -)
                {
                
$prcalc.='<div class="pregwcalc_table">
        <h2> Vaikelis turėjo gimti </h2>
        </div>'
;
                }
                
            if (
$duedateindaysvalue >= -$pregnancyweeknumber <= 41 &$pregnancyweeknumber >= )
                {
        
        
$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 $pregnancyweeknumber )
                    {
            
$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!
Bra1nSOFT is offline   Reply With Quote
Old 10-11-2012, 07:10 AM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
essentially, all you have to do is check which option corresponds to the submitted value and give it the selected attribute
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 10-11-2012, 09:41 AM   PM User | #3
Bra1nSOFT
New to the CF scene

 
Join Date: Oct 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Bra1nSOFT is an unknown quantity at this point
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..;/
Bra1nSOFT is offline   Reply With Quote
Old 10-11-2012, 10:37 AM   PM User | #4
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
where you’re already setting the selected attribute: lines #81, #90, #99, #173, #237.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 10-11-2012, 01:04 PM   PM User | #5
Bra1nSOFT
New to the CF scene

 
Join Date: Oct 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Bra1nSOFT is an unknown quantity at this point
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...
Bra1nSOFT is offline   Reply With Quote
Old 10-12-2012, 06:30 AM   PM User | #6
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
the submitted value is in the $_POST array ($_POST["name_of_dropdown"]).
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:49 PM.


Advertisement
Log in to turn off these ads.