duanchun1852 2012-11-06 15: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 15: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 
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部