【问题标题】:Country Selector (AJAX/JSON + PHP)国家选择器 (AJAX/JSON + PHP)
【发布时间】:2019-03-04 02:04:56
【问题描述】:

你能帮我解决这种情况吗?

假设我现在有这个:

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');
            }
        });
    })
});

我的.php 文件应该是什么?我需要做这样的事情:

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

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

非常感谢!

【问题讨论】:

  • 你为什么使用mysql_*
  • @LawrenceCherone 我应该使用什么?你在参考 MySQLi 吗?我很感激任何信息。谢谢!
  • 凯文回复:mysql_ 请阅读this Q&A

标签: php json ajax country


【解决方案1】:

当您使用POST 方法将数据发送到您的服务器端php 代码时,您可以使用$_POST$_REQUEST 像这样的全局变量捕获数据-

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

您说您在 ajax 调用时遇到错误,您可以尝试使用这种方式来调试该错误的原因-

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

要捕获 mysqli 错误,(我建议您使用 mysqli 并避免使用旧的/已弃用的 mysql),请参阅 MORE

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

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

对于其他类型的 SQL 语句,INSERT、UPDATE、DELETE、DROP 等, mysql_query() 成功时返回 TRUE,错误时返回 FALSE。

【讨论】:

  • 谢谢,我也想过 $_POST 但我的问题是根据我当前的 JS 转换整个 PHP 文件。
  • 是否有帮助,即您还有什么问题吗?
  • 无论我在 PHP 文件上使用什么,系统都会给我 $.alert('error');,我想我需要澄清 .php 中的所有结果,成功(与否),但我从未使用过 AJAX/JSON
  • 感谢您的帮助。这很奇怪,因为现在使用该调试选项,当我尝试选择不同的国家/地区选项时,我没有收到错误消息,但它不会执行和更新 MySQL 查询。
  • 我也有同样的情况 :(
【解决方案2】:

试试这个,它应该适合你的情况。我已经在我的本地主机中对其进行了测试。 对于您的 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 文件:

<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>

对于您的 .PHP 文件:

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

如果还需要帮助,请告诉我兄弟。

【讨论】:

  • 无论我在 PHP 文件上使用什么,系统都会给我 $.alert('error');,我想我需要澄清 .php 中的所有结果,成功(与否),但我从未使用过 AJAX/JSON
  • 而我需要做的是在 .php 中:mysql_query("UPDATE igaccounts SET Country='$InstagramCountry' WHERE Username='$InstagramUsername'")
【解决方案3】:

您好,您可以这样做:

你的 php 脚本:

                  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;

                          }
                      }

action 是你想要执行 SLC、UPD、DEL 等的命令,id 是参数

然后在你的 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 }
                      })
                  }

这样称呼它:

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

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-16
    • 2017-09-08
    • 1970-01-01
    相关资源
    最近更新 更多