-2

I need to save selected value of the drop-down list to a CodeIgniter session.

This is my HTML code:

<div class="form-group">

    <label for="lastname" class="control-label">Your Packages</label>
    <?php
        if(isset($tourbuddy_packages)){
    ?>
    <select id="itemType_id" class="form-control input-sm" name="tripbuddy_PackageTitle" onChange="disp_text()">
    <?php
        foreach ($tourbuddy_packages as $packages) {
    ?>
    <option value="
    <?php
        echo $packages['PackageID']
    ?>
    ">
    <?php
        echo $packages['PackageTitle']
    ?>
    </option>
    <?php
        }
    ?>
    </select>
    <input type="hidden" name="PackageID" id="country_hidden">
    <?php
        } else {
    ?>
    <select class="form-control input-sm" name="tripbuddy_PackageTitle">
        <option>Add Packages</option>
    </select>
    <?php 
        }
    ?>
    </div>

This my JavaScript code:

<script type="text/javascript">
    function disp_text() {
        var w = document.Tripbuddy.tripbuddy_PackageTitle.selectedIndex;
        var selected_text = document.Tripbuddy.tripbuddy_PackageTitle.options[w].value;
        alert(selected_text); //*** you could omit this line now ***
    }
</script>

How could I achieve this?

SáT
  • 3,633
  • 2
  • 33
  • 51
user2951386
  • 261
  • 1
  • 5
  • 13
  • 3
    This question has been asked numerous times. http://stackoverflow.com/questions/1917576/how-to-pass-javascript-variables-to-php – Max Langerak Mar 26 '14 at 15:49

4 Answers4

3

You cannot pass variable values from the current page javascript to the current page PHP code... PHP code runs at the server side and it doesn't know anything about what is going on on the client side.

Pixel
  • 1,946
  • 2
  • 15
  • 26
2

You need to check out sending variable to another file with ajax.

Burak Tokak
  • 1,810
  • 1
  • 20
  • 27
2

PHP - back-end scripting
JS - front-end scripting

Ergo, you can't pass a variable from a front-end to a back-end. But you can fetch a value from front-end to the back-end by means of a <form></form>. All you have to do is create a hidden field in your form then with some JS scripting, try setting the value of the hidden field to the values you want. Then when the form is submitted, you can then fetch the value you want to have.

YouSer
  • 393
  • 3
  • 12
0

You have to take help of jquery and ajax. Run an additional page using jquery and set the session variables. http://code.tutsplus.com/tutorials/submit-a-form-without-page-refresh-using-jquery--net-59