Kevin
Kevin

Reputation: 13

Country Selector (AJAX/JSON + PHP)

Could you please help me with this situation?

Let's say I have this right now:

HTML

<select name="insta_country_code" class="widget-user-country country-selector" data-account-name="
    <?php echo $account1['Username'] ?>">
    <option value="br">Brazil</option>
    <option value="cl">Chile</option>
    <option value="es" selected>Spain</option>
    <option value="mx">Mexico</option>
    <option value="gb">United Kingdom</option>
    <option value="cr">Costa Rica</option>
</select>

JS

$(document).ready(function () {
    $('.country-selector').change(function () {
        var ig_name = $(this).data('account-name');
        var country_code = $(this).val();
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: 'change-country.php',
            data: {
                insta_user: ig_name,
                country_code: country_code,
            },
            success: function (data) {
                if (data['status'] != 1) {
                    $.alert('server error');
                }
            },
            error: function () {
                $.alert('error');
            }
        });
    })
});

What should be my .php file? I need to do something like this:

$InstagramCountry = $data['country_code'];
$InstagramUsername = $data['insta_user']);

$sql = mysql_query("UPDATE igaccounts SET Country='$InstagramCountry' WHERE Username='$InstagramUsername'");

Thank you so much!

Upvotes: 1

Views: 387

Answers (3)

stan chacon
stan chacon

Reputation: 768

Hi you can do it this way:

your php script:

                  if (isset($_POST["action"])) {
                          $action = $_POST["action"];
                          switch ($action) {
                              case 'SLC':
                                  if (isset($_POST["id"])) {
                                      $id = $_POST["id"];
                                      if (is_int($id)) {
                                          $query = "select * from alumni_users where userId = '$id' ";
                                          $update = mysqli_query($mysqli, $query);
                                          $response = array();
                                          while($row = mysqli_fetch_array($update)){
                                          .......
                                          fill your response here

                                          }
                                         echo json_encode($response);
                                      }
                                  }
                                  break;

                          }
                      }

Where action is a command you want to do SLC, UPD, DEL etc and id is a parameter

then in your ajax:

                function getdetails() {
                      var value = $('#userId').val(); // value can be your array ! note: if you send a object json_encode(json_decode(,MyObj,true))
                     return $.ajax({
                          type: "POST",
                          url: "getInfo.php",
                          data: {action: "SLC",id: value }
                      })
                  }

call it like this:

    getdetails().done(function(response){
                  var data=JSON.parse(response);
                  if (data != null) {
                  //do somthing with your Data
                  }
                  })

Hope it Helps

Upvotes: 0

Abdul Rehman Sheikh
Abdul Rehman Sheikh

Reputation: 939

Try this, it should must work for your case. I have tested it in my localhost. For your HTML File:

<select name="insta_country_code" class="widget-user-country country-selector" data-account-name="<?php echo $account1['Username'] ?>">
            <option value="br">Brazil</option>
            <option value="cl">Chile</option>
            <option value="es" selected>Spain</option>
            <option value="mx">Mexico</option>
            <option value="gb">United Kingdom</option>
            <option value="cr">Costa Rica</option>
        </select>

For your JS File:

<script>
            $(document).ready(function () {
                $('.country-selector').change(function () {
                    var ig_name = $(this).data('account-name');
                    var country_code = $(this).val();
                    $.ajax({
                        type: 'POST',
                        dataType: 'json',
                        url: 'change-country.php',
                        data: {
                            insta_user: ig_name,
                            country_code: country_code,
                        },
                        success: function (data) {
                            if (data['status'] != 1) {
                                $.alert('server error');
                            }
                        },
                        error: function () {
                            $.alert('error');
                        }
                    });
                })
            });
        </script>

For your .PHP File:

<?php 
$data = $_REQUEST; 
$InstagramCountry = $data['country_code'];
$InstagramUsername = $data['insta_user'];
echo "insta_user=".$InstagramUsername . " AND country_code=" . $InstagramCountry; exit; 

Tell me if still need any help bro.

Upvotes: 0

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38502

As you're using POST method to send data to your server side php code, you can catch the data using $_POST OR $_REQUEST global variable like this-

$InstagramCountry = $_POST['country_code'];
$InstagramUsername = $_POST['insta_user']);

You said vai comment that you're getting error on ajax call, you can try this way to debug what is the reason of that error using-

error: function(xhr, textStatus, error){
      console.log(xhr.statusText);
      console.log(textStatus);
      console.log(error);
  }

To capture mysqli error, (I urge you to use mysqli and avoid to use the old/deprecated mysql), SEE MORE

$result = mysql_query('UPDATE igaccounts SET Country='$InstagramCountry' WHERE Username='$InstagramUsername');

if (!$result) {
    die('Invalid query: ' . mysql_error());
}

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

Upvotes: 1

Related Questions