PLEASE HELP in PHP with some javascript

Discussion in 'Programming' started by sweetguyzzz, Feb 19, 2010.

  1. #1
    Hello Senior Webmasters

    I have created a form in PHP in which using a javascript to add some extra fields but when user submits a form or any error found then form will be returned with errors but problem is here that the fields added from the javascript are not returning their previous values like in a field of email address a user inputs: example@xyzcom
    and by mistake he or she did not place a dot(.) between "xyz" and "com", so there error will be occured and PHP form will be returned again but with some errors, so with error a value user entered in that javascript created field must be returned but its not returning. Any body can please solve my problem. Please send some type of script as example so this will clearly solving my problem.



    page where this example code is: http://www.funfluster.com/2.php

    MY PHP PAGE CODE IS:
    <html>
    <head>
    <script type='text/javascript'>
    function AddFields() {
    var MainDiv = document.getElementById('MoreFunnyImages');   //for getting the element(div) from where to start putting more fields
    var AddFields = document.getElementById('NoofFunnyImages').value;   //for getting number of fields to be added
    var PreviousNoOfFields = document.getElementById('FunnyImagesCount');   //this is to get the value of field which have saved the number which is given before
    var NewDivIdName = 'divFunnyImage';     //to get the id of div which have to be removed or been added
    
    
    
    for (j=1;j<=PreviousNoOfFields.value;j++) {                 //loop is for removing previous given DIVS
      var OldDiv = document.getElementById(NewDivIdName+j);     //getting the name of previous DIVS by help of there ids
      MainDiv.removeChild(OldDiv);                              //removing that DIV from the parent DIV
    }
    PreviousNoOfFields.value = AddFields;   //Now we are assigning new value to the previous value variable so next time this amount of divs will be removed
    
    
    if (AddFields && AddFields > 0) {
      for (i=1;i<=AddFields;i++) {      //for loop continue till the given value becomes equal
        var NewDiv = document.createElement('div'); //gving that what element to be created, we give DIV
        NewDiv.setAttribute('id',NewDivIdName+i);   //setting id attribute for the div
        NewDiv.innerHTML = "Alt Tag "+i+":<input type='text' name='alt"+i+"' value='' class='field' /><br /><br />";
        MainDiv.appendChild(NewDiv);                //from where to print these div we give a variable.appendChild function with attribute of newdivname
      }//loop bracket closes here
    }//if condition closes here
    }//function closes here
    </script>
    </head>
    
    <body>
    
    <?php
    extract($_GET);
    extract($_POST);
    
    if ($action == "submit") {
     $show_fields = "AddFields();";
     }
    
    echo "<form name='AddFunnyImages' enctype='multipart/form-data' method='post' action='?action=submit'>
    
    Number of Images:<input id='NoofFunnyImages' type='text' name='imagesfield' value='$imagesfield' /><input id='FunnyImagesCount' type='hidden' name='countfield' value='0' />
    <input type='button' value='Submit' onclick='AddFields()' />
    <div id='MoreFunnyImages'></div><!--All of The Images Browse Button Will Display in This DIV-->
    <input type='submit' name='submit' value='Go..' />
    </form>
    <script type='text/javascript'>$show_fields</script>
    ";
    ?>
    
    </body>
    </html>
    PHP:
    Please answer fast it is important.
    I will be very much thankful to helpers.
    And thanks in advance to helpers
     
    sweetguyzzz, Feb 19, 2010 IP
  2. NeoCambell

    NeoCambell Peon

    Messages:
    456
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I can tell you two methods.

    1. Open a session and store values in a cookie. So each time this page is loaded, we can fill the fields just as for back button

    2. Create similar number of hidden fields in html and fill them at submit. These fields will be filled by the browsers upon pressing back button. Then we can fill our dynamic fields using the values in hidden text fields.
     
    NeoCambell, Feb 21, 2010 IP
  3. Obi

    Obi Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I'm tupist! How JavaScript using with php?
     
    Obi, Feb 25, 2010 IP
  4. NeoCambell

    NeoCambell Peon

    Messages:
    456
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #4
    NeoCambell, Feb 25, 2010 IP
  5. sweetguyzzz

    sweetguyzzz Active Member

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    66
    #5
    Thanks NeoCambell,

    I have gone through your ideas but eventually when I am trying to apply these I got an idea using a javascript method. The method is to get the value from the loading page submitted by form through PHP post method and then include them in javascript by setting document.getElementById('idname').value = "$post['field_name']"

    So is it good?? And may it make more load on server as compare to others ??

    Thanks
    Waiting for your good response!
     
    Last edited: Feb 25, 2010
    sweetguyzzz, Feb 25, 2010 IP
  6. NeoCambell

    NeoCambell Peon

    Messages:
    456
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I don't think so as $post['field_name'] must be processed in php on server side.

    You can experiment something like:
    document.getElementById('idname').value = <?php echo ($post['field_name']); ?>;

    Give it a try and let me know...
     
    NeoCambell, Feb 25, 2010 IP
  7. sweetguyzzz

    sweetguyzzz Active Member

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    66
    #7
    hello NewCambell

    I try that and its successfully work. I use a loop to get values like this

    foreach($_POST as $field_name => $field_value) { 
      $get_field_value[$field_name] =$field_value;
    
      if(substr($field_name,0,3) == "alt") { 
    	$javascript_for_previous_values_of_dynamic_fields .= "document.getElementById('$field_name').value = '$get_field_value[$field_name]';
    	";	} //2nd IF Condition ends here
    } //1st LOOP ends here
    PHP:
    and then use this variable $javascript_for_previous_values_of_dynamic_fields in my code between the tag of <script></script>
     
    sweetguyzzz, Mar 2, 2010 IP