1

I need to pass a variable value from javascript to controller. The value is obtained by select option in html, and the javascript code is inside the view. I've written the code but it's just showing the value that I choose to HTML, and it's not stored in php variable.

Here's my javascript code :

$('#button-search-ohr').on('click', function() {
    var month = $('#monthChoose').val();

    if(month == 0) {
        alert('Month must be choosen!');
    }

    $.ajax({
        async   : true,
        type    : 'POST',
        url     : '<?php echo base_url();?>sms/lapkinerjastat', //this url to controller
        data    : {month:month},
        success : function(data) {
            $('#month').html(data);
        }
    });
}

And here's my html :

<h5 class="pilih-bulan">Month Choose
     <select id="monthChoose" name="month">
          <option value="0">Pilih bulan</option>
          <option value="Januari">Januari</option>
          <option value="Februari">Februari</option>
          <option value="Maret">Maret</option>
          <option value="April">April</option>
          <option value="Mei">Mei</option>
          <option value="Juni">Juni</option>
          <option value="Juli">Juli</option>
          <option value="Agustus">Agustus</option>
          <option value="September">September</option>
          <option value="Oktober">Oktober</option>
          <option value="November">November</option>
          <option value="Desember">Desember</option>
     </select>
     <button id="button-search-ohr" type="submit">Search</button>
</h5>

<div id='month'>
    <?php 
    if(isset($_POST['month'])) {
        $month = $_POST['month'];
        echo 'This is '.$month;
    }else {
        $month = null;
        echo 'Nothing';
    }?>
</div>

Note : It'll be better with ajax to pass variable to controller.

2
  • use .serialize method when you submit your form Commented Sep 24, 2018 at 3:31
  • @curiosity Thanks for your reply but I don't use form for select option. I'd like to pass the variable in javascript and store it to controller in php. Commented Sep 24, 2018 at 3:50

3 Answers 3

1

In your code you are utilizing the value of the variable month twice, instead of defining 'month' to be the variable name of the $_POST global:

data    : {month:month},

After parsing, in case the chosen month is April for instance, the result is:

data    : {April:April},

while you actually want to have

data    : {month:April},

Thus I suggest altering your code and just wrapping the variable name in quotes:

data    : {'month':month},

That way the PHP script will actually have a $_POST['month'] to work with ;)

Sign up to request clarification or add additional context in comments.

2 Comments

Is this publicly available? If so, please provide a link to the page. Alternatively, a code snippet would be useful.
I try your suggestion but the result is the page becomes double. Here's the screenshot ibb.co/j5s3Gp. In that picture, I managed to pass variable from javascript to php but the result is double page. EDIT in previous comment I unintentionally put the wrong picture. No it's in localhost.
0

you write down the condition inside your controller so that the ajax result is just the plain text.

1 Comment

But I need to send and store the variable to view, not just echoing it.
0

You are misunderstanding how ajax works. JS is client-side, while php is server side. Whether or not you choose async the php on the page is already rendered thus you can't change php code or variables after the fact. The best you can do is manipulate the div in the success callback (which seems like you are already attempting to do).

So in your ajax controller (lapkinerjastat) you would do:

$month = $this->input->post('month');
if (is_null($month)) {
    echo 'This is ' . $month;
} else {
    echo 'Nothing';
}

and via $('#month').html(data); your logic (that you had in server-side php) is replicated.

Why you want ajax for this I don't know. You could easily do this all with straight js.

1 Comment

Thanks for your reply, do I need to remove success : function(data) { $('#month').html(data); } ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.