Javascript 使用按钮发布到php脚本
基本上,我将为我正在开发的应用程序提供一个控制面板,它只是一组按钮。我想将按下哪个按钮的信息传递给脚本,该脚本将处理实际命令。我不太明白如何使用POST来做到这一点。。。这里有一些代码,我到目前为止Javascript 使用按钮发布到php脚本,javascript,php,button,Javascript,Php,Button,基本上,我将为我正在开发的应用程序提供一个控制面板,它只是一组按钮。我想将按下哪个按钮的信息传递给脚本,该脚本将处理实际命令。我不太明白如何使用POST来做到这一点。。。这里有一些代码,我到目前为止 <php include("writemessage.php"); echo " <form action='writemessage.php' method='POST'> <input type='submit' name='message' value='custom1
<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>";
?>
据我所知,通过阅读一些教程和文档,它应该可以工作吗?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);
?>
我希望它在后台提交。我想让它看起来好像不刷新或在任何地方导航一样。1-当你想通过后台提交时,你需要使用Ajax。我将展示如何使用Jquery的Ajax 2-当你想在后台发布时,你不再需要: 3-现在在您的读取文件中,您可以将POST HIDDOPTION读取为所选按钮的值:
<?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);
?>
就这么简单。1-当您想通过后台提交此文件时,您需要使用Ajax。我将展示如何使用Jquery的Ajax 2-当你想在后台发布时,你不再需要: 3-现在在您的读取文件中,您可以将POST HIDDOPTION读取为所选按钮的值:
<?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);
?>
很简单。确保按钮有不同的名称,这就是在$\u POST数组中引用按钮的方式 例如,尝试以下方法,而不是您所拥有的:
<input type='submit' name='message_1' value='custom1'/>
<input type='submit' name='message_2' value='custom2'/>
确保按钮有不同的名称,这就是在$u POST数组中引用它们的方式 例如,尝试以下方法,而不是您所拥有的:
<input type='submit' name='message_1' value='custom1'/>
<input type='submit' name='message_2' value='custom2'/>
没有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
?>
注2:您也会对以下内容感兴趣
没有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
?>
注2:您也会对以下内容感兴趣
另外,您的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>';
}
}
另外,您的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>';
}
}
如果您执行var_dump$\u POST;在你的接收脚本中?作为一个好习惯,关闭您的文件也是值得的。您可以为每个按钮制作一个单独的表单,这可能有助于清理问题。是否希望在每次单击按钮时提交页面?还是希望它在后台提交数据?@RobertSeddon Smith file\u put\u内容和file\u get\u内容都会自动关闭文件。我希望它在后台提交。我希望它看起来好像不刷新或在任何地方导航..如果你使用var_dump$\u POST会发生什么;在你的接收脚本中?作为一个好习惯,关闭您的文件也是值得的。您可以为每个按钮制作一个单独的表单,这可能有助于清理问题。是否希望在每次单击按钮时提交页面?还是希望它在后台提交数据?@RobertSeddon Smith file\u put\u内容和file\u get\u内容都会自动关闭文件。我希望它在后台提交。我想让它看起来好像不刷新或在任何地方导航..使用严格的相等性检查可能是个好主意:$\u SERVER['REQUEST\u METHOD']='POST'+1@jgetrost这一定是输入错误,请参见条件数组中的$\u key\u exists'message',$\u POST必须与$current=$\u POST['message']匹配@哦,我忘了用你的代替我的书面信息,一切都很好,效果也很好。非常感谢。使用严格的平等性检查可能是个好主意:$\u服务器['REQUEST\u METHOD']='POST'+1@jgetrost这一定是输入错误,请参见条件数组中的$\u key\u exists'message',$\u POST必须与$current=$\u POST['message']匹配@哦,我忘了用你的代替我的书面信息,一切都很好,效果也很好。非常感谢你的支持。
<?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>';
}
}