0

i am knew to php, and i want to know if there is a way to show the input based on the names that were checked, for example i have cat, dog and fish, if i choose dog and fish, i have to type the names of those animals.

here is the code to ask what animal do you have, and by checking and submit, it appears on the other page

<body>

<?php 

$check_list[]=isset($_GET["check_list"])?$_GET["check_list"]:"";

if(!empty($_POST['check_list'])) {
    foreach($_POST['check_list'] as $check) {
        echo $check; 
        }
}
?>

<form action="names.php" method="POST">
    <h1>choose the animals you have, and type their names</h1>

<input type="checkbox" name="check_list[]" value="cat">
cat
<br>
<input type="checkbox" name="check_list[]" value="dog" >
dog
<br>
<input type="checkbox" name="check_list[]" value="fish" >
fish
<br>

<input type="submit" value="next">

</form>

and here is the other page, that show what animals you have selected, and on this page i want to show the inputs based on the checked

<body>
<?php

    echo '<h3>You have select following animals</h3>';
    foreach ( $_POST ["check_list"] as $animals )
        echo '<p>' . $animals . '</p>';
    ?>

  • 1
    you're mixing GETs with POST, why? and `$check_list[]` there shouldn't be brackets there – Funk Forty Niner Jul 30 '16 at 14:42
  • And the problem is? – u_mulder Jul 30 '16 at 14:43
  • i dont just want to show what was checked, i want to show the input as well, for the person to type the name of his animals – André Yonamine Jul 30 '16 at 14:49
  • if you are new to PHP - and even after - over use the debug function: `print_r()`. try `print_r($_POST)` it will always help you understand how to parse the result. – antoni Jul 30 '16 at 14:49
  • also as @Fred-ii- said I think you are mixing wrongly GET and POST unless you don't show the full code. In this piece of code nothing is available in $_GET everything in $_POST – antoni Jul 30 '16 at 14:51
  • To show your input, just echo the html for the input in your names.php. Just like you are doing to echo which animals here selected. Inside that loop, echo the HTML for the input, and use your loop variable `$animals` if needed to output a certain name in the input – sidyll Jul 30 '16 at 15:57
  • hi @sidyll you understand what i want!!!!, but i dont know how to do this, i know that my array search if is checked and bring to me, but how do i do with the input text ? – André Yonamine Jul 30 '16 at 20:03
  • @AndréYonamine here is a suggestion: https://gist.github.com/anonymous/783169e9305e92efcfc5bb3783f5005d – sidyll Jul 30 '16 at 20:59
  • @sidyll hey mate thanks !!!, that was exactly what i was looking for. – André Yonamine Jul 31 '16 at 15:17

1 Answers1

1

So in your code,

just replace $check_list[]=isset($_GET["check_list"])?$_GET["check_list"]:""; with $check_list = isset($_POST["check_list"]) ? $_POST["check_list"] : ""; and you should be good.

Also your variable $animals always carry one animal so it should be named animal.

and this:

$check_list[]=isset($_GET["check_list"])?$_GET["check_list"]:"";

if(!empty($_POST['check_list'])) {
    foreach($_POST['check_list'] as $check) {
        echo $check; 
        }
}

can be improved to:

$check_list = isset($_GET["check_list"]) ? $_GET["check_list"] : null;

if ($check_list) {
    foreach($check_list as $animal) {
        echo "<p>My animal is: $animal</p>"; 
    }
}

Next step is to learn more about security checks on user data!

Respectfully, good luck for you learning. PHP is beautiful ;)

antoni
  • 5,001
  • 1
  • 35
  • 44