2

ユーザーが選択ボックスを介してフォームに他のユーザーを追加できるようにしようとしています。したがって、ユーザーはドロップダウン選択から別のユーザーを選択し、[ユーザーの追加] を押すと、それらのユーザーが配列に追加されます。ユーザーは別のユーザーを選択でき、その 2 番目のユーザーをこの配列に追加する必要があります。現在、ユーザーが追加されるたびに、元のユーザーは消えます。非表示の入力を使用してみましたが、うまくいきません。

formprocessing.php について

 //this generates a list of users. The code to actually generate the users is omitted
 <center><h3>Invite Friends</h3></center><br>
     <form method="post">
     <tr><td>Friends:</td><td><select name ="friend"><Option value="">Friends<? echo $selection; ?></Select></td>
     <input type="hidden" name="hiddenFriends" value="<? echo $friends; ?>"/>
     <td><input type="submit" name="submitFriend" value="Add Friend"/>
     </form>

form.php について

//i'm trying to create an array of users which are added. When completed, they'll be added to the db
<?
if(isset($_POST['submitFriend'])){
    if(count($_POST['hiddenFriends']) > 0){
        $friend = $_POST['friend'];
        array_push($friends,$_POST['friend']);
    }
    else{
        $friends = array('');
        array_push($friends,$_POST['friend']);
    }
}
?><input type="hidden" name="hiddenFriends" value="<? echo $friends; ?>"/><?
print_r($friends);

?>
4

1 に答える 1

2

PHP配列をhtml入力に追加しようとしています。その不可能

しかし、explode 関数と implode 関数を使用できることを心配する必要はありません。

<?
$friends = array();
if(isset($_POST['submitFriend'])){

    if(strlen($_POST['hiddenFriends']) > 0){
        $friends = explode(',',$_POST['hiddenFriends']);                
    }    
    $friends[] = $_POST['friend'];
}
?>
<input type="hidden" name="hiddenFriends" value="<? echo implode(',',$friends); ?>"/>
于 2012-10-07T01:35:28.603 に答える