简体   繁体   English

用php接收发布数据ajax

[英]Receive post data ajax with php

I'm trying to receive post data with php from ajax in same page but seems like i have some issues that i have no idea to solve them 我试图在同一页面中从ajax接收带有php的帖子数据但看起来我有一些问题,我不知道要解决它们

here is my html/php code : 这是我的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>";
                                }
} ?>

And here is my Ajax post request : 这是我的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")
               }
         });
        }
        });

the code below can replace all your data with the new ones with clean writing :) 下面的代码可以用干净的写作替换你的所有数据:)

// 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>
$json_data=file_get_contents('php://input');
$json=json_decode($json_data,true);
if(array_key_exists("selectName",$json)){
   $selectOption =$json["selectName"];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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