用户点击提交按钮后,我需要将已检查的所有内容的值发送到myLibrar-6ren">
gpt4 book ai didi

php - 从PHP表单提交选中的选项

转载 作者:行者123 更新时间:2023-12-03 08:29:59 24 4
gpt4 key购买 nike

我试图弄清楚如何将选中的选项从一系列复选框发送到跟踪所选内容的文本文件。我现在最多的就是这个。

<html>
<?php
//Get contents from zmzonSongs.txt file, put into array.
$songList = explode("\n", file_get_contents('zmzonSongs.txt'));

print "Welcome to Zmzon. Select songs below to add to your library.";
//Print contents as checklist.
foreach($songList as $songs){
echo "<br/><input type='checkbox' name='songList[]' value='$songs' />"$songs<br>";
}

?>

</html>

用户点击提交按钮后,我需要将已检查的所有内容的值发送到myLibrary.txt之类的文件中,但是每次我尝试使用echo命令添加提交按钮时,我的整个页面都会停止工作并显示为空白。我完全迷路了。

最佳答案

您的复选框和提交按钮应该在表单内,并且您的提交按钮是HTML,而不是PHP。

<?php 
/* Lets set the destination to the same file as the form. (see form action)
* That means, if someone has submitted the form, there will be data inside
* $_POST right now!
*/
// var_dump( $_POST ); <-- Can be used to quickly peek inside $_POST

if ( !empty( $_POST ) ) // Check if there is any data
{
save_data_to_file( $_POST ); // pseudo-code (should be replaced by you)
echo "success!"; // A success message perhaps?
exit(); // Stop here to not display the form below.
}

// If we come this far, it means the form has not been submitted.
// So lets display the form!

// You can have this line first in the file, or later,
// as long as you do this before you try to use it in your checkboxes.
$songList = explode("\n", file_get_contents('zmzonSongs.txt'));
?>

<h1>Welcome to Zmzon. Select songs below to add to your library</h1>
<form action="thisfile.php" method="POST">
<?php
// See, you can jump between PHP and HTML whenever you want.
foreach($songList as $songs){
echo "<input type='checkbox' ... ><br>";
}
?>
<input type="submit">
</form>

您可以将数据发送到另一页或同一页,具体取决于“操作”。
数据将位于变量$ _POST或$ _GET内部(取决于您选择的发送方式)。

查看这些全局变量的一种简单方法是使用var_dump()。例如:
<pre>
<?php var_dump($_POST); ?>

再次更新

关于php - 从PHP表单提交选中的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21768888/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com