简体   繁体   English

jquery 将变量传递给 php

[英]jquery passing variable to php

when i try get and echo the variable on php, sending by ajax, i receive always var_dump null当我尝试获取并回显 php 上的变量时,通过 ajax 发送,我总是收到 var_dump null

the alert is ok, he shows the current slide id number when <a>(role=button) prev or next is clicked: 1 on slide 1, 2 on 2, 3 on 3;警报正常,当单击 <a>(role=button) prev 或 next 时,他会显示当前幻灯片 ID 号:幻灯片 1 上的 1、幻灯片 2 上的 2、幻灯片 3 上的 3;

<script>
$("#myCarousel").on('slide.bs.carousel', function(e) {
  var value1 = $(this).find('.carousel-item.active').attr('id');
  alert(value1);
  var value = {
    idCarrosselAtivo: $(this).find('.carousel-item.active').attr('id')
  }
  $.ajax({
    type: "POST",
    url: "teste.php",
    data: value,
    success: function(data) {
      console.log(data);
    }
  });
})
</script>
    
<?php
    if(isset($_POST['idCarrosselAtivo'])&& $_POST['idCarrosselAtivo'] === 'c1') {
        echo $idCarrossel;
    } else {
      var_dump($idCarrossel);
    }
?>

Consider this example.考虑这个例子。

jQuery jQuery

$("#myCarousel").on('slide.bs.carousel', function(e) {
  console.log("Sending ", $(this).find('.carousel-item.active').attr('id'));
  $.ajax({
    type: "POST",
    url: "teste.php",
    data: {
      idCarrosselAtivo: $(this).find('.carousel-item.active').attr('id')
    },
    success: function(data) {
      console.log(data);
    }
  });
});

PHP PHP

<?php
  if(isset($_POST['idCarrosselAtivo'])){
    $idCarrossel = $_POST['idCarrosselAtivo'];
    if($idCarrossel === 'c1') {
      echo $idCarrossel;
    } else {
      var_dump($_POST['idCarrosselAtivo']);
    }
  }
?>

It's not clear why you are sending back the ID when you just sent it to the script.目前尚不清楚为什么在将 ID 发送到脚本时将其发回。 This will send the ID and get some PHP back.这将发送 ID 并返回一些 PHP。

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

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