php question

Discussion in 'Web Design and Development' started by Mike Teezie, Feb 5, 2007.

  1. Mike Teezie macrumors 68020

    Mike Teezie

    Joined:
    Nov 20, 2002
    #1
    I know next to nothing about php.

    What I'm trying to do, is get my php to ignore (and not output) the boxes that aren't checked.

    Here is my html:

    HTML:
          <form action="checkboxes.php" method="post">
                    
                    <img src="http://mnjordan.com/images/doggie.jpg">
    				<h3>DD_01</h3>
    				<br>
                    <input type="checkbox" name="4x6"<?php if($4x6 == "on"){echo" CHECKED";}?>4x6
    			<input type="checkbox" name="5x5"<?php if($5x5 == "on"){echo" CHECKED";}?>5x5
                    <input type="checkbox" name="5x7"<?php if($5x7 == "on"){echo" CHECKED";}?>5x7
                    <input type="checkbox" name="8x10"<?php if($8x10 == "on"){echo" CHECKED";}?>8x10
                    <br>
    			<input type="checkbox" name="8x12"<?php if($8x12 == "on"){echo" CHECKED";}?>8x12
    			<input type="checkbox" name="10x10"<?php if($10x10 == "on"){echo" CHECKED";}?>10x10
                    <input type="checkbox" name="11x14"<?php if($11x14 == "on"){echo" CHECKED";}?>11x14
                    <input type="checkbox" name="12x18"<?php if($12x18 == "on"){echo" CHECKED";}?>12x18
    			<br>
    			<input type="checkbox" name="16x20"<?php if($16x20 == "on"){echo" CHECKED";}?>16x20
    			<input type="checkbox" name="8_Wal"<?php if($8_Wal == "on"){echo" CHECKED";}?>8 wallets
                    <input type="checkbox" name="24_Wal"<?php if($24_Wal == "on"){echo" CHECKED";}?>24 wallets
                    <br>
    				<input type="submit" name="submit" value="Submit">
                </form>
    And here is my php:

    PHP:
    <?php if (condition) {echo $_POST["4x6"];} ?>4x6
    <br><?php if (condition) {echo $_POST["5x5"];} ?>5x5
    <br><?php if (condition) {echo $_POST["5x7"];} ?>5x7
    <br><?php if (condition) {echo $_POST["8x10"];} ?>8x10
    <br><?php if (condition) {echo $_POST["8x12"];} ?>8x12
    <br><?php if (condition) {echo $_POST["10x10"];} ?>10x10
    <br><?php if (condition) {echo $_POST["11x14"];} ?>11x14
    <br><?php if (condition) {echo $_POST["12x18"];} ?>12x18
    <br><?php if (condition) {echo $_POST["16x20"];} ?>16x20
    <br><?php if (condition) {echo $_POST["8_Wal"];} ?>8_Wal
    <br><?php if (condition) {echo $_POST["24_Wal"];} ?>24_Wal
    Here is a link:

    http://mnjordan.com/checkboxes.htm

    What I would like to see the php generate is - say you selected 4x6 and 5x7, just 4x6 and 5x7 on the page.

    Like I said, my knowledge of php is non-existant. Please don't flame me if I've done something incredibly dumb.

    Thanks!
     
  2. nightelf macrumors 6502

    Joined:
    Mar 25, 2003
    #2
    You need to set a value to each checkbox.

    <input type="checkbox" name="4x6" value="true" <?php if ($_POST['4x6']) echo 'checked="checked"'; ?>/> 4x6

    <?php if ($_POST['4x6']) echo '4x6<br />'; ?>
     
  3. pengu macrumors 6502a

    Joined:
    Mar 20, 2005
    Location:
    Diddily Daddily...
    #3
    i suggest you do something a bit more robust, such as this:

    PHP:

    <?php

    function checkVar($varName$checkBox false)
    {
        if(isset(
    $_POST[$varName]) && !empty($_POST[$varName]))
        {
            if(
    $checkBox)
            {
                echo 
    'checked="checked"';
            }
            else
            {
                echo 
    $_POST[$varName];
            }
        }
    }


    ?>
    <input type="checkbox" name="4x6" value="true" <?php checkVar('4x6'true); ?>/> 4x6

    <?php checkVar('4x6'); ?><br />

     
  4. Mike Teezie thread starter macrumors 68020

    Mike Teezie

    Joined:
    Nov 20, 2002
    #4
    Guys, thanks so much for the help!

    If I wanted to use a text field, so a user could input a value there, how much more difficult would it be?

    For instance, if say a person put a "2" in the text field next to 4x6, I would like it to output "4x6 - 2" or something similar.

    Seriously though, thanks for all the help guys.
     
  5. pengu macrumors 6502a

    Joined:
    Mar 20, 2005
    Location:
    Diddily Daddily...
    #5
    then it becomes:
    PHP:
    <?php

    function checkVar($varName)
    {
        if(isset(
    $_POST[$varName]) && !empty($_POST[$varName]))
        {
         echo 
    $_POST[$varName];
        }
    }


    ?>
    <input type="text" name="4x6" value="<? checkVar('4x6'); ?>"/> 4x6

    4x6 - <?php checkVar('4x6'); ?><br />
     
  6. Mike Teezie thread starter macrumors 68020

    Mike Teezie

    Joined:
    Nov 20, 2002
    #6
    Thanks pengu!

    One more question - if no value is entered in a field, I want nothing at all to show up.

    PHP:
    "4x6 -" <?php checkVar('4x6'); ?><br />
    I realize the "4x6 -" outside the php is being generated by html. So I guess I'm asking how to make it show up only if a value is entered in the form.

    Here are the test files I'm working with:

    http://mnjordan.com/textfields2.htm and

    http://mnjordan.com/order.php

    Thanks a ton, you are teaching me a lot!
     
  7. pengu macrumors 6502a

    Joined:
    Mar 20, 2005
    Location:
    Diddily Daddily...
    #7
    ok, so you only want it to actually the label for 4x6 if it has a value?

    the simplest way (if you aren't doing anything else in the same form) would be this:

    PHP:
    foreach($_POST as $key => $val)
    {
    if(
    $key != 'submit_button_name')
    {
    if(!empty(
    $_POST[$key]))
    {
    echo(
    "$key$_POST[$key] <br />");
    }



    }

    extend the first if statement to ignore any other fields you have. the other way would be to set the input boxes to have the same name, such as '<input type="text" name="myField[4x6]" />'

    and then do a foreach on $_POST[myField[]]. PHP will recognize that you want the fields with the [] in the name as an array.
     

Share This Page