根据表单提交结果回显 2 个不同的 url 位置
echo 2 different url locations depending on form submit results
我有一个从数据库填充数据的表单,用户通过单击复选框来选择感兴趣的各个行。一切都已成功运行,但我现在正尝试在提交表单时将页面重定向到 2 个位置之一,具体取决于所选行数。 (选中复选标记。)这是出于安全原因压缩的当前脚本。
if(isset($_POST['submit'])){
$DELETE = "DELETE FROM job_picks WHERE ID='$userid'";
$wpdb->query($DELETE);
foreach($_POST['check_list'] as $selected){
$rowCount = count($_POST['ref']);
for ($i=0; $i<$rowCount;$i++)
{
if ($selected == $_POST['ref'][$i])
$compcode = $_POST['COMPCODE'][$i];
$note = $_POST['note'][$i] ; // changed for security reasons
$time = $_POST['time'][$i]; // changed for security reasons
$date = $_POST['date'][$i]; // changed for security reasons
$SQL = " left out for security reasons but is correctly populating data";
$wpdb->query($SQL);
echo "<script>window.location='https://example.com/step2/';</script>";
} ?>
<br class="clear">
<?php } ?>
<br class="clear">
<?php } ?>
<br class="clear">
<?php }
?>
我试过换行
echo "<script>window.location='https://example.com/step2/';</script>";
到
if ($rowcount == 1) {
echo "<script>window.location='https://example.com/step3/';</script>";
}
else
echo "<script>window.location='https://example.com/step2/';</script>";
但似乎只将所有结果发送到第 3 步,而不是仅将那些只检查了一项的提交。任何见解都将不胜感激,片段总是有帮助的,在此先感谢。
我看到了你的 $rowCount
和 $rowount
,希望这只是错别字。
并且您可以调试循环中每个 $rowCount
的值。类似于您的调试代码:
foreach($_POST['check_list'] as $selected){
$rowCount = count($_POST['ref']);
//print the debug data
print_r($rowCount, true );
}
die();
此外,请确保 $_POST['ref']
有效。
仍然不知道发生了什么,请附上调试数据,以便其他人知道问题所在
回答
它们都变成三个的原因是您在循环内进行重定向。意思是,在循环迭代 0 上你执行你的 <script>window.location = 'bla'
,它试图直接重定向(不是直接,而是在其他人有机会做任何事情之前)
您需要将 if
子句移到循环块之外和之后。
其他说明
- 请不要将
POST
变量直接输入 SQL 查询。请。
- PHPheaders优于脚本重定向。您的评论提到它不起作用,但您使用的是 WP,甚至可以考虑使用 wp_redirect 方法 https://developer.wordpress.org/reference/functions/wp_redirect/
- 删除
<br/>
。如果您已正确分离逻辑并正确查看,则提交处理程序 中散落的 html 为零
我有一个从数据库填充数据的表单,用户通过单击复选框来选择感兴趣的各个行。一切都已成功运行,但我现在正尝试在提交表单时将页面重定向到 2 个位置之一,具体取决于所选行数。 (选中复选标记。)这是出于安全原因压缩的当前脚本。
if(isset($_POST['submit'])){
$DELETE = "DELETE FROM job_picks WHERE ID='$userid'";
$wpdb->query($DELETE);
foreach($_POST['check_list'] as $selected){
$rowCount = count($_POST['ref']);
for ($i=0; $i<$rowCount;$i++)
{
if ($selected == $_POST['ref'][$i])
$compcode = $_POST['COMPCODE'][$i];
$note = $_POST['note'][$i] ; // changed for security reasons
$time = $_POST['time'][$i]; // changed for security reasons
$date = $_POST['date'][$i]; // changed for security reasons
$SQL = " left out for security reasons but is correctly populating data";
$wpdb->query($SQL);
echo "<script>window.location='https://example.com/step2/';</script>";
} ?>
<br class="clear">
<?php } ?>
<br class="clear">
<?php } ?>
<br class="clear">
<?php }
?>
我试过换行
echo "<script>window.location='https://example.com/step2/';</script>";
到
if ($rowcount == 1) {
echo "<script>window.location='https://example.com/step3/';</script>";
}
else
echo "<script>window.location='https://example.com/step2/';</script>";
但似乎只将所有结果发送到第 3 步,而不是仅将那些只检查了一项的提交。任何见解都将不胜感激,片段总是有帮助的,在此先感谢。
我看到了你的 $rowCount
和 $rowount
,希望这只是错别字。
并且您可以调试循环中每个 $rowCount
的值。类似于您的调试代码:
foreach($_POST['check_list'] as $selected){
$rowCount = count($_POST['ref']);
//print the debug data
print_r($rowCount, true );
}
die();
此外,请确保 $_POST['ref']
有效。
仍然不知道发生了什么,请附上调试数据,以便其他人知道问题所在
回答
它们都变成三个的原因是您在循环内进行重定向。意思是,在循环迭代 0 上你执行你的 <script>window.location = 'bla'
,它试图直接重定向(不是直接,而是在其他人有机会做任何事情之前)
您需要将 if
子句移到循环块之外和之后。
其他说明
- 请不要将
POST
变量直接输入 SQL 查询。请。 - PHPheaders优于脚本重定向。您的评论提到它不起作用,但您使用的是 WP,甚至可以考虑使用 wp_redirect 方法 https://developer.wordpress.org/reference/functions/wp_redirect/
- 删除
<br/>
。如果您已正确分离逻辑并正确查看,则提交处理程序 中散落的 html 为零