Search code examples
phpformsyiidatepickeryii-components

Yii: How to post the data value from 'form' to the same page


I want to send the value from this form to the same page, but the submit button not send the value

This is the form:

<form method="post" action="">
        <select name="b" value="<?=isset($_POST['b']) ? CHtml::encode($_POST['b']) : '' ; ?>">

            <option value="1">Januari</option>
            <option value="2">Februari</option>
            <option value="3">Mac</option>
            <option value="4">April</option>
            <option value="5">Mei</option>
            <option value="6">Jun</option>
            <option value="7">Julai</option>
            <option value="8">Ogos</option>
            <option value="9">September</option>
            <option value="10">Oktober</option>
            <option value="11">November</option>
            <option value="12">Disember</option>               
        </select>
        <input type="submit" value="Papar Statistik" /><hr />
    </form>

This is the funtion that use to Get the data value from form above:

<?php 

if (isset($_GET['b']) && !empty($_GET['b']))
    $current_month = $_GET['b'];
else
    $month = date('Y-m-d', time());
    $bulan = strtotime($month); // FUNCTION CONVERSION DATE KEPADA DAY
    $current_month = date("m", $bulan);

    $teacher = Yii::app()->user->getId();
    $jumlahStatistik_hadir = 0;

    for ($d = 1; $d <= 30; $d++){

      $statistik_hadir = Detail::model()->countByAttributes(array(
            'status' => '1',
            'date' => '2015-'.$current_month.'-'.$d.'',
            'teacher_id' => $teacher));

      $jumlahStatistik_hadir = 
          ($jumlahStatistik_hadir+$statistik_hadir);
    }
    print_r ($jumlahStatistik_hadir);
?>

Solution

  • If you are sending your form data using POST method

    <form method="post" action="">
        <select name="b">
            <option value="1">Januari</option>
            <option value="2">Februari</option>
        </select>
    </form>
    

    You can expect the PHP to provide the data in the $_POST global, not post.

    // This is correct
    echo $_POST['b'];
    // This is not correct
    echo $_GET['b'];
    

    NOTE: Instead of using 'value' in your select tag.

       <select name="b" value="2">
    

    You need to provide the selected option for the chosen value

    <option value="1">Januari</option>
    <option value="2" selected='selected'>Februari</option>