Jump to content

Dynamic add element on the the page


Ashish Sood

Recommended Posts

Here is a different kind of function. If I understand your needs, it should work. It requires a few changes to your HTML, which I show here:

<fieldset id="field">    <input type="text" name="incident" />                <select name="sev">        <option value="none0"></option>        <option value="###">###</option>        <option value="###">###</option>        <option value="###">###</option>        <option value="###">###</option>        <option value="###">###</option>    </select>        <select name="status">        <option value="###"></option>        <option value="###">###</option>        <option value="###">###</option>        <option value="###">###</option>        <option value="###">###</option>        <option value="###">######</option>        <option value="###">###</option>    </select>                  <textarea rows="2" cols="30" name="description_tic"></textarea></fieldset><input type="button" value="Add" id="addButton" />

The four form controls that you want duplicated are inside a fieldset. The function below clones the fieldset and its contents. Then it changes the names of the new form controls so that when the form is submitted, your server script can tell which data is which. There is another way to change the names since you are using PHP. Ask me if you want. It might be better.

window.onload = function () {    document.getElementById("addButton").onclick = cloneFieldSet;}var counter = 0;function cloneFieldSet () {    counter++;    var theField = document.getElementById("field");    var theClone = theField.cloneNode(true);    var elements = theClone.elements;    for (var el in elements) {        elements[el].name += counter.toString();    }    theClone.id += counter.toString();    theField.form.insertBefore(theClone, this);}

  • Like 1
Link to comment
Share on other sites

After clicking on the button nothing is happen no new fieldset is added on the page.

<script>window.onload = function () {    document.getElementById("addButton").onclick = cloneFieldSet;}var counter = 0;function cloneFieldSet () {    counter++;    var theField = document.getElementById("field");    var theClone = theField.cloneNode(true);    var elements = theClone.elements;    for (var el in elements) {	    elements[el].name += counter.toString();    }    theClone.id += counter.toString();    theField.form.insertBefore(theClone, this);}</script><fieldset id="field">    <input type="text" name="incident" />		        <select name="sev">	    <option value="none0"></option>	    <option value="###">###</option>	    <option value="###">###</option>	    <option value="###">###</option>	    <option value="###">###</option>	    <option value="###">###</option>    </select>        <select name="status">	    <option value="###"></option>	    <option value="###">###</option>	    <option value="###">###</option>	    <option value="###">###</option>	    <option value="###">###</option>	    <option value="###">######</option>	    <option value="###">###</option>    </select>			      <textarea rows="2" cols="30" name="description_tic"></textarea></fieldset><input type="button" value="Add" id="addButton" />

Link to comment
Share on other sites

When ever i try to insert data into my mysql database nothing is happen mean data is not inserting into the table.Please have a look into my code I have created a two file 1) index.php2) validation.php => This is used for validation,database connection string and mysql query [index.php]

<?php include_once('validation.php'); ?><html><body><head><title>Unix Daily Turnover</title></head> <script>window.onload = function (){	document.getElementById("addButton").onclick = cloneFieldSet;}var counter = 0;function cloneFieldSet (){	counter++;	var theField = document.getElementById("field");	var theClone = theField.cloneNode(true);	var elements = theClone.elements;	for (var el in elements)	{			elements[el].name += counter.toString();	}	theClone.id += counter.toString();	theField.form.insertBefore(theClone, this);} </script><form action="index.php" method="post"> <fieldset id="field">	<input type="text" name="incident" />				  	<select name="sev">			<option value="none0"></option>			<option value="###">###</option>			<option value="###">###</option>			<option value="###">###</option>			<option value="###">###</option>			<option value="###">###</option>	</select>  	<select name="status">			<option value="###"></option>			<option value="###">###</option>			<option value="###">###</option>			<option value="###">###</option>			<option value="###">###</option>			<option value="###">######</option>			<option value="###">###</option>	</select>							<textarea rows="2" cols="30" name="description_tic"></textarea></fieldset><input type="button" value="Add" id="addButton" /></center><input type="submit" value="submit" id="submit" /></center></form></body></html>

validation.php

<?phpdate_default_timezone_set('Asia/Calcutta');$date=date("m/d/Y"); if(isset($_POST['submit'])){			  if(isset($_POST['submit']))					    include("connection.php");   $incidentid=$_POST['incident'];  $severity=$_POST['sev'];  $status =$_POST['status'];  $description_ticket=$_POST['description_tic'];   $selectcondition = "SELECT * FROM test where DATE='".$date."'";//Select Query Which fetch the DATE  $check=mysql_query($selectcondition,$con);						$fetch=mysql_num_rows($check);   if($fetch >=1)				   {		  	echo "<br><b><br><font color='grey'>NOTICE<br>TURNOVER For The Same Date Is Already Exit";	echo "<br>Please Check 'Search' Or Update 'Page'. </b></font>";     }     else  {    $sql = mysql_query("INSERT INTO test (DATE,INCIDENTID,SEVERITY,STATUS,TICKETDES) VALUES   ('$date','$incidentid','$severity','$status','$description_ticket')") or die ("Database Connection Error" .mysql_error());      if($sql)		   {  	echo "<br><b> <font color='green'>Turnover for $date Submitted Successfully</b></font>";     }   } }?>

Edited by Ashish Sood
Link to comment
Share on other sites

Try adding a name="submit" attribute to your submit button. I think your test for $_POST['submit'] is failing, and you have not added any way of printing a message when this happens. also I do not understand why index.php includes validation.php. Have you tried NOT including validation.php and submitting the form directly to validation.php instead of to index.php?

Link to comment
Share on other sites

Because every added duplicate input element has the same name for name attribute, and so it will read the first one only, you could use name="sev[]" which will send it as array of all these values, entered. For the copying of values from previous you will have to clear these from the new when added.

Link to comment
Share on other sites

<?php  $sev = array_filter($_POST['sev']);$status = array_filter($_POST['status']);foreach ($sev as $sevn){echo 'Sev value individually: '.$sevn.'<br />';} foreach ($status as $stat){echo 'Status: individually: '.$stat.'<br />';} echo 'Sev Grouped together: '.implode(', ',$sev).'<br />';echo 'Status Grouped together: '.implode(', ', $status).'<br />';?>

All depend how, or what you want to save, individually or grouped together, want to include null/empty value, or not (which is what array_filter() does remove null and false values), depends if you have set it up so they must fill all addition inputs before processing, don't know? let you figure that out.

Edited by dsonesuk
Link to comment
Share on other sites

Thanks for the quick response dsonesuk I tried the above code and its giving me a warning(undefined index sev) upon load the page for the first time, but when i submitting the page its print the values coz sev got the value from variable. could you please explain me how could i use this code with two files, index.php and validation.php, Please read my earlier post i had paste the code, And also please explain how could i use the mutiple value in my validation.php which come from index,php Please let me know if you dont understand

Link to comment
Share on other sites

That is because you placed it in index.php, its more for the validation.php than index page. if you plan to insert values into the database from each multiple fieldset of form inputs one at a time use the first, the id field must be able to be duplicated if you wish to use same id.if you plan to insert values into the database from each multiple fieldset of form inputs in one row, with the values comma separated, use the second the id can be unique with this, i can see difficulty with textarea content doing it this way though.

Link to comment
Share on other sites

I don't know what you are looking for, if you enabled additional multiple identical named form elements, you must have considered how you wanted to add all these additional input data to the database, before even considering it. One way would be to store all these additional input into a 'many to one' table, where the id ref of many, would link to the id ref of 'one to many' of the user. name, address etc.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...