【问题标题】:get action to happen, only when submit button is pressed仅在按下提交按钮时才会发生操作
【发布时间】:2013-05-04 10:11:51
【问题描述】:

我有一些代码可以发布到用户墙上,但是,当页面加载时它会发布,我需要它仅在提交“发布到我的墙”按钮时发布。

这是我的代码:

<div align="center">
<form method="GET" action="translate.php">
<textarea name="status2" cols="50" rows="5"<input type="text"/>
<?php echo str_ireplace(array     ('old','awkward','all','again','behind','along','alright','hello','among','children','yes','child','kids','food','barnard castle','beer','book','blow','beautiful','bird','burst','brown','burn','boots'), 
array ('auld', 'aakwad', 'aall','agyen','ahint','alang','alreet','alreet','amang','bairns','aye','bairn','bairns','bait','barney','beor','beuk','blaa','bonny','bord','borst','broon','bourn','byeuts'),$status); ?> 

</textarea><br>

<input type="submit" value="post to wall"
// i did try my wall code here but it still posted on page load
 />
</form>

</div>

<?php

$args = array(
    'message'   => 'Hello World',
    'link'      => 'http://apps.facebook.com/geordie-status/',
    'caption'   => 'Translate from English to Geordie'
    );
$post_id = $facebook->api("/$uid/feed", "post", $args);

?>

【问题讨论】:

    标签: php html facebook facebook-php-sdk facebook-apps


    【解决方案1】:

    name 属性添加到您的input 标记。并使用isset 来检查用户是否按下了提交按钮。

            <input type="submit" value="post to wall" name="submit"
            // i did try my wall code here but it still posted on page load
             />
            </form>
    
    </div>
    
        <?php    
            if (isset($_POST['submit'])){
            $args = array(
                'message'   => 'Hello World',
                'link'      => 'http://apps.facebook.com/geordie-status/',
                'caption'   => 'Translate from English to Geordie'
                );
            $post_id = $facebook->api("/$uid/feed", "post", $args);
            }
            ?>
    

    【讨论】:

    • lmao,我只是想通了,但将表单操作更改为 post.php,基本上将上述 if 添加到 post.php 中。无论如何,谢谢,你仍然得到答案:D
    【解决方案2】:

    我认为您的问题是每次重新加载页面时您的浏览器都会重新发送数据。

    有两种方法:

    • 将用户重定向到同一页面,因此发送的数据将被清除(header("Location: asd")

    • 在会话中存储一些哈希,进行隐藏输入并检查哈希是否正确。正确提交表单后更改哈希。

    【讨论】:

    • 说实话不知道这意味着什么。上面的代码是我的 translate.php 页面。在此之前,用户使用我的 index.php,并且在那里输入的数据使用 POST 发送到 translate.php。 textarea 用于显示更改后的数据,用户可以点击“提交”,以便将数据发布到他们的墙上。
    【解决方案3】:

    您应该将发布的代码放在 translate.php 中的墙上,因为那是表单操作中列出的页面。当表单被提交时,值将作为参数传递给 translate.php 然后你可以使用 $_GET 来获取它们并执行写入墙上的代码。

    【讨论】:

    • 对不起,我应该提到,这段代码在 translate.php 中。我需要的是仅在单击提交按钮时才执行的 php 代码。在我弄清楚这一点之后,我可以专注于我的其余问题。谢谢
    • 你可以在表单中添加一个 ,然后检查 $_GET['isSubmitted'] == 'true ' 如果是这样,那么您知道表单提交按钮实际上已被单击并且正在发布数据。
    猜你喜欢
    • 2017-12-27
    • 1970-01-01
    • 2012-07-10
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 2019-10-18
    • 2013-07-13
    相关资源
    最近更新 更多