简体   繁体   中英

Onchange select with AJAX and PHP

I want to get the value from this select form, with the onchange function:

<p for="session">Time:</p>
<select class="form-control" id="session" name="session" onchange="myfunctionTime(this.value)"> 
   <option selected="selected" value="12:00">12:00</option>
   <option value="16:00">16:00</option>
   <option value="20:00">20:00</option>
</select>

On the top of the file I have the ajax function which I can see it's working because inside the console(f12) I see like "Session 16:00" or "Session 12:00" etc:

<script type="text/javascript">
function myfunctionTime(session)
 {
    //make the ajax call
    $.ajax({
        url: 'book.php?id=<?php echo $film_id; ?>',
        type: 'POST',
        data: {option : session},
        success: function() {
            console.log("Session "+session);
        }
    });
}

At the end I have the php code that give me the error:

: Undefined variable: session in on line :未定义的变量:第行上会话

function myfunctionTime(){
        $session = $_POST['option'];
    }
    echo $session;

Change that lines of code to



    $session = $_POST['option'];
    echo $session;

Not a good way to do. But given your code, it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM