使用按钮张贴到一个PHP脚本 [英] Use buttons to post to a php script

查看:131
本文介绍了使用按钮张贴到一个PHP脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我要对我工作的一个应用程序的控制面板只是按钮的数组。我想通过哪个按钮是pssed来将处理实际的命令脚本$ P $信息。我不能完全弄清楚如何使用POST做...下面是一些code,我有这么远。<​​/ P>

Basically I am going to have a control panel for an application I'm working on that is just an array of buttons. I want to pass information about which button was pressed to the script that will handle the actual commands. I can't quite figure out how to use POST do that... Here's some code that I have so far.

<php
include("writemessage.php");
echo " <form action='writemessage.php' method='POST'>
<input type='submit' name='message' value='custom1'/>
<input type='submit' name='message' value='custom2'/>
</form>";
?>

据我可以通过一些教程和文档应该是工作读书收集?该writemessage.php脚本只是把信息在一个文件中,和我一起GET测试它和它的工作就好了。

As far as I can gather from reading through some tutorials and documentation it should be working? The writemessage.php script just puts the information in a file, and I tested it with GET and it worked just fine.

<?php
$file = 'log.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Change file to command.
$current = $_POST[message];
// Write the contents back to the file
file_put_contents($file, $current);
?>

我希望它在后台提交。我希望它看起来好像它不刷新或任何地方浏览。

I want it to submit in background. I want it to appear as though it doesn't refresh or navigate anywhere..

推荐答案

在事实上,它的工作原理...只是一些细节来解决(在注释中说明):


No Ajax

In fact it works... just a few details to fix (noted in comments):

<?php //<-- here it was "<php" fix that!
    include("writemessage.php");
    //Don't sent people over to writemessage, otherwise why did you include it?
    echo '<form action="" method="POST">
    <input type="submit" name="message" value="custom1"/>
    <input type="submit" name="message" value="custom2"/>
    </form>';
    //Note: I changed quotes, no big deal
    echo $current; //you can read $current
?>

writemessage.php:

writemessage.php:

<?php
    $file = 'log.txt';
    if (isset($_POST['message'])) //check if isset, also use quotes
    {
        // Change file to command.
        $current = $_POST['message']; //again quotes
        // Write the contents back to the file
        file_put_contents($file, $current);
    }
    else
    {
        if (file_exists($file)) //check if file exists
        {
            // Open the file to get existing content
            $current = file_get_contents($file);
        }
        else
        {
            // default value?
            //$current = '???';
        }
    }
?>


的Ajax

我没有看到你说:在后台提交,它的意思是你不想要一个页面加载?你可以做到这一点与阿贾克斯...


Ajax

I didn't notice you said "submit in background", does it mean you don't want a page load? you can do that with Ajax...

<?php include("writemessage.php"); ?>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script>
    function _submit(value)
    {
        //This is the value set locally, do you want to be able to get values from other users? that is a whole level more complex
        $('#current').html(value);
        //Here we send the info the server via AJAX with jQuery
        var ajax = $.ajax(
        {
            type: "POST",
            url: "writemessage.php",
            data: {message: value}
        });
        //This is the value form the server, note: it may change from another client and this will not be updated
        ajax.done(function(response)
        {
            $('#current').html(response);
        });
    }
</script>
<form action="">
    <input type="button" name="message" value="custom1" onclick="_submit('custom1')"/>
    <input type="button" name="message" value="custom2" onclick="_submit('custom2')"/>
</form>
<span id="current"><?php echo $current; ?></span>

注1:我使用jQuery从网址 HTTP://$c$c.jquery.com/jquery-1.10.2.min.js 选择你想要的版本,并将其放置在您的服务器来代替。

Note 1: I'm using jQuery from the url http://code.jquery.com/jquery-1.10.2.min.js choose the version you want and place it in your server instead.

writemessage.php:

writemessage.php:

<?php
    $file = 'log.txt';
    if ($_SERVER['REQUEST_METHOD'] === 'POST' && array_key_exists('message', $_POST))
    {
        $current = $_POST['message'];
        file_put_contents($file, $current);
        echo $current; //this is the response we send to the client
    }
    else
    {
        if (file_exists($file)) //check if file exists
        {
            $current = file_get_contents($file);
        }
        else
        {
            //$current = '???';
        }
    }
?>

注2:你也有兴趣 POST-REDIRECT-GET

这篇关于使用按钮张贴到一个PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆