简体   繁体   中英

How do I display result on the same page if I meet condition in php?

I want to display a checkbox on the same page if the second option is chosen, could you help me with the code please.

Here is my current code:

<form action="form1.php" method="post">
<?php

$company = array (1 => 'One', 'Two', 'Three');
echo '<fieldset>
<select name="companys">';
foreach ($company as $key => $value) {
    echo "<option value=\"$key\">$value</option>\n";
}
echo '</select>';
if ($company == 2) {
    echo'<p><input type="checkbox" name="tandc" value="terms" />I accept the terms and conditions
    </p>';
} else {
    echo 'OK';
};
echo '</fieldset>';
?>
</form>

Assuming you post the form somewhere, and the current page is form1.php.

<form action="form1.php" method="post">
<?php

$company = array(1 => 'One', 2 => 'Two', 3 => 'Three');
echo '<fieldset>
    <select name="companys">';

        foreach ($company as $key => $value) {
            echo "<option value=\"$key\">$value</option>\n";
        }

echo '</select>';

if (isset($_POST['companys']) && $_POST['companys'] == 2) {
    echo'<p><input type="checkbox" name="tandc" value="terms" />I accept the terms and conditions</p>';
} else {
    echo 'OK';
}

echo '</fieldset>';

?>
</form>
***if ($company == 2) {***

$company is defined as array but you have mentioned as string. please check.

Try this:

<form action="#" method="post">
<?php

$company = array (1 => 'One', 'Two', 'Three');
echo '<fieldset>
<select name="companys">';
foreach ($company as $key => $value) {
    echo "<option value=\"$key\" onclick='this.form.submit()'>$value</option>\n";
}
echo '</select>';
if ($_POST['companys'] == 2) {
    echo'<p><input type="checkbox" name="tandc" value="terms" onclick="this.form.submit()"/>I accept the terms and conditions
 </p>';
} else {
    echo 'OK';
};
echo '</fieldset>';
?>
</form>

You do not need to submit form to just show an element if dropdown value matches condition you can simply use javascript function to make it possible see example code below

<form action="form1.php" method="post">
    <?php

    $company = array(1 => 'One', 2 => 'Two', 3 => 'Three');
    echo '<fieldset>
        <select name="companys" onchange="check_option(this.value);">';

            foreach ($company as $key => $value) {
                echo "<option value=\"$key\">$value</option>\n";
            }

    echo '</select>';


    echo'<p><input type="checkbox" id="terms" name="tandc" value="terms"  style="display:none;"/>I accept the terms and conditions</p>';


    echo '</fieldset>';

    ?>
    </form>
    <script>
        function check_option(val)
        {
            if(val=='Two')
            {
                document.getElementById('terms').style.display='block';
            }
        }
    </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM