#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Question Submit separably triggered form parts at once


    Hi

    I'm using PHP to create forms for user to fill up. part of the form is not for everyone, so users who want to fill it up has to click on a box below the form to trigger new fields. I'm using Ajax for triggering the hidden part of the forms. Ajax will make a new part of the form apear in the page without having to refresh.

    The problem is that when I trigger several of the hidden parts I can submit them all to the data base at once. each of them was triggered at a different time and it has its own save button.

    How can I save them at once.


    "Proprofs" quiz making site is a good example. user can add different type of questions as he goes in making a quize the different questions for will appear on the page then will be submitted at once.





    Ajax code.
    function handleChange(cb) {

    //get the selected value
    var value = cb.value;
    var divNum = cb.value;

    //status variable for validation
    var status;
    (cb.value == 0 )?status ='new': status =2;

    alert (cb.value);

    //set image back to delete and undo deletion
    if (cb.name =='undo')
    status='delete';

    if (cb.value==0)


    //check if browser suports ajax
    var xmlhttp = null;
    if(typeof XMLHttpRequest != 'udefined'){
    xmlhttp = new XMLHttpRequest();
    }

    else if(typeof ActiveXObject != 'undefined'){
    xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
    }

    else
    throw new Error('You browser doesn\'t support ajax');
    //open connection with activateImages.php to recieve the active images as an acho
    xmlhttp.open("GET", "ManageQuestionsImages.php?status="+status + "&id="+value,true);


    if (status !='new'){
    //check if ready to recieve
    xmlhttp.onreadystatechange = function (){

    if(xmlhttp.readyState == 4)
    window.activate(xmlhttp,divNum);
    };
    xmlhttp.send(null);
    }

    else {
    //check if ready to recieve for add new template form
    xmlhttp.onreadystatechange = function (){
    if(xmlhttp.readyState == 4)
    window.activated(xmlhttp);
    };
    xmlhttp.send(null);

    }
    }

    //recieve the active images then insert them in the specified location of the page.
    function activate(xhr,divNum){
    if(xhr.status == 200){
    document.getElementById(divNum).innerHTML = xhr.responseText;
    }
    else
    throw new Error('Server has encountered an error\n'+
    'Error code = '+xhr.status);
    };

    //recieve the active form then insert it in template div.
    function activated(xhr){
    if(xhr.status == 200){
    document.getElementById('template').innerHTML = xhr.responseText;
    }
    else
    throw new Error('Server has encountered an error\n'+
    'Error code = '+xhr.status);
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,508
    Rep Power
    594
    1) Enclose your PHP code (of which there is none) in [ PHP ] tags (see sticky at the top of this forum) and your Javascript in [ CODE ] tags. Also use proper indentation for both.
    2) You only need 1 submit button if you put all the fields in a single form. You can make some of the fields hidden and then make them visible as appropriate with Javascript. There is no need to have multiple forms. You use the 'POST' method for your PHP script and use the $_POST associative array to access fields in your script (it doesn't matter to PHP if they are visible or not on the client side).
    There are 10 kinds of people in the world. Those that understand binary and those that don't.

IMN logo majestic logo threadwatch logo seochat tools logo