使用按钮发布到php脚本

Use buttons to post to a php script

本文关键字:php 脚本 按钮      更新时间:2023-12-31

基本上,我将为我正在处理的应用程序提供一个控制面板,它只是一个按钮阵列。我想将有关按下哪个按钮的信息传递给将处理实际命令的脚本。我不太明白如何使用POST来完成。。。这是我目前掌握的一些代码。

<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测试了它,它运行得很好。

<?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);
?>

我希望它在后台提交。我希望它看起来好像不会刷新或导航到任何地方。。


没有Ajax

事实上它是有效的。。。只有几个细节需要修复(在评论中注明):

<?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:

<?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可以做到这一点。。。

<?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:我使用的是来自url http://code.jquery.com/jquery-1.10.2.min.js的jQuery,选择您想要的版本并将其放在服务器中。

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-REDECT-GET感兴趣。

确保按钮有不同的名称,这就是在$_POST数组中引用它们的方式。

例如,试试这个:

<input type='submit' name='message_1' value='custom1'/>
<input type='submit' name='message_2' value='custom2'/>

1-由于您想通过后台提交,因此需要使用Ajax。我将展示如何与Jquery的Ajax一起使用。

2-当你想在后台发布时,你不再需要<form>:

    <php
    include("writemessage.php");
    ?>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script>
        //Post data to "writemessage.php" on background (AJAX)
        function customSubmit(optionVal)
        {
            $.ajax({
              url: "writemessage.php",
              data: { 
                    'message': optionVal
                },
            }).done(function(data) {
              //if you want to see what this return, use alert(data);
            });
        }

    </script>

echo "
<input type='button' name='message1' value='custom1' onclick="javascript:customSubmit("custom1");"/>
<input type='button' name='message2' value='custom2' onclick="javascript:customSubmit("custom2");"/>
";
?>

3-现在在您的读取文件中,您可以读取POST"hidOption"作为所选按钮的值:

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

就这么简单。

<form method="post">
    <div class="response"></div>
    <input type="submit" name="message" value="custom1" />
    <input type="submit" name="message" value="custom1" />
<form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
    jQuery(function ($) {
        $('form').on('submit', function (e) {
            e.preventDefault();
            $.ajax({
                context : this,
                data : $(this).serialize(),
                type : 'post',
                url: 'writemessage.php'
            }).success(function (response) {
                $(this).find('.response').html(response);
            });
        });
    });
</script>

另外,您的writemessage.php文件也可以稍微清理一下。

<?php
$file = 'log.txt';
if (file_exists($file))
{
    // Do some kind of validation on your input.
    if (in_array($_POST['message'], array('custom1', 'custom2'))
    {
        file_put_contents($file, $_POST['message']);
        echo "<p>The value is {$_POST['message']}</p>";
    }
    else
    {
        echo '<p class="error">Illegal value!!</p>';
    }
}