基本上我将为我正在处理的应用程序设置一个控制面板,它只是一个按钮数组。我想将有关按下哪个按钮的信息传递给将处理实际命令的脚本。我无法弄清楚如何使用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);
?>
我希望它在后台提交。我希望它看起来好像不刷新或导航到任何地方..
答案 0 :(得分:2)
事实上它起作用......只需修改一些细节(在评论中注明):
<?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做到这一点......
<?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:我正在使用网址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-REDIRECT-GET感兴趣。
答案 1 :(得分:1)
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);
?>
这很容易。
答案 2 :(得分:1)
确保您的按钮具有不同的名称,这就是您在$ _POST数组中引用它们的方式。
例如,请尝试以下操作:
,而不是您拥有的<input type='submit' name='message_1' value='custom1'/>
<input type='submit' name='message_2' value='custom2'/>
答案 3 :(得分:1)
<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>';
}
}