Jump to content

PHP + Javascript From Duplication


Lautarox

Recommended Posts

I have this php code with a form..

<?php
echo'<html>
<head>
<script type="text/javascript" src="addform.js"></script>
</head>
<body>';
$album = $_GET['a'];
$date = time();
$dia = date("j", $date);
$mes = date("n", $date);
$year = date("Y", $date);

echo '	<table border=0>
<div id="readroot" style="display: none">
<input type="hidden" name="album" value="'.$album.'">

<tr>
<td><input type="button" value="Eliminar Ultima" onclick="this.parentNode.parentNode.removeChild(this.parentNode);"></td>
</tr><tr>
<td>Album ID: '.$album.'</td>
</tr><tr>
<td>Fecha:</td>
<td><select name="day">';
	 for($i=1;$i<=31;$i++) {
		if($i == $dia) {
			echo '<option value="'.$i.'" selected="selected">'.$i.'</option>';
		}
		else {
			echo '<option value="'.$i.'">'.$i.'</option>';
		}
	 }
	echo'</select>';
	echo'<select name="month">';
	 for($i=1;$i<=12;$i++) {
		if($i == $mes) {
			echo '<option value="'.$i.'" selected="selected">'.$i.'</option>';
		}
		else {
			echo '<option value="'.$i.'">'.$i.'</option>';
		}
	 }
	echo '</select>';
	echo '<select name="year">';
	 for($i=$year;$i>$year-6;$i--) {
		if($i==$year) {
			echo '<option value="'.$i.'" selected="selected">'.$i.'</option>';
		}
		else {
			echo '<option value="'.$i.'">'.$i.'</option>';
		}
	 }
	echo '</select> </td>
</tr><tr>
<td>Comentario o texto Adicional</tr>
</tr><tr>
<td><textarea rows="5" cols="26" name="text" maxlenght="250"></textarea></td>
</tr>
</div>
<form name="photos" method="post" action="?go=10&d=">
<span id="writeroot"></span>
<tr>
<input type="hidden" name="ammount" value="1">
<td><input type="button" id="moreFields" onclick="moreFields();" value="Mas Fotos"></td>
<td><input type="submit" value="Subir Fotos"></td>
</tr>
</table>

</form>';
echo' </body></html>';
ob_end_flush();
?>

And I have a javascript function to duplicate the form, I don't know really why isn't working, at the begining It wasn't even showing the form, so I added an ob_end_flush(), and now it adds the form, but incorrectly, not as the javascript function does.

var counter = 0;

function moreFields() {
counter++;
var newFields = document.getElementById('readroot').cloneNode(true);
newFields.id = '';
newFields.style.display = 'block';
var newField = newFields.childNodes;
for (var i=0;i<newField.length;i++) {
	var theName = newField[i].name
	if (theName)
		newField[i].name = theName + counter;
}
var insertHere = document.getElementById('writeroot');
insertHere.parentNode.insertBefore(newFields,insertHere);
document.photos.ammount.value = counter;
}

window.onload = moreFields;

Any suggestions?

 

This is what's printing

<div id="readroot" style="display: none;">
</div><div id="" style="display: block;"> -> it creates the div and it closes it
</div><span id="writeroot"></span> -> it was supposed to write all the parts of the form before writeroot but instead it's only writing the div ¬¬
<table border="0">
<input name="album" value="12" type="hidden">

<tbody><tr>
<td><input value="Eliminar Ultima" onclick="this.parentNode.parentNode.removeChild(this.parentNode);" type="button"></td>
</tr><tr>
<td>Album ID: 12</td>
</tr><tr>
<td>Fecha:</td>
<td><select name="day">*select omitted * </select> </td>
</tr><tr>
<td>Comentario o texto Adicional</td></tr>
<tr>
<td><textarea rows="5" cols="26" name="text" maxlenght="250"></textarea></td>
</tr>

<form name="photos" method="post" action="?go=10&d="></form>
<tr>
<input name="ammount" value="1" type="hidden">
<td><input id="moreFields" onclick="moreFields();" value="Mas Fotos" type="button"></td>
<td><input value="Subir Fotos" type="submit"></td>
</tr>
</tbody></table>

Link to comment
https://forums.phpfreaks.com/topic/139940-php-javascript-from-duplication/
Share on other sites

  • 1 month later...

Fixed it up for you:

var counter = 0;

function moreFields() {
   counter++;
   var newFields = document.getElementById('readroot').cloneNode(true);
   newFields.id = '';
   newFields.style.display = 'block';
   var newField = newFields.childNodes;
   for (var i=0;i<newField.length;i++) {
      var theName = newField[i].name;
      if (theName){
      newField[i].name = theName + counter;}
   }
   var insertHere = document.getElementById('writeroot');
   insertHere.parentNode.insertBefore(newFields,insertHere);
   document.photos.ammount.value = counter;
}


window.onload = moreFields;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.