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 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts

    need help in php forms

    hi i want to make a form. the form will have three text fields and with two buttons. what i want with this form is when the user enters the first three fields and if he wants to add more then he will click on add more button and on the same page three more fields will appear below the first three fields. the user will then enter these three fields then if he wants to add more then he will click on add more button. so three more fields will appear below the first six fields. user will enter these fields. he will be allowed to enter upto eight or less than eight times. once he finishes with this then he will click the second button to insert this data into db.
    i create the following form but it only works just for two times. how can i achieve this?

    my code is here
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Untitled Document</title>
    </head>
    
    <body>
    <form name="form1" method="post" action="cvEducation.php">
      <label>Degree:
      <input type="text" name="degree1" id="degree1">
      </label>
      <p>
        <label>CGPA/Grade:
        <input type="text" name="cgpa1" id="cgpa1">
        </label>
      </p>
      <p>
        <label>Institute:
        <input type="text" name="institute1" id="institute1">
        </label>
      </p>
      <p>
        <label>
        <?php if(!isset($_POST['return'])) { ?>
        <input type="submit" name="return" id="return" value="Save And Add Another">
        <?php } ?>
        </label>
        <?php
    if(isset($_POST['return']))
    { ?>
      <label>Degree:
      <input type="text" name="degree2" id="degree2">
      </label>
      <p>
        <label>CGPA/Grade:
        <input type="text" name="cgpa2" id="cgpa2">
        </label>
      </p>
      <p>
        <label>Institute:
        <input type="text" name="institute2" id="institute2">
        </label>
      </p>
      <p>
        <label>
        <input type="submit" name="return" id="return" value="Add More">
        </label>
    </p>
    <?php }
    ?>
    </p>
    </form>
    </body>
    </html>
    in this form i provide only one button but it will be two once this problem is solved.

  • #2
    met
    met is offline
    Regular Coder
    Join Date
    Oct 2009
    Location
    United Kingdom
    Posts
    728
    Thanks
    4
    Thanked 119 Times in 119 Posts
    the reason it only works once is because your code is simply displaying those three boxes everytime the button is clicked. you need to code a way to handle if the button is submitted more than once, otherwise it wil just repeat itself, hence you only see the same 3 every time.

    this might be what you are after
    http://www.dustindiaz.com/add-and-re...th-javascript/

    this kind of effect can be achieved wtih PHP but its probably easier to achieve with JS/ajax or similar.

    you could put the extra fields in to several <divs> with their display:hidden property, then if an add-more is clicked, change it to display:visible

    l googled dynamically adding form fields, there's a tonne of stuff available


    edit
    unless I misunderstand and you just want a way for the user to add another record after they submit the form.. in that case

    PHP Code:
    if(isset($_POST['return'])) {

    // process form 

    }

    // display form 

    /* on the same page, it will insert the record to DB 
    and provide the form again on teh same page, allowing you to add another record. */ 
    Last edited by met; 10-24-2009 at 02:06 PM.


  •  

    Posting Permissions

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