【问题标题】:Receive post data ajax with php用php接收post数据ajax
【发布时间】:2017-05-28 15:51:36
【问题描述】:

我正在尝试在同一页面中接收来自 ajax 的 php 发布数据,但似乎我有一些我不知道如何解决的问题

这是我的 html/php 代码:

<select style="width:auto; margin-left:6%;" class="form-control" name="n-omran-select" id="num_omrane">
    <option value='' >Choisir...</option>
    <?php
    while ($row = $result->fetch_assoc())
    {
            echo "<option value=".$row['N_omran'].">".$row['N_omran']."</option>";
    }
    ?>
</select><br>
<?php
if (isset($_POST["selectName"])) {  // try to receive post values but it seems that's not working
            $selectOption = $_POST["selectName"];
            $query = "SELECT nom,prenom,tel,adress,matricule_assu FROM `personnel` WHERE N_omran ='$selectOption'";
            $result = mysqli_query($db,$query);
            $row=mysqli_fetch_array($result,MYSQLI_ASSOC);
            if ($row) {
                echo "      <h4>Nom : {$row['nom']}</h4>
                                    <h4>Prénom : {$row['prenom']}</h4>
                                    <h4>Téléphone : {$row['tel']} </h4>
                                    <h4>Maticule d'assurance : {$row['matricule_assu']}</h4>
                                    <h4>Adresse : {$row['adress']}</h4>";
                                }
} ?>

这是我的 Ajax 发布请求:

        $('#num_omrane').on('change', function () {
         var n_omrane = $('#num_omrane').val();
        if(n_omrane != ''){
            $.ajax({
           type: "POST",
           url: "index.php",
           data: {selectName: n_omrane},
           success: function () {
                alert("Post request successfully done")
               }
         });
        }
        });

【问题讨论】:

  • success 选项需要一个函数,例如像这样:success: function () { alert(...); }
  • 这看起来像是你可以用来学习的东西。 stackoverflow.com/questions/8220950/…
  • 已更改但仍无法获取我的 php 代码@PeterMader 上的值
  • 在这一行之后 if(n_omrane != ''){ put alert('hello') 并检查它是否被显示?我猜这个值没有被传递@HamzaBoularbah
  • 是的,我看到了警报 .. 我也可以看到 post 的成功消息 .. 但我无法在 php @Exprator 上收到值

标签: javascript php jquery ajax post


【解决方案1】:

下面的代码可以用干净的新数据替换你的所有数据:)

// get data from Database

<?php
if (isset($_POST["selectName"])) {  // try to receive post values but it seems that's not working
  $selectOption = $_POST["selectName"];
  $query = "SELECT nom,prenom,tel,adress,matricule_assu FROM `personnel` WHERE N_omran ='$selectOption'";
  $result = mysqli_query($db,$query);
  $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
} ?>
// show rows to be selected 
<select style="width:auto; margin-left:6%;" class="form-control" name="n-omran-select" id="num_omrane">
    <option value='' >Choisir...</option>
    <?php while ($row = $result->fetch_assoc()) { ?>
      <option value="<?= $row['N_omran'] ?>"> <?= $row['N_omran'] ?></option>
    <?php } ?>
</select><br>
// show recieved data 
<?php if ($row) { ?>
  <div id="informations">
    <h4>Nom : <span id="nom"><?= $row['nom'] ?></span></h4>
    <h4>Prénom : <span id="prenom"><?= $row['prenom'] ?></span></h4>
    <h4>Téléphone : <span id="tel"><?= $row['tel'] ?> </span></h4>
    <h4>Maticule d'assurance : <span id="matricule_assu"><?= $row['matricule_assu'] ?></span></h4>
    <h4>Adresse : <span id="adress"><?= $row['adress'] ?></span></h4>
  </div>
<?php } ?>

// script for making ajax call
<script>
$('#num_omrane').on('change', function () {
  var n_omrane = $('#num_omrane').val();
  if(n_omrane != ''){
    $.ajax({
      type: "POST",
      url: "index.php",
      data: {selectName: n_omrane},
      success: function (response) {
        $("#nom").text(response.nom);
        $("#prenom").text(response.prenom);
        $("#tel").text(response.tel);
        $("#matricule_assu").text(response.matricule_assu);
        $("#adress").text(response.adress);
      }
    });
  }
});
</script>

【讨论】:

    【解决方案2】:
    $json_data=file_get_contents('php://input');
    $json=json_decode($json_data,true);
    if(array_key_exists("selectName",$json)){
       $selectOption =$json["selectName"];
    }
    

    【讨论】:

    • @HamzaBoularbah 而不是if (isset($_POST["selectName"])) { // try to receive post values but it seems that's not working $selectOption = $_POST["selectName"];
    • 警告:array_key_exists() 期望参数 2 是数组,给定 null .. 它不工作
    • @HamzaBoularbah 我的测试显示$_POST["selectName"] 成功接收价值。插入 PHP 代码 echo "&lt;!-- ".print_r($_POST,true)." --&gt;" 并检查控制台输出
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 1970-01-01
    • 2019-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多