0

i can't find the error every thing ok expect check box value not getting as list

<?php

if(isset($_POST['sub']))
{
    $fname = $_POST['fname'];
    $age = $_POST['age'];
    $comment = $_POST['comment'];
    $gender = $_POST['gender'];
    $hobby_temp = $_POST['hobby'];
    $size = count($hobby_temp);
    for($i = 0; $i < size; $i++)
    {
        $hobby=$hobby.','.$hobby_temp[$i];
    }
    echo 'Name:'.$fname.'<br>';
    echo 'age:'.$age.'<br>';
    echo 'comment:'.$comment.'<br>';
    echo 'gender:'.$gender.'<br>';
    echo 'Hobbies:'.$hobby.'<br>';
}
?>

note : every thing display expect hobbies

4

3 回答 3

0

使用内爆

$hobby = isset($_POST["hobby"])?implode(",",$_POST["hobby"]):"");
于 2013-06-09T19:20:19.377 回答
0

你的问题在这里:

$hobby=$hobby.','.$hobby_temp[$i];

它应该是

$hobby = "" //initialize outside the loop
for($i = 0; $i < $size; $i++){
    if($i==0) $hobby = $hobby_temp[$i];
    else $hobby = $hobby .', '.hobby_temp[$i] ;
}
于 2013-06-09T18:46:10.143 回答
0

您正在覆盖您的$hobbyat 每个循环,而不是您应该连接您的变量

$hobby = '';
for($i = 0; $i < size; $i++)
{
    $hobby .= $hobby_temp[$i] . ',';
}
$hobby = substr_replace($hobby, '', -1);
于 2013-06-09T18:46:16.487 回答