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 to the CF scene
    Join Date
    Feb 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    php form submit only data that was used

    I am creating a scoring system for deer and I have a form that asks for the measurements. I also have an option that adds more forms (if the deer has more than 4 tines, or 4 abnormal points). Now when I submit my form it sends it a /add.php. I have it so it displays the original forms and then I want to make it so it shows the addition forms. BUT only if they used that form. So basically I want it to display the measurement in the fields ONLY if they were used.

    Code:
    <?php
    $a = $_GET['a'];
    $b = $_GET['b'];
    $c = $_GET['c'];
    $d = $_GET['d'];
    //....continueing on....//
    
    $answer = $a + $b + $c + $d + $e + $f + //....continue...//
    
    ?>
    I have the above code in my /add.php to pull in the information from my forms.


    Code:
    <table width="400" id="file_d" style="display:none" cellspacing="0" cellpadding="0" border="0">
    <tr>
    <td width="35"><b>Ab6</b></td>
    <td width="100"><input type="text" name="pp"></td>
    <td width="50">&nbsp;</td>
    <td width="100"><input type="text" name="qq"></td>
    </tr>
    <tr>
    <td colspan="4">
    <a href="#" id="link_d" onclick="new_field('file_e');hide_mais('link_d')">Add More</a>
    </td></tr><br></table>
    The above code is to add more fields (along with a ID set in the table). Basically when you click add more it adds the next field. So for the above example it displays "D" and then when clicked add more it displays "E" which is just another table. This above code is in my forms on a seperate page

    Code:
    <input type="text" name="pp">
    The above is an example of one of the forms that im using.

    I am wanting to display the original fields (a,b,c,d,etc..) and then if someone had clicked "add more" and entered in another field. I want that field to be displayed as well (aa,bb,cc,etc...)


    I tried something like


    Code:
    <?php
    if (!isset($_POST['q']) || empty($_POST['q']))
    {
    echo "<tr>
    <td width='40'>G5:</td>
    <td width='50'><?php echo $q; ?></td>
    </tr>";
    
    }
    ?>
    But that just displays the field regardless of whether or not information was put in.

    I dont know much about php so any help would be appreciated

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You actually want to use arrays for these. You can name them if you want, but if the name's have no actual value by themselves, than a list is what you'll get.
    PHP retrieves arrays from html with the following conventions: <input type="text" name="yourname[specificoffset]" /> where you can now find the value in $_POST['yourname']['specificoffset']. If no specific is required, than a simple [] will suffice. 'yourname' is whatever you want to call it.

    Then in processing you simply sum them up:
    PHP Code:
    if (isset($_POST['yourname']))
    {
        
    $total array_sum($_POST['yourname']);

    Or iteratively with a for or foreach loop.
    Naming them is useful if you want to display some info for them. You can retrieve that from the key() of the array during iteration, or $key from the foreach:
    PHP Code:
    foreach ($_POST['yourname'] AS $k => $v)
    {
        
    printf('Value for %s is %s' PHP_EOL$k$v);

    Like that.

    This works well with JS as well as you can create the item with the same naming convention and append it to the dom forms.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts
    As for the part you tried, Fou-Lu's way is easiest

    PHP Code:
    foreach($_GET['yourname'] as $k => $v){
    echo 
    "<tr>
    <td width='40'>"
    .$k.":</td>
    <td width='50'>"
    .$v."</td>
    </tr>"
    ;

    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month


  •  

    Posting Permissions

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