duanchun1852 2012-11-06 23:33
浏览 32
已采纳

不明白为什么单击输入按钮后没有设置$ _POST

In the context of a form without a submit button, I am not understanding why the $_POST variable doesn't get set. Here is my code:

<?php
  $view="blablabla";
?>
<form class="" method='POST' accept-charset="UTF-8" action='index.php'>
<p>
  <textarea rows="4" cols="60" name='article' id='article' WRAP=SOFT>
    <?php
      echo $view;
    ?>
  </textarea>
</p>    
<p>
  <input type="button" name='do1' id='do1' value="do 1"/>
  <input type="button" name='do2' id='do2' value="do 2"/>
</p>
</form>

<?php
  var_dump("POST :",$_POST);
?>

Shouldn't the var_dump show a value to $_POST once I click one of the 2 buttons?

  • 写回答

1条回答 默认 最新

  • dongren2128 2012-11-06 23:35
    关注

    Nope- the input types have to be submit (<input type="submit"/>) for the form to be submitted. A POST request has to be sent to your PHP script in order for there to be any values populated in the $_POST array :)

    EDIT: They don't have to be submit, but in the case of the code above, with no external triggers on the buttons, you'll need submit buttons :)

    For your multiple buttons issue:

    If you're using JavaScript/Ajax to send the POST request to PHP on the button click, you'll need to do something similar:

    if (isset($_POST['x'])) {
        var_dump($_POST);
        exit;
    }
    

    This is so the script doesn't return the HTML of the page (i.e. your form)- this isn't an issue if you're POSTing to another page :)

    Below is some sample jQuery code to send some POST data on button click (not going to give you the exact code, you'll have to modify it to your needs!)

    $('#my_button').click(function(){
        $.post('myfile.php', data, function(response) { //the var 'data' is the POST data you're sending
            // handle response here, the data being in the response variable 
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么