PHPNewbie
PHPNewbie

Reputation: 247

how to post data with ajax, php and bootstrap select

I am banging my head against a wall with this now so any help will go a long way with this one.

I am trying to get some data from a drop down list and update a database with the data selected.

This is my drop down list:

<form method="post" data-remote="true">
<select class="selectpicker" data-live-search="true" name="status[]">
    <?php while($test3 = sqlsrv_fetch_array($test2, SQLSRV_FETCH_ASSOC)) : ?>
        <option data-tokens="<?php echo $test3['Name']; ?>" value="<?php echo $test3['Name']; ?>"> 
        </option 
    <?php endwhile; ?>
</select>

View Area

This is my ajax:

        $(document).on('click', '#sim-area', function() {
            $.ajax({
            type: "POST",
            url: 'sim-area.php',
            data: {changeStatus: status},
            success: function() {
              "area switched"
            }
          });
        });

and this is my page that is updating the databse (sim-area.php):

<?php

$userID = $user['userID'];
$selectedArea = $_GET['changeStatus'];
$queryAreaName = "
        SELECT UserID, Name, U_NB, U_RTN
        FROM Table
        WHERE UserID = '$userID' AND Name = '$selectedArea'";
$getAreaname = sqlsrv_query($sapconn2, $queryAreaName);
$areaSwitch = sqlsrv_fetch_array($getAreaname, SQLSRV_FETCH_ASSOC);

$areaNB = $test2['U_NB'];
$areaRTN = $test2['U_RTN'];

//UPDATE TABLE

?>

No matter what I try I get an undefined error, I have changed it to hard code the values and in inserts fine, so I know everything is working fine, It just isn't passing the data through

Upvotes: 0

Views: 1103

Answers (1)

Ranjana
Ranjana

Reputation: 825

Looks you are not passing data correctly to ajax call. Below is updated code for dropdown (changed name of select and added id attribute):

    <form method="post" data-remote="true">
    <select class="selectpicker" data-live-search="true" name="status" id="changeStatus">
        <?php while($test3 = sqlsrv_fetch_array($test2, SQLSRV_FETCH_ASSOC)) : ?>
            <option data-tokens="<?php echo $test3['Name']; ?>" value="<?php echo $test3['Name']; ?>"> 
            </option 
        <?php endwhile; ?>
    </select>

Updated code for jquery (changed code for passing value of data):

    $(document).on('click', '#sim-area', function() {
        $.ajax({
        type: "POST",
        url: 'sim-area.php',
        data: {changeStatus: $('#changeStatus').val()},
        success: function() {
          "area switched"
        }
      });
    });

Upvotes: 1

Related Questions