0

I'm still studying php html, any help is appreciated.

I have code like this:

<form method="post">
    <input type="checkbox" id="option1" name="option" value="<?php echo "Hello World!"; ?>" />
    <label for="option1"> Do you want to print Hello World? </label> <br />
    <input type="checkbox" id="option2" name="option" value="<?php echo "Hello Brother" ?>" />
    <label for="option2"> Do you want to print Hello Brother?</label> <br />
    <input type="checkbox" id="option3" name="option" value="<?php echo "Hello Human" ?>" />
    <label for="option3"> Do you want to print Hello Human?</label> <br />
    <br />
    <br />
    <input type="submit" id="Submit" name="Submit" value="Submit"/>
    
</form>
<?php
        
        if(empty($_POST["option"])){
            echo "You need to choose at least one!";
        }
        
        else {
            echo "Print successful!";
        }
    
    ?>

I want to have function that if checked, then print the value in different php file. I also have problem that when I checked and submit, the check mark disappeared.

I want it to be like, if checked then true, print the value. if not checked then false, do not print the value. Any idea? Thank you very much!

2
  • 1
    on Submit. your page is refreshed and that's why checkmark is disappeared. bcoz after refresh they get default value So until you don't store that checkbox value its not gone work for you. Commented Aug 30, 2020 at 9:45
  • phpfiddle.org/main/code/brkr-u9st Commented Aug 30, 2020 at 10:26

2 Answers 2

1

You have to use action="#" in form tag and mentioned the page name.

Working Demo: http://phpfiddle.org/main/code/brkr-u9st

    <input type="checkbox" id="option1" name="option1" value="<?php echo "Hello World!"; ?>" <?php if(isset($_POST['option1'])) echo "checked='checked'"; ?>  />
    <label for="option1"> Do you want to print Hello World? </label> <br />
    <input type="checkbox" id="option2" name="option2" value="<?php echo "Hello Brother" ?>" <?php if(isset($_POST['option2'])) echo "checked='checked'"; ?>  /> 
    <label for="option2"> Do you want to print Hello Brother?</label> <br />
    <input type="checkbox" id="option3" name="option3" value="<?php echo "Hello Human" ?>" <?php if(isset($_POST['option3'])) echo "checked='checked'"; ?>  />
    <label for="option3"> Do you want to print Hello Human?</label> <br />
    <br />
    <br />
    <input type="submit" id="Submit" name="Submit" value="Submit"/>
    
</form>

<?php
        
        if(empty($_POST["option1"]) && empty($_POST["option2"]) && empty($_POST["option3"]) ){
            
            echo "You need to choose at least one!";
        }
        else {

                if(isset($_POST["option1"])){

                    echo $_POST["option1"];
                 } 
                 
                 if(isset($_POST["option2"])){

                    echo $_POST["option2"];
                 }
                
                if(isset($_POST["option3"])){

                    echo $_POST["option3"];
                 }
               
              
        }
    
?>
Sign up to request clarification or add additional context in comments.

5 Comments

Dear sir, thanks for your answer. If you not mind, I want to ask how to print the value in different php file?
ok wait I will update it, on my working demo give me a min.
@Hendra Please Check Update Link: phpfiddle.org/main/code/brkr-u9st
thank you very much! I think I know how the code running. I will search solution to execute in another php file. I'm still newbie, thank you for answering my newbie question.
@Hendra hey No Problem, I have one suggestion for you, use youtube & w3school for reference if you are new. so you get a better idea of core PHP. :)
0

You need to have your form action attribute pointed to a PHP file. Then that file will receive the data via POST and you can do whatever you'd like with it there. The action attribute looks like this

<form method="post" action="\path\to\phpfile.php">

Also, the reason the check mark and other values disappear is because when you submit the form, it is currently executing the PHP file that code is written in. Essentially resetting everything. You can retain the input values by passing the POST input values into the HTML. It would look like this


<form method="post">
    <input type="checkbox" id="option1" name="option1" value="<?php echo "Hello World!"; ?>" <?php if(isset($_POST['option1']){ echo 'checked="checked"';} ?>/>
    <label for="option1"> Do you want to print Hello World? </label> <br />
    <input type="checkbox" id="option2" name="option2" value="<?php echo "Hello Brother" ?>" <?php if(isset($_POST['option2']){ echo 'checked="checked"';} ?> />
    <label for="option2"> Do you want to print Hello Brother?</label> <br />
    <input type="checkbox" id="option3" name="option3" value="<?php echo "Hello Human" ?>" <?php if(isset($_POST['option3']){ echo 'checked="checked"';} ?> />
    <label for="option3"> Do you want to print Hello Human?</label> <br />
    <br />
    <br />
    <input type="submit" id="Submit" name="Submit" value="Submit"/>
    
</form>
<?php
        
        if(empty($_POST["option"])){
            echo "You need to choose at least one!";
        }
        
        else {
            echo "Print successful!";
        }
    
?>

Make sure your input values don't have the same name.

1 Comment

Okay, I will remember to not give the same name. Sorry, I'm still newbie, I will code more further. If you not mind, if I have question again I will ask again.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.