user2161017
user2161017

Reputation: 1

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>

Upvotes: 0

Views: 365

Answers (4)

Veerendra
Veerendra

Reputation: 2622

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>

Upvotes: 0

Jesse
Jesse

Reputation: 479

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>

Upvotes: 1

Zword
Zword

Reputation: 6793

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>

Upvotes: 0

Naveenkumar
Naveenkumar

Reputation: 7

***if ($company == 2) {***

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

Upvotes: 0

Related Questions