dongqian5639 2014-01-09 15:22
浏览 39
已采纳

每个php的多个帖子值

So I am working with some checkboxes that are populated from a db table. Which looks like this.

<form action="" method="post">
<?php
foreach ($chores as $retrieved_search){ 
?>
<tr>
<td>
<?php echo "<input type='checkbox' id=".$retrieved_search->id." value=".$retrieved_search->image." name='chores[]'>$retrieved_search->name";?>
<?php echo "<input type='text' value='$retrieved_search->name' id='optionName' name='optionName[]' />"?>
</td>
</tr>
<?php
}
?>
</form>

I am trying to figure out a way that I can get that value from the checkbox which I am currently doing and works fine. The value from the checkbox is an image URL.

I also need to get the name associated with the checkbox so I added another input field that is populating the name of the checkbox which I will then hide.

So this is my php statement that is getting the post value from the checkbox but how can I also get the value from the input field for the same insert statement?

<?php
if(isset($_POST['saveOptions'])) {

        global $wpdb;        
        $table_name = $wpdb->prefix . "chartUsersOptions";
        $user_ID = get_current_user_id();
        $typeChore = "chore";
        $typeBehavior = "behavior";

if(isset($_POST['chores'])){
        foreach ($_POST['chores'] as $chores) {

            $wpdb->insert( $table_name, array( 
                'userId' => $user_ID,
                'optionImage' => $chores, 
                'type' => $typeChore,
                ));
         }
        }

        $msg = "Saved now redirect to 3rd step"; 

        echo $msg;
    }
    else{
.............
    }
?>

EDIT:

Based on a suggestion This worked.

<?php
foreach ($chores as $retrieved_search){ 
echo "<input type='checkbox' value='{$retrieved_search->image}' name='chores[{$retrieved_search->id}]'>{$retrieved_search->name}";
echo "<input type='text' value='{$retrieved_search->name}' name='optionName[{$retrieved_search->id}]'>"
}
?>

And This

if(isset($_POST['chores'])){
        foreach ($_POST['chores'] as $chores_key => $chores) {
            $text_input_value = $_POST['optionName'][$chores_key];  
            $wpdb->insert( $table_name, array( 
                'userId' => $user_ID,
                'optionImage' => $chores, 
                'optionName' => $text_input_value,
                'type' => $typeChore,
                ));
         }
        }

But optionName is still inserting blank.

  • 写回答

2条回答 默认 最新

  • doupao1530 2014-01-09 15:33
    关注

    I'm not sure what the exact problem is, but my guess is that your checkboxes don't match your input boxes. That is because unchecked checkboxes do not get sent to the server, so you probably end up with an checkboxes array that is smaller than the input boxes array.

    You should change your html so that a checkbox-array-key always matches an input-array-key, so something like:

    echo "<input type='checkbox' value='{$retrieved_search->image}' name='chores[{$retrieved_search->id}]'>{$retrieved_search->name}";
    echo "<input type='text' value='{$retrieved_search->name}' name='optionName[{$retrieved_search->id}]'>"
    

    Also note that ID's need to be unique so I removed them but that is probably not related to your problem.

    Edit: To get the value of the text box after making the above changed, you can do:

    if (isset($_POST['chores'])) {
        foreach ($_POST['chores'] as $chores_key => $chores) {
            $text_input_value = $_POST['optionName'][$chores_key];
            // and the rest of your code
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services