lrony* 2017-12-05 11:04 采纳率: 0%
浏览 11

Ajax-将按钮值发送到php

I have a problem with send data to php. I want to send button value via jquery ajax. This is my code: HTML

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
$(document).ready(function(){
    $.ajax({
        url: "try.php",
        type: "POST",
        data: {
            data: val    // removed ; after val.
        }
    });
});

</script>

    <body>

      <button id="1" name="1"  value="some_value">1</button>
      <button id="2" name="2"  value="some_value">2</button>


    </body>

PHP:

<?php

$name = $_POST['data'];
echo $name;

?>

It doesn't working...

  • 写回答

3条回答 默认 最新

  • weixin_33734785 2017-12-05 11:07
    关注

    Make proper json string to send data. You are having extra ; there.

    $(document).ready(function(){
        $.ajax({
            url: "try.php",
            type: "POST",
            data: {
                data: val    // removed ; after val.
            }
        });
    });
    

    And get it with data key in php.

    <?php
    
    $name = $_POST['data'];
    echo $name;
    ?>
    

    Also, write your event listeners inside document.ready(). Currently your listeners are not getting applied as the script is on the top and is not able to find the <button> as they are not yet present.

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?