Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    May 2013
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Get checked checkboxes on form submit and insert values into textarea

    I am having trouble with forms!

    The picture below is how i would like to have the interface set up. Basically it consists of a textarea, then a form with 'Submit' buttons, then a drop down box.
    The red outlined box is a div whose contents changes depending on the value selected from the dropdown box, whose current value is 'Player'.

    I can't seem to get the following behaviour to work: depending on the number of checkboxes clicked, output a phrase in the textarea a certain number of times upon pressing the 'Defend' button.



    The code I have so far is as follows:
    Code:
    <?php
        ob_start();
        // First we execute our common code to connection to the database and start the session 
        define('MyConst', TRUE);
    	
    	include('../database.class.php');
     	include('../table.class.php'); 
     	include('../user.class.php');
    	include('../loginattempts.class.php');
    	include('../timer.class.php');
        include('../characters.class.php');
        include('../weapontype.class.php');
        include('../objects/weapons/weaponobject.class.php');
        include('../objects/weapons/bowieknife.class.php');
        include('../npc/enemy.class.php');
        include('../npc/skinhead.class.php');
        include('../npc.class.php');
        include('../npctype.class.php');
     	include('../functions.php');
        include('../loginf.php');  
     	include('locationf.php');
    	
    	$dbo = database::getInstance();
    	$dbo -> connect("*************", "**********", "*********", "**************", array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')); 
    	
    	secSessionStart();
    
        // At the top of the page we check to see whether the user is logged in or not 
        if(empty($_SESSION['user'])) 
        { 
            // If they are not, we redirect them to the login page. 
            header("Location: login.php"); 
             
            // Remember that this die statement is absolutely critical.  Without it, 
            // people can view your members-only content without logging in. 
            die("Redirecting to login.php"); 
        }
        
        $_SESSION['currentlocation'] = "combat.php";
    ?>    
    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
        <script type="text/javascript">
            if(document.getElementById("wrapper") == null) {
                window.location = "../index.php";    
            }
        </script>
        
        <textarea id="combatinfo" rows="4" cols="50"></textarea>
        <br />   
    <?php 
        if(isset($_POST['defend'])) {
    ?>
    
    <script type="text/javascript">
    var selected = new Array();
    
    
    
    </script>
    <?php        
            if($_SESSION['currentComboAttack'] < $_SESSION['maxComboAttack']) {
                $_SESSION['currentComboAttack']++;
                
                for($i = 0; $i < sizeOf($_SESSION['enemies']); $i++) {
                    $clones = $_SESSION['playercharacter']->clones; 
                    $numOfClones = sizeOf($_SESSION['playercharacter']->clones);
                    $clones[$numOfClones] = $_SESSION['playercharacter'];
                    
                    for($j = 0; $j < sizeOf($clones); $j++) {
                        if (in_array($_SESSION['enemies'][$i], $clones[$j]->defendAgainst)) {
                            if($_SESSION['enemies'][$i]->target1 === $clone[$j]) {
                                if($_SESSION['enemies'][$i]->attackType != "miss") {
                                    $_SESSION['enemies'][$i]->attackMessage = $_SESSION['enemies'][$i]->attackMessage."Clone #".strval($j + 1)."defends.";
                                    $loss = $_SESSION['enemies'][$i]->attackValue - $clone[$j]->defend($_SESSION['enemies'][$i]);
                                    
                                    if($clone[$j]->objectType == "player") {
                                        $_SESSION['enemies'][$i]->attackMessage = $_SESSION['enemies'][$i] ->attackMessage."You lose".strval($loss)."health.";        
                                    }
                                    else {
                                        $_SESSION['enemies'][$i]->attackMessage = $_SESSION['enemies'][$i] ->attackMessage."Clone #".strval($j +1). "loses".strval($loss)."health.";
                                    } 
                                    $clone[$j]->health = $clone[$j]->health - $loss;
                                }    
                            }
                            else if($_SESSION['enemies'][$i]->target2 === $clone[$j]) {
                                if($_SESSION['enemies'][$i]->attackType != "miss") {
                                    $_SESSION['enemies'][$i]->attackMessage = $_SESSION['enemies'][$i]->attackMessage."Clone #".strval($j + 1)."defends.";
                                    $loss = $_SESSION['enemies'][$i]->attackValue - $clone[$j]->defend($_SESSION['enemies'][$i]);
                                    
                                    if($clone[$j]->objectType == "player") {
                                        $_SESSION['enemies'][$i]->attackMessage = $_SESSION['enemies'][$i] ->attackMessage."You lose".strval($loss)."health.";        
                                    }
                                    else {
                                        $_SESSION['enemies'][$i]->attackMessage = $_SESSION['enemies'][$i] ->attackMessage."Clone #".strval($j +1). "loses".strval($loss)."health.";
                                    } 
                                    $clone[$j]->health = $clone[$j]->health - $loss;
                                }    
                            }
                        }
                        else if(!in_array($_SESSION['enemies'][$i], $clones[$j]->defendAgainst)) {
                            if($_SESSION['enemies'][$i]->target1 === $clone[$j]) {
                                if($clone[$j]->objectType == "player") {
                                    $_SESSION['enemies'][$i]->attackMessage = $_SESSION['enemies'][$i] ->attackMessage."You lose".strval($_SESSION['enemies'][$i]->attackValue)."health.";        
                                }
                                else {
                                    $_SESSION['enemies'][$i]->attackMessage = $_SESSION['enemies'][$i] ->attackMessage."Clone #".strval($j + 1). "loses".strval($_SESSION['enemies'][$i]->attackValue)."health.";
                                }
                                $clone[$j]->health = $clone[$j]->health - $_SESSION['enemies'][$i]->attackValue;
                            }
                            else if($_SESSION['enemies'][$i]->target2 === $clone[$j]) {
                                if($clone[$j]->objectType == "player") {
                                    $_SESSION['enemies'][$i]->attackMessage = $_SESSION['enemies'][$i] ->attackMessage."You lose".strval($_SESSION['enemies'][$i]->attackValue)."health.";        
                                }
                                else {
                                    $_SESSION['enemies'][$i]->attackMessage = $_SESSION['enemies'][$i] ->attackMessage."Clone #".strval($j + 1). "loses".strval($_SESSION['enemies'][$i]->attackValue)."health.";
                                }
                                
                                $clone[$j]->health = $clone[$j]->health - $_SESSION['enemies'][$i]->attackValue;
                            }
                        }
                    }    
                }    
    ?>
        <form action="index.php" method="post">
        <input type="submit" name="defend" value="Defend" />
        <input type="submit" name="flee" value="Flee" />
        </form>
    <?php             
            }    
        }
        $_SESSION['currentlocation'] = "combat.php"; 
    ?>    
    <?php
        $_SESSION['currentlocation'] = "combat.php";
        $_SESSION['currentComboAttack'] = 1; 
        if($_SESSION['ambush'] && $_SESSION['flee']) {
            
            for($i = 0; $i < sizeOf($_SESSION['enemies']); $i++) {
                $scaling = 20 - $_SESSION['enemies'][$i] -> luck;
                if($_SESSION['enemies'][$i]-> rank >= 20 && $_SESSION['enemies'][$i]-> rank < 40) {
                    $prob = rand(1, 100);
                    
                    if($prob <= 100) {              
    //                if($prob <= 75 - $scaling * 3.8) {
                        if($_SESSION['maxComboAttack'] < 2) {
                            $_SESSION['maxComboAttack'] = 2;
                        }
                        
                        $_SESSION['enemies'][$i]->comboAttack = 2;
                        $_SESSION['enemies'][$i]->attackMessage="Enemy #".strval($i + 1)." pulls off a 2-hit combo.
    ";
            
                    }
                    
                }
                else if($_SESSION['enemies'][$i]-> rank >= 40 && $_SESSION['enemies'][$i]-> rank < 60) {
                    $prob = rand(1, 100);
                    
                    if($prob <= 50 - $scaling * 3.8) {
                        if($_SESSION['maxComboAttack'] < 3) {
                            $_SESSION['maxComboAttack'] = 3;
                        }
                        
                        $_SESSION['enemies'][$i]->comboAttack = 3;
                        $_SESSION['enemies'][$i]->attackMessage="Enemy #".strval($i + 1)." pulls off a 3-hit combo.";
    
                    }
                    else if($prob <= 75 - $scaling * 3.8) {
                        if($_SESSION['maxComboAttack'] < 2) {
                            $_SESSION['maxComboAttack'] = 2;
                        }
                        
                        $_SESSION['enemies'][$i]->comboAttack = 2;
                            
                    }
                }
                
                if($_SESSION['enemies'][$i] != null) {
                   $_SESSION['enemies'][$i]->attack($i + 1); 
                   // echo $_SESSION['enemies'][$i]->attackMessage;                
                }    
    ?>                  
        <script type="text/javascript">
            $(document).ready(function() {
                
                $('#combatinfo').append('<?php echo $_SESSION['enemies'][$i]->attackMessage;?>'); 
                                       
            });
        </script>    
    <?php      
            }
    ?>
        <form action="index.php" method="post">
        <input type="submit" name="defend" value="Defend" />
        <input type="submit" name="flee" value="Flee" />
        </form>
    <?php           
        }
        else if(ambush && !flee) {
    ?>        
        <button type="button">Defend</button> <button type="button">Flee</button> 
    <?php
        }
        if(!ambush && flee) {
    ?>        
        <button type="button">Attack</button> <button type="button">Flee</button>
    <?php
        }
        else if(!ambush && !flee) {
    ?>        
        <button type="button">Defend</button> <button type="button">Flee</button>
    <?php     
        }
    ?>
        <br />
        <select id="selectClone">        
    <?php
        if(sizeOf($_SESSION['playercharacter'] -> clones) == 0) {
    ?>
            <script type="text/javascript">
                $(document).ready(function() {
                    var dropDownMenu = document.getElementById("selectClone");
                    dropDownMenu.disabled = true;
                    
                });
            </script>             
            <option value="Player">Player</option>
    <?php        
        }
        else if(sizeOf($_SESSION['playercharacter'] -> clones) == 1) {
    ?>
            <option value="Clone 1">Clone 1</option>              
    <?php
        }
    ?>            
        </select>
        <div id="player" class="group">
        <input name="YourChoice" type="checkbox" value="fish" <?php if($_POST['YourChoice']=="fish") echo "checked=checked"; ?> > Fish
        <input name="YourChoice" type="checkbox" value="meat" <?php if($_POST['YourChoice']=="meat") echo "checked=checked"; ?> > Meat
        </div>
        <div id="clone1" class="group">dsfdsfds</div>  
        
        <script type="text/javascript">
            $(document).ready(function() {
                $('.group').hide();
                $('#player').show();
                $('#selectClone').change(function () {
                    $('.group').hide();
                    $('#'+$(this).val()).show();
                })
                
                $('#player').children().each(function() {
                   if($(this).attr("checked")) 
        $('#combatinfo').append("DOG");
    })
       });     
        </script>
    </body>
    </html>
    Last edited by pintee; 06-18-2013 at 09:40 AM.

  • #2
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,302
    Thanks
    28
    Thanked 276 Times in 270 Posts
    Quote Originally Posted by pintee View Post
    I can't seem to get the following behaviour to work: depending on the number of checkboxes clicked, output a phrase in the textarea a certain number of times upon pressing the 'Defend' button.
    This seems to be a PHP problem and my PHP skills suck, but I'll give it a try:

    PHP Code:
    <textarea id="combatinfo" rows="4" cols="50">
    <?php
        
    if (isset($_POST['defend'])) {
            
    $combat_messages "";
            foreach(
    $_POST['YourChoice'] as $enemy){
                if (
    $combat_messages !== "") {
                    
    $combat_messages .= "\r\n";
                }
                if (
    $enemy === "fish") {
                    
    $combat_messages .= "You defended against GOLDFISH!";
                }
                elseif (
    $enemy === "meat") {
                    
    $combat_messages .= "You defended against DELICIOUS RIBEYE!";
                }
            }
            echo 
    $combat_messages;
        }
    ?>
    </textarea>
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #3
    New Coder
    Join Date
    May 2013
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I don't think this is what I'm after. Thanks for trying anyway.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •