1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP and javascript problems in dynamic adding

Discussion in 'PHP' started by sweetguyzzz, Feb 4, 2010.

  1. #1
    I use a form to input some values but I have a one field where the user have right to put unlimited values so I use the dynamic adding javascript to add these fields.

    Dynamic adding script is working but the fields which is added in the form through javascript having a problem. The fields are adding with the default value of a "$phpvariable" instead of being empty. In dynamic adding I use the attribute value='$phpvariable'. So this is because of value attribute but it must show an empty field becz $phpvariable have no initial value so this field must be empty but its showing this value so can anybody solve my problem please ??

    MY JAVASCRIPT IS javascript.js:
    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='$alt"+i+"' 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
    Code (markup):
    and MY PHP FORM SCRIPT IS form.php:
    <?php
    
    echo "<html>
    <head>
    <script src='javascript.js' type='text/javascript'></script>
    </head>
    <body>
    
    <form name='AddFunnyImages' enctype='multipart/form-data' method='post' action='?action=add'>
    
    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>
    </body>
    </html>";
    ?>
    PHP:

     
    sweetguyzzz, Feb 4, 2010 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    value='<?= $imagesfield ?>'
     
    SmallPotatoes, Feb 4, 2010 IP
  3. v368

    v368 Guest

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    in form.php change
    <input type='button' value='Submit' onclick='AddFields()' />
    to <input type='button' value='Submit' onclick='AddFields(\'$phpvalue\')' />

    js function change to
    function AddFields(value) {
    .........
    NewDiv.innerHTML = "Alt Tag "+i+":<input type='text' name='alt"+i+"' value='"+value+i+"' class='field' /><br /><br />";

    .........
    }

    make sure value you parse into AddFields is a string.
     
    v368, Feb 4, 2010 IP