Execute input value in different php file

167 views Asked by At

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

There are 2 answers

5
RïshïKêsh Kümar On BEST ANSWER

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"];
                 }
               
              
        }
    
?>
1
Benjamen Kuhman On

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.